0

我创建 i navigationController 项目名称:autoload,然后创建两个 uiviewContorller 命名为:second,three

我想要的过程是加载rootView在方法中加载第二个:“viewDidLoad”然后在方法“viewdidload”中自动加载三个,这里是代码:

根视图:

- (void)viewDidLoad {
    self.title = @"first";
    Second *second = [[Second alloc] init];
    AutoLoadAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [delegate.navigationController pushViewController:second animated:YES];
    [super viewDidLoad];
}

第二:

- (void)viewDidLoad {
    self.title = @"second";
    [super viewDidLoad];
}

现在构建一个程序,我可以自动加载到第二个非常正确的内容,包括标题内容和导航按钮

然后我想在第二个中自动加载三个,所以在第二种方法中添加代码:“viewdidload”

第二:

- (void)viewDidLoad {
    self.title = @"second";
    **Three *three = [[Three alloc] init];
    AutoLoadAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [delegate.navigationController pushViewController:three animated:YES];**
    [super viewDidLoad];
} 

最后将标题添加到三个:

三:

- (void)viewDidLoad {
    self.title = @"three";
    [super viewDidLoad];
}

然后build走,你会发现内容对了“三”但是标题错了“二”,应该是“三”,你也会发现导航按钮错了

我的那个有什么问题,我应该怎么做才能实现自动加载到三个的程序?

笔记:

我试试看:如果我在第二个添加一个按钮并移动代码

Three *three = [[Three alloc] init];
    AutoLoadAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [delegate.navigationController pushViewController:three animated:YES];

到 ibaction ,它将正常工作,但我希望它自动加载

4

1 回答 1

2

不要在 viewDidLoad 方法中调用 pushViewController,而是尝试在 applicationDidFinishLaunching 方法中设置 viewControllers 数组:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    RootViewController *root = [[RootViewController alloc] init];
    root.title = @"root";
    Second *second = [[Second alloc] init];
    second.title = @"second";
    Three *three = [[Three alloc] init];
    three.title = @"three";
    [navigationController setViewControllers:[NSArray arrayWithObjects:root,second,three,nil] animated:YES];
    [root release];
    [second release];
    [three release];

    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}
于 2010-03-02T04:26:04.090 回答