2

我有这是viewDidLoad:

// Set up register button
UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
[registerButton setTitle:@"Register" forState:UIControlStateNormal];
registerButton.frame = CGRectMake(0.0, 0.0, self.screenWidth / 2.0, 100.0);

和这个:

- (IBAction)buttonPressed:(UIButton *)sender
{
}

我的 .h 文件中也有这个:

@property (nonatomic, weak) IBOutlet UIButton *registerButton;

如何将 IBOutlets 和 IBActions 连接到我在代码中创建的按钮?

4

2 回答 2

2

您必须为按钮设置一个目标,如下所示:

[registerButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

于 2013-08-26T08:05:41.973 回答
0
UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            registerButton.frame=CGRectMake(150, 10, 150,25);
            registerButton.userInteractionEnabled = YES;

            [registerButton setTitle:@"Register" forState:UIControlStateNormal];

            [registerButton addTarget:self action:@selector(buttonPressed:) 
               forControlEvents:UIControlEventTouchUpInside];
            [view.contentView addSubview:registerButton];

尝试这个。我认为这对你有用。

于 2013-08-26T09:21:49.133 回答