我正在开发一个屏幕,我希望根据condition.i.e加载视图控制器,条件是它应该在应用程序启动时从app delegate加载特定的视图控制器类。
if(condition success)
{
//Load viewcontroller1
} else
{
//Load viewcontroller2
}
我怎样才能实现这一目标。请帮帮我..
答案 0 :(得分:1)
只需打开Xcode,创建一个新项目,将其设为通用(iPad / iPhone),您就会看到一个例子。它为您创建了两个.xib文件。一个用于iPad,一个用于iPhone。
然后,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];
}
在这种情况下,它对两个.xib使用相同的ViewController
类(ViewController.h和.m)。但是,你当然可以改变它。只需进入Xcode图形设计器中的每个.xib(以前是Interface Builder),选择.xib,选择文件所有者,然后在Inspector选项卡上(属性...通常在右侧)你可以从组合框中选择自定义类。
因此,如果您需要为每个子类提供不同的Objective-C UIViewController
子类,则可以这样做。请记住更改上面的代码以匹配([ViewController alloc]
)。
答案 1 :(得分:1)
Apple可以看到同样的事情。创建通用应用程序。在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];
}
根据条件,他们加载不同的视图控制器。