我已经看到了与此主题相关的其他帖子,但我无法解读与我遇到的问题相关的问题。我的项目的目标是有3个表视图,1个根,然后在根中选择一个项后推送到堆栈的第二个,然后在第二个视图中选择一个项后第三个推到堆栈。
我试图通过以编程方式使用名为FirstView的根创建导航控制器来实现此目的。然后在FirstView xib中包含第二个视图控制器,将SecondView推送到堆栈。然后在SecondView中使用第三个视图控制器将ThirdView推送到堆栈。
结果是我得到FirstView以显示并正确地将SecondView推送到堆栈。然后,当我在SecondView表中选择一个项目时,在尝试将ThirdView推送到堆栈时出现以下错误:“应用程序试图在目标上推送一个nil视图控制器。”
我将控制器连接到第二和第三控制器的插座,所以我不确定这两者之间的差异以及为什么第三个控制器为零。附上以下代码以获取推送视图的所有引用(对不起,如果这很多)
TestAppDelegate.h
#import <UIKit/UIKit.h>
@class RootViewController;
@interface TestAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UINavigationController *navigationController;
@property (strong, nonatomic) UIWindow *window;
@end
TestAppDelegate.m
#import "FirstView.h"
#import "TestAppDelegate.h"
@implementation TestAppDelegate
@synthesize window = _window;
@synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootController = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
FirstView.h
#import <UIKit/UIKit.h>
@class SecondView;
@interface FirstView : UITableViewController
@property (nonatomic, strong) NSMutableArray *mList;
@property (nonatomic, strong) IBOutlet SecondView *secondController;
@end
FirstView.m
#import "FirstView.h"
#import "SecondView.h"
#import "TestAppDelegate.h"
@implementation FirstView
@synthesize mList;
@synthesize secondController;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:self.secondController animated:YES];
}
SecondView.h
#import <UIKit/UIKit.h>
@class ThirdView;
@interface SecondView : UITableViewController
@property (nonatomic, strong) NSMutableArray *secondList;
@property (nonatomic, strong) IBOutlet ThirdView *thirdController;
@end
SecondView.m
#import "ThirdView.h"
#import "SecondView.h"
#import "FirstView.h"
#import "TestAppDelegate.h"
@implementation SecondView
@synthesize secondList;
@synthesize thirdController;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:self.thirdController animated:YES];
}