【问题标题】:UIButton that's created programmatically crashing when method called调用方法时以编程方式创建的 UIButton 崩溃
【发布时间】:2012-03-22 13:40:22
【问题描述】:

我有一个 rootViewController 和一个以编程方式创建的 UIButton。我希望这个 UIButton 显示另一个视图控制器。由于某种原因,它崩溃并出现以下错误:

架构 i386 的未定义符号:
“_OBJC_CLASS_$_TutorialViewController”,引用自: RootViewController.old 中的 objc-class-ref:未找到架构 i386 collect2 的符号:ld 返回 1 个退出状态

这是创建 Info UIButton 的代码。这段代码在 loadView 方法中:

 // Create a Button to get Help          
 UIButton *helpButton =  [UIButton buttonWithType:UIButtonTypeInfoDark ] ;
 CGRect buttonRect = helpButton.frame;

 // CALCulate the bottom right corner
 buttonRect.origin.x = self.view.frame.size.width - buttonRect.size.width - 8;
 buttonRect.origin.y = buttonRect.size.height - 8; 
 [helpButton setFrame:buttonRect];

 [helpButton addTarget:self action:@selector(doHelp:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:helpButton];

}

这是转换到另一个视图控制器的操作:

- (IBAction)doHelp:(id)sender{
 NSLog(@"help button pressed");

 TutorialViewController *sampleView = [[[TutorialViewController alloc] init] autorelease];
 [sampleView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
 [self presentModalViewController:sampleView animated:YES];
}

感谢您的帮助。

【问题讨论】:

  • 你为什么用“init”而不是“initWithNibName:bundle:”来初始化视图控制器?
  • init 应该没问题,最坏的情况是 viewController 没有视图并且显示为黑色。也许他从这个init方法调用initWithNibName:bundle:。我不时这样做。
  • 好的,所以这不是崩溃而是构建失败,对吧?事实上,这让我很困惑。

标签: iphone objective-c ios


【解决方案1】:

adig 可能是对的。

此外,从技术上讲,这并不是“崩溃”。它“无法构建,出现此链接器错误”。您可以说这是一个链接器错误,因为它说“ld 返回 1 个退出状态”。 ld 是链接器。

在幕后,XCode 在运行之前编译并链接您的代码。如果它在编译或链接期间失败,则它是“构建失败”,而不是“崩溃”。崩溃是应用程序构建时,但在运行时突然停止。造成这种情况的一个常见原因是访问 nil 指针。

【讨论】:

    【解决方案2】:

    检查您的 Target 设置,在 Compile Sources 列表下的 Build Phases 上,您在列表中有 TutorialViewController.m 文件。

    【讨论】:

    • 或者选择 TutorialViewController.m 并在右侧栏中勾选“Target Membership”。
    • @MatthiasBauch 好样的!不知道这个:)
    • 你是对的!我将 TutorialViewController 文件复制到项目中。复制过程中一定有问题...感谢您的帮助!
    【解决方案3】:

    你必须写(void)doHelp而不是(IBAction)doHelp:(id)sender....试试这个:)

    【讨论】:

      【解决方案4】:

      您是在#importing .h 文件,而不是 .m 文件吗?

      问题不在于你的 UIButton,问题在于 TutorialViewController 类没有被正确编译。

      【讨论】:

        【解决方案5】:

        非常基本但有时我们可能会错过它:) 你在 .h 文件中声明了相同的方法吗?

        【讨论】:

          【解决方案6】:

          我认为你必须写(void)而不是(IBAction)

          【讨论】:

          • 没有 IBAction 被定义为 void:#define IBAction void
          • 但是按钮不是由 IB 创建的......所以我认为我们不应该写 (void) 而不是 (IBOutlet) 吗??
          • @hanumanDev:从你的方法中删除(id)发件人......我认为这是最终的解决方案......
          • @Goti 您的回答不正确。在与 UI 元素关联的操作的情况下,使用 (void) 而不是 (IBAction) 是错误的概念
          猜你喜欢
          • 2011-09-03
          • 2012-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多