如果您不想添加导航控制器,您可以使用以下命令在现有视图控制器之间进行转换presentViewController
从第一个转到第二个,并且dismissViewControllerAnimated
回来。
假设您使用的是 NIB(否则您只需使用 Storyboard 的 embed 命令),如果您想添加一个与 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 视图控制器从一个 NIB 视图控制器导航到另一个 NIB 视图控制器pushViewController
并返回popViewControllerAnimated
。在你的viewDidLoad
您还可以使用self.title = @"My Title";
命令控制视图导航栏中显示的内容。您可能还需要更改 NIB 中的“顶部栏”属性以包含导航栏模拟指标,以便您可以布局屏幕并清楚地了解其外观:
显然,如果您有一个非 ARC 项目,那么带有视图控制器的 alloc/init 的那些行也应该有一个autorelease
,也是(当您查看应用程序委托时,这一点会很明显)。