Xcode 5 ボタンの問題があります。
プログラムで多数のボタンを作成し、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]);
}
しかし、 action@selector を指しているときに「宣言されていないセレクターbuttonClicked
」というエラーが発生しました):viewDidLoad
[hotelButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];