我正在开发一个屏幕,我想在其中根据条件加载 View Controller 。即关于条件,它应该在应用程序启动时从应用程序委托(delegate)加载特定的 View Controller 类。
if(condition success)
{
//Load viewcontroller1
} else
{
//Load viewcontroller2
}
我怎样才能做到这一点。请帮助我......
最佳答案
只需打开 Xcode,创建一个新项目,将其设为 Universal (iPad/iPhone),您就会看到这样的示例。它为您创建两个 .xib 文件。一个用于 iPad,一个用于 iPhone。
然后,应用委托(delegate)执行此操作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
在这种情况下,它对两个 .xibs 使用相同的 ViewController
类(ViewController.h 和 .m)。但是,您当然也可以更改它。只需进入 Xcode 图形设计器(以前是 Interface Builder)中的每个 .xib,选择 .xib,选择 File's Owner 并在 Inspector 选项卡上(属性...通常在右侧)您可以从组合框中选择一个自定义类。
因此,如果您需要为每个不同的 Objective-C UIViewController
子类,您可以这样做。记住也要更改上面的代码以匹配 ([ViewController alloc]
)。
关于iphone - 如何在应用程序启动时从应用程序委托(delegate)加载不同的 View Controller 类(即从应用程序委托(delegate)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11607024/