简体   繁体   English

UIButton-addTarget不起作用

[英]UIButton - addTarget not working

Adding a button to the bottom right of the screen: 在屏幕的右下方添加一个按钮:

    UIButton *testBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [testBtn addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

    testBtn.frame = CGRectMake(screenWidth - 100, screenHeight - 100, 100, 100);

    [self.view addSubview:testBtn];

I can see the button, and tapping it turns it blue momentarily. 我可以看到该按钮,然后点击它会立即将其变为蓝色。 It just doesn't call the test: method. 它只是不调用test:方法。 Why not? 为什么不?

EDIT - here's the target method: 编辑-这是目标方法:

- (void)test:(id)sender {
    NSLog(@"hi");
}

Could it be because your selector is test: instead of just test? 可能是因为您的选择器是test:而不是test? Also, you called it lockBtn, but you used btn to reference the method call...you are referencing a different button maybe? 另外,您将其称为lockBtn,但是您使用btn来引用方法调用...您正在引用其他按钮吗?

  1. Your instance is called lockBtn but you are adding a target to btn . 您的实例称为lockBtn但您正在向btn添加目标。
  2. Your method signature may be @selector(test) and not @selector(test:) . 您的方法签名可能是@selector(test)而不是@selector(test:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM