【问题标题】:loading viewDidLoad at second xib issue在第二个 xib 问题上加载 viewDidLoad
【发布时间】:2012-11-01 19:52:30
【问题描述】:

我有两个 xib 文件。我从“ViewController.xib”开始,当用户单击一个按钮时,它会加载一个名为“GamePage.xib”的不同 xib 文件。

按钮的代码是:

-(IBAction)startButtonClicked{    
    UIViewController *gamePage = [[UIViewController alloc] initWithNibName:@"GamePage" bundle:nil];
    [self presentViewController:gamePage animated:NO completion:nil];
}

当按钮被点击时,第二个xib显示在屏幕上,但它的“viewDidLoad”方法没有运行……

这是为什么呢?

【问题讨论】:

  • 第二个视图的viewDidLoad代码是什么?

标签: iphone xcode sdk


【解决方案1】:
UIViewController *gamePage = [[UIViewController alloc] initWithNibName:@"GamePage" bundle:nil];

这似乎不正确。

viewDidLoad 可能正在运行,但它是 Apple 的 UIViewControllerviewDidLoad。当你初始化它时,你应该使用你的类而不是UIViewController。一般来说,您应该使用您需要调用的 viewDidLoad 类。

【讨论】:

    【解决方案2】:

    您的 XIB 必须有其所有者。您需要创建一个从 UIViewController 派生的类,然后您需要使该类成为您的 XIB 的所有者。 只需按照以下步骤操作:

    • 打开你的项目
    • 在左侧导航面板中选择项目名称
    • 右键单击并选择新建文件
    • 选择 Objective C 类型类
    • 当你点击它时,它会要求你给它一个名字
    • 假设你给了 GameViewController
    • 检查“使用 XIB 作为接口”
    • 然后添加文件
    • 现在在您的导航面板中,您将看到三个类 游戏视图控制器.h 游戏视图控制器.m 游戏视图控制器.xib 在您的 xib 中创建您的界面。

    现在,如果您必须移动到此类并显示其视图,请编写以下代码:

     -(IBAction)startButtonClicked{    
        GameViewController *gamePage = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil];
        [[self navigationController] pushVIewController:gamePage animated:YES];
        [gamePage release]; //If not used ARC
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 2013-07-04
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多