2012-02-19 82 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

这可能会对你有所帮助。

享受编码:)

相关问题