我想用不同的 Xib 启动我的应用程序。我该怎么做?
谢谢
最佳答案
如果您使用的是界面生成器:
在 SupportingFiles 下的 -info.plist 中,查找名为“Main nib file base name”的键。将其更改为您希望它首先加载的 XIB
您还可以将该条目完全从 plist 中取出,并在 main.m 中为其指定您的 appDelegate 的名称:
int retVal = UIApplicationMain(argc, argv, nil, @"HelloViewAppDelegate");
然后在您的 appDelegate 中,您可以根据您的代码和逻辑手动加载您的第一个 View Controller 。就个人而言,我更喜欢这个,因为它更清楚 - 这是我的委托(delegate)和加载它的代码。它没有我需要记住的 IB 中的所有绑定(bind)。
- (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]];
此时,如果它没有编译,您需要编辑您的问题并将您的 main.m 和 appDelegate 完全按原样粘贴到问题中。
关于iphone - 使用不同的 XIB 启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8028421/