我正在尝试创建一个按钮,一旦点击它就会显示另一个UIView的弹出窗口。为了进行测试,我的viewDidLoad部分中包含以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.hard1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.hard1 setFrame:CGRectMake(884, 524, 105, 60)]; // set the x,y,width and height based on your specs
    UIImage *buttonImage = [UIImage imageNamed:@"green.jpg"];
    hard1.layer.cornerRadius = 10;
    hard1.clipsToBounds = YES;
    [hard1 addTarget: self
              action: @selector(buttonClicked:)
    forControlEvents: UIControlEventTouchUpInside];
    [self.hard1 setImage:buttonImage forState:UIControlStateNormal];
    [self.view addSubview:self.hard1];
}

再往下走:
- (IBAction) buttonClicked: (id)sender
{
    NSLog(@"Tap");
}

但是,当我按下按钮时,控制台不会记录“Tap”。有任何想法吗?

最佳答案

观看以下三行代码:

hard1.layer.cornerRadius = 10;
hard1.clipsToBounds = YES;
[hard1 addTarget: self
          action: @selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];

你想念自己。在这三个方面。他们应该是:
self.hard1.layer.cornerRadius = 10;
self.hard1.clipsToBounds = YES;
[self.hard1 addTarget: self
          action: @selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];

同样,如果您以编程方式创建它,则它不应是IBAction(IB代表界面构建器,并且它不是在界面构建器中创建的)。

09-25 16:25