embedded导航控制器

我刚刚更新我的Xcode从4.2到4.3.3,我不断遇到一个问题是有可能在一个单一的视图应用程序中添加一个导航控制器,因为当我试图embedded到控制器没有任何反应。 我想有两个视图控制器通过button连接到第二个控制器和导航栏回到第一个视图控制器。

我想不出任何其他方式来连接视图控制器,请帮助我的任何想法。

  1. 如果不想添加导航控制器,则可以使用presentViewController在现有的视图控制器之间presentViewController ,从第一个到第二个,然后dismissViewControllerAnimated以返回。

  2. 假设你正在使用一个NIB(否则你只是使用embedded命令的故事板),如果你想添加一个导航控制器留在你的NIB,你可以相应地改变你的应用程序委托。

所以,你可能有一个应用程序委托,说:

 // AppDelegate.h #import <UIKit/UIKit.h> @class YourViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) YourViewController *viewController; @end 

改变这个来添加一个导航控制器(你可以在这里摆脱之前对你的主视图控制器的引用):

 // AppDelegate.h #import <UIKit/UIKit.h> //@class YourViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; //@property (strong, nonatomic) YourViewController *viewController; @property (strong, nonatomic) UINavigationController *navigationController; @end 

然后,在你的应用程序委托的实现文件中,你有一个didFinishLaunchingWithOptions ,可能是这样的:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } 

你可以改变说:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. //self.viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil]; //self.window.rootViewController = self.viewController; YourViewController *viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; self.window.rootViewController = self.navigationController; [self.window makeKeyAndVisible]; return YES; } 

完成之后,您现在可以从一个NIB视图控制器导航到另一个使用pushViewController并使用popViewControllerAnimated返回。 在你的viewDidLoad你也可以使用self.title = @"My Title"; 命令来控制视图导航栏中显示的内容。 您可能还想要更改NIB中的“顶部栏”属性,以包含导航栏模拟度量标准,以便您可以布置屏幕并对其外观如何:

在这里输入图像说明

很显然,如果你有一个非ARC项目,这些视图控制器的alloc / init行也应该有一个autorelease (当你看你的应用程序委托时,这是显而易见的)。