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

发布于 2024-12-14 03:15:27 字数 44 浏览 0 评论 0原文

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

谢谢

I want to start my app up with a different Xib. How would I do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城仅有旧梦在 2024-12-21 03:15:27

如果您使用的是界面生成器:

在 SupportingFiles 下的 -info.plist 中,查找名为“Main nib file base name”的键。将其更改为您希望它首先加载的 XIB

您还可以将该条目从 plist 中取出,然后在 main.m 中为其指定您的 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]]; 

此时,如果它没有编译,您需要编辑您的问题并将 main.m 和 appDelegate 完全按原样粘贴到问题中。

If you're using interface builder:

Under SupportingFiles, in -info.plist, look for a key named "Main nib file base name". Change that to the XIB you want it to load first

You can also take that entry out of the plist altogether an in main.m give it your appDelegate's name:

    int retVal = UIApplicationMain(argc, argv, nil, @"HelloViewAppDelegate");

Then in your appDelegate, you can manually load your first view controller based on your code and logic. Personally, I like this better because it's much clearer to - here's my delegate and code to load it. It doesn't have all the bindings in IB I need to remember.

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

EDIT:

Answering your comment below. You pasted this invalid code:

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

That's not valid. you need an instance variable name. By referring to it as "ViewController" your attempting to call class member variables. If your class is called ViewController then it should be:

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

At this point, if it's not compiling you need to edit your question and paste your main.m and appDelegate exactly as is into the question.

怀中猫帐中妖 2024-12-21 03:15:27

MainWindow.xib 文件只是一个提供应用程序 UIWindow 的 shell。虽然您可以将 UIViewController 放在 MainWindow.xib 中,但您也可以在应用程序委托上有一个未连接的 UIViewController 出口,并选择在运行时在 application:didFinishLaunchingWithOptions:方法,例如:

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

The MainWindow.xib file is just a shell that provides the application's UIWindow. While you can put a UIViewController in MainWindow.xib, you can also just have an unconnected UIViewController outlet on your app delegate and pick which nib to load at runtime in your application:didFinishLaunchingWithOptions: method, e.g.:

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文