简体   繁体   English

如何在应用程序启动时从应用程序委托加载不同的视图控制器类(即从应用程序委托)

[英]How to load different view controller class from app delegate at the time of application launch(i.e from app delegate)

I was developing a screen where i want to load view controllers based on the condition.ie with respect to condition it should load a particular view controller class from app delegate at the time of application launch. 我正在开发一个屏幕,我想基于condition.ie加载视图控制器,条件是它应该在应用程序启动时从app delegate加载特定的视图控制器类。

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

How can i achieve this .Please help me out.. 我怎样才能做到这一点。请帮帮我..

Just open Xcode, create a new project, make it Universal (iPad/iPhone) and you'll see an example of this. 只需打开Xcode,创建一个新项目,使其成为通用 (iPad / iPhone),你会看到一个这样的例子。 It creates for you two .xib files. 它为您创建了两个.xib文件。 One for iPad and one for iPhone. 一个用于iPad,一个用于iPhone。

Then, the app delegate does this: 然后,app委托执行此操作:

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

In this case, it uses the same ViewController class (ViewController.h and .m) for both .xibs. 在这种情况下,它对两个.xibs使用相同的ViewController类(ViewController.h和.m)。 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. 只需进入Xcode图形设计器中的每个.xib(以前是Interface Builder),选择.xib,选择File's Owner,然后在Inspector选项卡上(属性...通常在右侧),您可以选择一个自定义类 。组合框。

So, if you need a different Objective-C UIViewController subclass for each, you can do that. 因此,如果您需要为每个子类提供不同的Objective-C UIViewController子类,则可以这样做。 Remember to change the code above to match, too ( [ViewController alloc] ). 请记住更改上面的代码以匹配( [ViewController alloc] )。

You can see the same done by Apple. 您可以看到Apple完成相同的操作。 Create a Universal Application. 创建通用应用程序。 In appDelegate you can see 在appDelegate中你可以看到

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
} else {
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
}

Based on a condition they load different view controllers. 根据条件,他们加载不同的视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从应用委托启动视图 - Launch view from app delegate View Controller不在App Delegate中显示 - View Controller not presenting from App Delegate 从app delegate调用视图控制器方法 - Calling view controller method from app delegate 如何在应用程序委托 class 中从我的 controller class 访问插座? - How can I access an outlet from my controller class in the app delegate class? 如何从app delegate访问导航控制器? - How do I access the navigation controller from app delegate? 如何在基于窗口的iPhone应用程序中从应用程序委托类进行导航。 如何在应用程序委托类中编写pushViewcontroller方法 - How to navigate from app delegate class in window based iPhone application. How to write pushViewcontroller method in app delegate class 在位置发生重大更改时重新加载/刷新View Controller(来自App委托) - Reload/Refresh View Controller on Significant Location Change (from App Delegate) 从 App Delegate 中的 Storyboard 获取初始视图控制器 - Getting Initial View Controller from Storyboard inside the App Delegate 从应用程序代理访问另一个View Controller IBOutlet - Accessing Another View Controller IBOutlet from App Delegate 从 iOS 中的 App Delegate 调用当前视图 controller 中的方法 - Calling method in current view controller from App Delegate in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM