使用不同的XIB启动应用程序



我想用不同的Xib启动我的应用程序。我该怎么做呢?

谢谢

如果你正在使用interface builder:

SupportingFiles下,在-info中。plist,寻找一个名为"主nib文件基名"的键。将其更改为您希望首先加载的XIB

您也可以将该条目完全从列表中取出。我给它一个appDelegate的名字:

    int retVal = UIApplicationMain(argc, argv, nil, @"HelloViewAppDelegate");
然后在你的appDelegate中,你可以根据你的代码和逻辑手动加载你的第一个视图控制器。就我个人而言,我更喜欢这个因为它更清晰-这是我的委托和加载它的代码。它没有IB中我需要记住的所有绑定。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
    CGRect windowBounds = screenBounds;
    windowBounds.origin.y = 0.0;
    // init window
    [self setWindow: [[UIWindow alloc] initWithFrame:screenBounds]];
    // init view controller
    _mainViewController = [[MainViewController alloc] init];
    [[self window] addSubview:[_mainViewController view]];
    [self.window makeKeyAndVisible];
    return YES;
}
编辑:

回答你下面的评论。您粘贴了无效代码:

// init view controller 
ViewController = [[ViewController alloc] init]; 
[[self window] addSubview:[ViewController view]]; 

这是无效的。您需要一个实例变量名。通过引用它为"ViewController"你试图调用类成员变量。如果你的类叫做ViewController那么它应该是:

// notice the instance myviewController is of type ViewController
ViewController *myViewController = [[ViewController alloc] init]; 
// notice calling view against instance (myViewController)
[[self window] addSubview:[myViewController view]]; 

此时,如果没有编译,则需要编辑问题并粘贴主文本。

主窗口。xib文件只是一个外壳,它提供了应用程序的ui窗口。你可以在主窗口中放置一个UIViewController。xib,你也可以在你的应用程序委托上有一个未连接的UIViewController出口,并选择在运行时在你的application:didFinishLaunchingWithOptions:方法中加载哪个nib,例如:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {
  if (case1)
    self.viewController = [[[MyViewController1 alloc] initWithNibName:nil bundle:nil] autorelease];
  else if (case 2)
    self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
  else
    self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
  [window addSubview:self.viewController.view];
  [window makeKeyAndVisible];
  return YES;
}

最新更新