如何在应用程序启动时从应用程序委托加载不同的视图控制器类(即从应用程序委托) [英] How to load different view controller class from app delegate at the time of application launch(i.e from app delegate)

查看:30
本文介绍了如何在应用程序启动时从应用程序委托加载不同的视图控制器类(即从应用程序委托)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个屏幕,我想在其中根据条件加载视图控制器.即,根据条件,它应该在应用程序启动时从应用程序委托加载特定的视图控制器类.

I was developing a screen where i want to load view controllers based on the condition.i.e with respect to condition it should load a particular view controller class from app delegate at the time of application launch.

     if(condition success)
 { 
//Load viewcontroller1 
} else 
{ 
//Load  viewcontroller2
 }

我怎样才能做到这一点.请帮帮我..

How can i achieve this .Please help me out..

推荐答案

只需打开 Xcode,创建一个新项目,使其成为 Universal (iPad/iPhone),您就会看到这样的示例.它为您创建两个 .xib 文件.一种用于 iPad,一种用于 iPhone.

Just open Xcode, create a new project, make it Universal (iPad/iPhone) and you'll see an example of this. It creates for you two .xib files. One for iPad and one for iPhone.

然后,应用程序委托执行以下操作:

Then, the app delegate does this:

- (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];
    }

在这种情况下,它对两个 .xib 使用相同的 ViewController 类(ViewController.h 和 .m).但是,您当然也可以更改它.只需进入 Xcode 图形设计器(以前是 Interface Builder)中的每个 .xib,选择 .xib,选择 File's Owner 并在 Inspector 选项卡上(属性...通常在右侧)可以从组合框中选择自定义类.

In this case, it uses the same ViewController class (ViewController.h and .m) for both .xibs. However, you can certainly change that, too. Just go into each .xib in the Xcode graphical designer (used to be Interface Builder), select the .xib, select File's Owner and on the Inspector tab (properties ... usually on the right) you can choose a Custom Class from a combo box.

所以,如果你需要一个不同的 Objective-C UIViewController 子类,你可以这样做.记住也要更改上面的代码以匹配([ViewController alloc]).

So, if you need a different Objective-C UIViewController subclass for each, you can do that. Remember to change the code above to match, too ([ViewController alloc]).

这篇关于如何在应用程序启动时从应用程序委托加载不同的视图控制器类(即从应用程序委托)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆