如何将一个视图控制器推送到另一个没有nib文件的视图控制器?

时间:2012-07-18 10:53:05

标签: iphone objective-c ios uiviewcontroller

如何将oneviewcontroller推送到没有nib文件的另一个viewcontroller

这就是我目前正在做的事情。 UserViewController没有nib文件,但我仍在使用initWithNibName:@"UserViewController"

-(IBAction)userProfile:(id)sender {

    UserViewController *userViewController = [[UserViewController alloc]
                                                            initWithNibName:@"UserViewController" bundle:nil];
    [self.navigationController pushViewController:userViewController animated:YES ];

    [userViewController release];  

}

感谢您的帮助!!!

3 个答案:

答案 0 :(得分:3)

你只需使用:

[[UserViewController alloc] init];

您可以在loadView方法中构建相应的视图。 但请查看Apple的View Controller Guide

答案 1 :(得分:2)

如果你没有任何nib文件,你不应该提及任何。请找到以下代码:

-(IBAction)userProfile:(id)sender {

UserViewController *userViewController = [[UserViewController alloc]
                                                        init];
[self.navigationController pushViewController:userViewController animated:YES ];

[userViewController release];  

}  

您可以将-(id)init方法放在 UserViewController 中并设置全局变量。

答案 2 :(得分:0)

-(IBAction)userProfile:(id)sender {

UserViewController *userVC = [[UserViewController alloc] init];

// Replace the current view controller
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
[viewControllers removeLastObject];
[viewControllers addObject:userVC];
[[self navigationController] setViewControllers:viewControllers animated:YES];
}