Window 应用程序非常适合您。在您的 AppDelegate 文件中,您应该有这样的部分:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//instantiate the venue view controller object
YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:@"YourView" bundle:[NSBundle mainBundle]];
// Configure and show the window
[window addSubview:[yourViewController view]];
[window makeKeyAndVisible];
}
这是声明、分配和添加自定义视图到窗口的代码部分。对于如何添加第二个视图,您有几个选择。您可以添加它来代替这个,或者使用导航控制器在这个之后添加它。要添加导航控制器,请将上述方法更改为如下所示:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//instantiate the venue view controller object
YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:@"YourView" bundle:[NSBundle mainBundle]];
UINavigationController *yourViewControllerWrapper = [[UINavigationController alloc] initWithRootViewController: yourViewController];
// Configure and show the window
[window addSubview:[yourViewControllerWrapper view]];
[window makeKeyAndVisible];
}
在那里,我们创建您的自定义视图,然后将其包装在导航控制器中。导航控制器是添加到窗口的内容。接下来切换到第二个视图的代码如下所示,假设您在按下按钮时切换视图:
-(IBAction)switchViewController{
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
[self.navigationController pushViewController:secondViewController];
}
当然,你应该换行
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
以正确的方式实例化您的第二个视图控制器。这可以来自上面的 nib 文件,也可以通过编程方式。
就创建视图文件而言,您应该在 Interface builder 中为所有内容的布局创建一个 nib,然后为 ViewController 代码本身创建一个 .h 和 .m 文件。