我有一個Xcode 5按鈕問題。Button click not registereding
我在UIViewController
上編程式地創建了一些按鈕,然後想要註冊點擊這些按鈕。我使用NSLog
來跟蹤點擊次數,但似乎沒有在日誌中註冊點擊次數。
任何人都可以幫忙嗎?
最終目的是在每個按鈕點擊後繼續到下一個UIViewController
,但只是測試以查看按鈕點擊是否先註冊。
這裏是我如何創建viewDidLoad
(僅顯示1)按鈕。 注意:if-statement
僅用於檢查在先前視圖中按下哪些按鈕。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *myString = [NSString stringWithFormat:@"%d", self.tagNumber ];
if (self.tagNumber == 1)
{
hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
hotelButton.tag = 1;
hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);
UIImage *buttonImage1 = [UIImage imageNamed:@"buttonImage1.png"];
[hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[self.view addSubview:hotelButton];
// Add targets and actions
[hotelButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
}
}
這裏是我如何檢查按鈕點擊
- (void) buttonClicked
{
NSLog(@"Button %i clicked", hotelButton.tag);
}
注:我也試過如下:
- (void) buttonClicked:(UIButton *) button
{
NSLog(@"Button %ld clicked", (long int)[button tag]);
}
但是,這給了一個錯誤「未申報選擇器buttonClicked
「在我的viewDidLoad
指向action @選擇器):
[hotelButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
好斑,完全錯過了。必須打錯輸入。謝謝。 – heyred