iOS:Xcode 4.2 和导航控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8606825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: Xcode 4.2 and Navigation Controller
提问by cyclingIsBetter
In XCode 4.2 when I select "new project" and also select "single view application" but now I want to add a navigation controller. What can I do in Xcode 4.2 to do it? (without storyboard)
在 XCode 4.2 中,当我选择“新项目”并选择“单视图应用程序”但现在我想添加一个导航控制器。我可以在 Xcode 4.2 中做什么来做到这一点?(没有故事板)
采纳答案by Jin
Unless you are adding the UINavigationController to another UIViewController that is utilized for a different method of navigation, i.e. UISplitViewController or UITabBarController, I would recommend adding the UINavigationController to your application window in the AppDelegate then add the UIViewController that has your view in it.
除非您将 UINavigationController 添加到另一个用于不同导航方法的 UIViewController,即 UISplitViewController 或 UITabBarController,否则我建议将 UINavigationController 添加到 AppDelegate 中的应用程序窗口,然后添加包含您的视图的 UIViewController。
If you are adding the UINavigationController as your main UIViewController, you can easily do this programmatically in the following method in the AppDelegate:
如果您将 UINavigationController 添加为您的主 UIViewController,您可以通过 AppDelegate 中的以下方法轻松地以编程方式执行此操作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
The code I would add is:
我要添加的代码是:
UINavigationController *navcon = [[UINavigationController alloc] init];
[navcon pushViewController:self.viewController animated:NO];
self.window.rootViewController = navcon;
Now, in your AppDelegate.mit should look like this:
现在,在您的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;
}
You can further learn how to utilize the UINavigationController by checking out the UINavigationController Apple Documentationand their example projects, which you can download from the same documentation page. The example projects will help you grasp the various ways you can utilize the UINavigationController.
您可以通过查看UINavigationController Apple 文档及其示例项目来进一步了解如何使用 UINavigationController ,您可以从同一文档页面下载这些文档。示例项目将帮助您掌握使用 UINavigationController 的各种方式。
回答by lifemoveson
You have to create UINavigationController
class in your project and attach in your delegate
class meaning define an IBOutLet UINavigationController
class in your application delegate
class and define it in your delegate class. In your Interface Builder
, connect the IBOutLet
to the delegate class.
你必须创建UINavigationController
在您的项目类,并附上您的delegate
类的意思定义IBOutLet UINavigationController
在类application delegate
类,并在你的委托类定义它。在您的 中Interface Builder
,将 连接IBOutLet
到委托类。