0

以下のコードでエラーが発生します。毎回ボタンを押す回数 (カウンター) でメッセージを更新できないように見える理由がわからないので、頭を壁にぶつけています。ボタンを押す...

- (IBAction)countButtonTapped:(id)sender {

    self.pressCount+=1;
    //int count = (int)self.pressCount;

    //[self.tapCountButton setTitle:@"I've been tapped %i time(s)!" forState:UIControlStateNormal];
    //[self.tapCountButton setTitle:@"I've hit the main button!" forState:UIControlStateNormal];
    //NSLog(@"I've been tapped %lu time(s)!", (unsigned long)self.pressCount);
    [(UIButton *)sender setTitle:@"I've been tapped %@ time(s)!",self.pressCount forState:UIControlStateNormal];
4

2 に答える 2

2

文字列フォーマッタが必要なようです。

これを試して:

- (IBAction)countButtonTapped:(id)sender {
    UIButton * button = (UIButton *) sender; 
    self.pressCount+=1;
    NSString * titleForButton = [NSString stringWithFormat: @"I've been tapped %d time(s)!",self.pressCount]
    [button setTitle: titleForButton forState: UIControlStateNormal];
}

ここでのもう 1 つの違いは、"self.pressCount" は NSInteger である可能性が高いため、NSString オブジェクトを示すための " " では%dなく " " を format 引数として使用することです。%@

于 2013-09-16T02:15:34.707 に答える
0

ストーリー ボードで、UIButton がタッチダウン時にのみ IBAction を呼び出すことを確認してください。おそらく、他の UIButton 状態で IBAction を呼び出しています。

これは私がそれを行う方法です

    @implementation v1ViewController

    ...
    static int counter = 0;


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

    //initialize
    counter = 0
    }

    - (IBAction)countButtonTapped:(id)sender {

        counter++;

    NSLog("counter: %d ...", counter);

UIButton *button = (UIButton *) sender;
        [button setTitle:@"I've been tapped %d time(s)!",counter forState:UIControlStateNormal];

    }
于 2013-09-16T02:40:28.280 に答える