0

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];
4

4 に答える 4

3

これを使って:

[hotelButton addTarget:self action:@selector(buttonClicked) 
    forControlEvents:UIControlEventTouchUpInside];

Apple ドキュメントから:

UIControlEventTouchUpInside
A touch-up event in the control where the finger is inside the bounds of the control.

UIControlEventTouchUpOutside
A touch-up event in the control where the finger is outside the bounds of the control

そして、この方法を維持してください:

- (void) buttonClicked
{
    NSLog(@"Button %i clicked", hotelButton.tag);
}
于 2013-10-31T09:03:55.177 に答える
1

外側から内側に変えてみてください。

[hotelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
于 2013-10-31T09:06:31.290 に答える
1

これを試して、

[hotelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

そして差出人を渡し、

- (void) buttonClicked:(UIButton*)sender
{
    NSLog(@"Button %i clicked", [sender tag]);
}
于 2013-10-31T09:05:11.283 に答える