iOS: Xcode 4.2 和导航控制器

5
在XCode 4.2中,当我选择“新项目”并选择“单视图应用程序”时,现在我想添加一个导航控制器。在Xcode 4.2中,我该怎么做呢?(不使用storyboard)
2个回答

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 苹果文档和他们的示例项目来进一步学习如何利用 UINavigationController。您可以从同一文档页面下载示例项目,这些示例项目将帮助您掌握各种使用 UINavigationController 的方法。


0
你需要在你的项目中创建一个UINavigationController类,并将其附加到你的delegate类中,也就是在你的application delegate类中定义一个IBOutLet UINavigationController类,并在你的delegate类中定义它。在你的Interface Builder中,将IBOutLet连接到delegate类。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接