[英]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.