【问题标题】:Can't Link up UIButton with File's Owner in XCode无法在 XCode 中将 UIButton 与文件的所有者链接起来
【发布时间】:2011-09-23 04:21:57
【问题描述】:

当我意识到我无法将我正在创建的按钮链接到我的文件时,我开始了为 ios 学习 Objective-c 的无畏之旅,并尝试在界面构建器中构建我的视图所有者。我已确保我的文件所有者选择了我的视图控制器,并尝试重新启动 xcode 和界面生成器。这是我的 .h 和 .m 文件的内容:

我的 CalculatorViewController.h 文件:

#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"
@interface CalculatorViewController : UIViewController {
     IBOutlet UILabel *display;
     CalculatorBrain *brain;
}
- (IBAction):digitPressed:(UIButton *)sender;
- (IBAction):operationPressed:(UIButton *)sender;

@end

还有 CalculatorViewController.m 文件:

#import "CalculatorViewController.h"

@implementation CalculatorViewController

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

简而言之,每次我单击 Interface Builder 视图中的按钮时,按住 CTRL 并将蓝线拖到“File's Owner”,什么都没有发生。在我正在观看的教程中(The Stanford Fall 2010 IOS tutorials, 第 2 课 - 如果有帮助的话)显示 File's Owner 突出显示并像冠军一样工作。任何帮助将非常感激。谢谢!

【问题讨论】:

  • 检查文件的所有者。是 CalculatorViewController 吗?
  • Bourne:虽然这并没有解决 James 的问题,但它帮助我解决了我的问题,所以感谢您分享您的解决方案。

标签: objective-c ios xcode


【解决方案1】:

IBAction 的定义无效。 (额外的冒号)

改变

- (IBAction):digitPressed:(UIButton *)sender;
- (IBAction):operationPressed:(UIButton *)sender;

到

- (IBAction) digitPressed:(UIButton *)sender;
- (IBAction) operationPressed:(UIButton *)sender;

【讨论】:

  • 就是这样!非常感谢。我现在觉得很傻。仍在整理 Obj-C 的语法。再次感谢。
【解决方案2】:

所以您想将您的按钮与 IBAction 关联起来吗?我做对了吗?您需要右键单击按钮,选择事件(通常是 Touch Up Inside),然后(从事件右侧的圆圈)拖动到所有者。现在,如果您想将某些东西连接到 IBOutlet(通常您使用 UITextField 等),您可以将 File Owner 拖到控件上并从弹出窗口中选择出口。

【讨论】:

    【解决方案3】:
     #import "CalculatorViewController.h"
    
    @implementation CalculatorViewController
    
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    
        // Release any cached data, images, etc that aren't in use.
    }
    
       - (void)viewDidUnload {
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (IBAction):digitPressed:(UIButton *)sender{
    }
    - (IBAction):operationPressed:(UIButton *)sender{
    }
    - (void)dealloc {
        [super dealloc];
    }
    
    @end
    

    【讨论】:

      【解决方案4】:

      - (IBAction)后面不能有冒号

      【讨论】:

        【解决方案5】:

        两件事:

        - (IBAction)后面不能有冒号

        将此行添加到您的代码中。

        //.h 文件

        IBOutlet UIButton *yourButtonName;
        
        
        @property (nonatomic, retain)IBOutlet UIButton *yourButtonName;
        

        /.m 文件

        @synthesize yourButtonName;
        

        现在拖动它就完成了。

        【讨论】:

          猜你喜欢
          • 2010-12-03
          • 1970-01-01
          • 2021-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-30
          • 2016-09-05
          • 2018-06-18
          相关资源
          最近更新 更多