在屏幕右下角添加一个按钮:
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];
我可以看到该按钮,点击它会立即将其变为蓝色。它只是不调用 test: 方法。为什么不?
编辑 - 这是目标方法:
- (void)test:(id)sender {
NSLog(@"hi");
}