我正在开发一个 iPhone 应用程序,但发现我需要另一个 View /窗口来让用户在那里输入和保存数据/信息。
如何添加另一个 View ?我是否将其添加到界面生成器中,然后将其链接到主应用程序委托(delegate)中,或者它是否有自己的 .h 和 .m 文件。
我选择了一个窗口 View 应用程序开始,我是否需要从一个翻转 View 应用程序重新开始,或者如果我在那里有正确的代码,无论如何都可以添加它。
非常感谢
卡尔
最佳答案
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];
}
这是声明、分配和添加自定义 View 到窗口的代码部分。对于如何添加第二个 View ,您有几个选择。您可以添加它来代替这个,或者使用导航 Controller 在这个之后添加它。要添加导航 Controller ,请将上述方法更改为如下所示:
- (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];
}
在那里,我们创建您的自定义 View ,然后将其包装在导航 Controller 中。导航 Controller 是添加到窗口的内容。接下来切换到第二个 View 的代码如下所示,假设您在按下按钮时切换 View :
-(IBAction)switchViewController{
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
[self.navigationController pushViewController:secondViewController];
}
当然,你应该更换线路
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
用正确的方式实例化你的第二个 View Controller 。这可以来自像上面这样的 nib 文件,或者以编程方式。
至于创建 View 文件,你应该在 Interface builder 中为所有内容的布局创建一个 nib,然后创建一个
.h
和 .m
ViewController
的文件代码本身。
关于ios - iPhone 应用程序 - 添加另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/969773/