从UITableView单元格转到详细信息视图

时间:2012-02-19 11:32:39

标签: iphone xcode ios5 tableview

我已经搜索了很多关于这个主题的内容,但我无法使用此代码。当我执行它时,它只显示我的测试NSLog,但从一个视图到另一个视图的代码不做任何事情。在这里您有以下代码:

// 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;
}

我不知道在这里编辑什么来做到这一点:     [[self navigationController] pushViewController:controller animated:YES]; 工作正常。

2 个答案:

答案 0 :(得分:0)

我猜数据源和委托设置或导航控制器存在问题。

查看本教程UITableView Tutorial

这可能对您有所帮助。

享受编码:)

答案 1 :(得分:0)

我认为问题出在 [self navigationController] ..在这行代码上设置了一个断点,并且你会发现它的值= nil 因为这个你没有你的细节控制器..你可以像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];