从UITableView单元格转到Detail视图



我已经搜索了很多关于这个主题的内容,但我无法获得这些代码。当我执行它时,它只显示我的测试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:控制器动画:YES];工作正常。

我认为问题出在[self-navigationController]中。。在这行代码上放一个断点,很可能你会发现它的值=nil,因为你没有详细控制器。。你可以像UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yourMainViewControllerInstance]; 那样解决这个问题

This 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];

我想数据源和委托设置或导航控制器有问题。

查看此教程UITableView教程

这可能对你有帮助。

享受编码:)

最新更新