2012-02-19 81 views
1

我已經搜索了很多關於此主題的內容,但無法獲得此代碼的工作。當我執行它時,它只顯示我的測試NSLog,但從一個視圖到另一個視圖的代碼不會執行任何操作。在這裏,你有下面的代碼:從UITableView單元轉到Detail視圖

//FirstViewController.h

#import <UIKit/UIKit.h> 
#import "StationDetailsViewController.h" 


@interface FirstViewController : UIViewController{ 
    NSArray *ListaEstaciones; 
    NSArray *ListaID; 
} 
@property (nonatomic, retain) NSArray *ListaEstaciones; 
@property (nonatomic, retain) NSArray *ListaID; 
@end 

//FirstViewController.m

#import "FirstViewController.h" 
#import "StationDetailsViewController.h" 
@implementation FirstViewController 
@synthesize ListaEstaciones; 
@synthesize ListaID; 

//... 

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
NSLog(@"Pushing..."); 
StationDetailsViewController *controller = [[StationDetailsViewController alloc] initWithNibName:@"StationDetailsViewController" bundle:nil]; 
[[self navigationController] pushViewController:controller animated:YES]; 
[controller release], controller = nil; 
} 

@end 

我已經試過很多的教程和我的書,但我不知道哪裏不對。非常感謝,夥計們。

編輯:讀你的答案我發現錯誤是在AppDelegate.m其中rootViewController被定義。

//AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; 
    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil]; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

我不知道該怎麼在這裏編輯,使這個: [自我navigationController] pushViewController:控制器動畫:是]; 正常工作。

+1

你可能沒有self.navigationController。你有一個導航控制器作爲rootViewController嗎?從基於導航的模板開始 – 2012-02-19 11:36:14

+0

我的應用程序是一個TabBarController,其中第一個選項卡是我在此複製的視圖。在AppDelegate.m我有這個:self.window.rootViewController = self.tabBarController; – 2012-02-19 11:39:17

回答

0

我認爲這個問題是在[自我navigationController] ..把一個破發點上這行代碼,並probaly你會發現它的價值= 的曲子,你不是有你詳細控制器..你能解決這個像UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yourMainViewControllerInstance];

這本的appDelegate代碼:

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
// Override point for customization after application launch. 
UIViewController *firstViewController = [[[FirstViewController alloc] initWithNibName:@"FBConFirstViewController" bundle:nil] autorelease]; 
UIViewController *secondViewController = [[[SecondViewController alloc] initWithNibName:@"FBConSecondViewController" bundle:nil] autorelease]; 
self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:firstViewController]autorelease]; 
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav, secondViewController, nil]; 
self.window.rootViewController = self.tabBarController; 
[self.window makeKeyAndVisible]; 
+0

我必須把這行代碼放在哪裏?在didSelectRowAtIndexPath中? – 2012-02-19 12:19:35

+0

在您介紹FirstViewController的類中沒有。 – 2012-02-19 12:22:00

+0

你的意思是寫UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:FirstViewController];在AppDelegate.m中的didFinishLaunchingWithOptions中使用 ? – 2012-02-19 12:24:35

0

我猜數據源和委託設置或導航控制器都有問題。

檢查本教程UITableView Tutorial

這可能會對你有所幫助。

享受編碼:)

相關問題