在XCode 4.2中,当我选择“新项目”并选择“单一视图应用程序”但现在我想添加一个导航控制器。我在Xcode 4.2中可以做些什么呢? (没有故事板)
答案 0 :(得分:5)
除非您将UINavigationController添加到另一个用于不同导航方法的UIViewController,即UISplitViewController或UITabBarController,我建议将UINavigationController添加到AppDelegate中的应用程序窗口,然后添加包含您视图的UIViewController
如果要将UINavigationController添加为主UIViewController,可以使用AppDelegate中的以下方法以编程方式轻松完成此操作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
我要添加的代码是:
UINavigationController *navcon = [[UINavigationController alloc] init];
[navcon pushViewController:self.viewController animated:NO];
self.window.rootViewController = navcon;
现在,在您的 AppDelegate.m 中,它应如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
}
else
{
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
}
UINavigationController *navcon = [[UINavigationController alloc] init];
[navcon pushViewController:self.viewController animated:NO];
self.window.rootViewController = navcon;
[self.window makeKeyAndVisible];
return YES;
}
您可以通过查看UINavigationController Apple Documentation及其示例项目来进一步了解如何利用UINavigationController,您可以从同一文档页面下载这些项目。示例项目将帮助您掌握使用UINavigationController的各种方法。
答案 1 :(得分:0)
您必须在项目中创建UINavigationController
类并附加到delegate
类中,这意味着在IBOutLet UINavigationController
类中定义application delegate
类并在您的委托类中定义它。在Interface Builder
中,将IBOutLet
与代理类相关联。