2

Xcode でアプリを作成しています。ボタンを 100 回クリックしたときにアラート ビューを表示したいのですが、何らかの理由でボタンを 100 回クリックしても何も起こりません。アプリにあるすべてのコードを表示します。現時点で、私がxibで行ったことと、皆さんが助けてくれることを願っています。

#import <UIKit/UIKit.h>

int counter;

@interface ViewController : UIViewController {

    IBOutlet UILabel *count;
}

-(IBAction)plus;
-(IBAction)minus;
-(IBAction)zero;

@end

実装

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void) buttonAction {
    counter++;
    if(counter == 100)
        [self showAlert];
}


- (void) showAlert {

    UIAlertView *alert = [[UIAlertView alloc]

                          initWithTitle:@"Tile"
                          message:@"This is the message" 
                          delegate:nil 
                          cancelButtonTitle:@"Dismiss" 
                          otherButtonTitles:nil];

    [alert show];

}

-(IBAction)plus {
    counter=counter + 1;
    count.text = [NSString stringWithFormat:@"%i",counter];
}

-(IBAction)minus {
    counter=counter - 1;
    count.text = [NSString stringWithFormat:@"%i",counter];
}

-(IBAction)zero {
    counter=0;
    count.text = [NSString stringWithFormat:@"%i",counter];
}




- (void)viewDidLoad {
    counter=0;
    count.text = @"0";
        [super viewDidLoad];

}




- (void)viewDidUnload {


     [super viewDidUnload];
    }





- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


@end

それが私が持っているすべてのコードであり、私が得ている問題は、カウンターが正常に動作することです.100に達したときに何も起こりません.

ここに画像の説明を入力

ここに画像の説明を入力 そして警告なし ここに画像の説明を入力

4

2 に答える 2

0

「buttonAction」関数を使用する代わりに、プラス関数内に if ステートメントを配置します。これに沿った何か:

-(IBAction)plus {
    counter=counter + 1;
    count.text = [NSString stringWithFormat:@"%i",counter];
    if(counter == 100)
        [self showAlert];
}

いずれにせよ呼び出された場合にカウンターを増やしすぎてしまう、その余分な関数の必要性を排除します。(plus と buttonAction の両方が counter に 1 ずつ増加するよう指示します。つまり、plus が呼び出されるたびに counter が 2 増加します)

于 2012-07-06T14:22:30.970 に答える
0

「buttonAction」メソッドを呼び出していません。これを試してください:

-(IBAction)plus {
    counter=counter + 1;
    [self buttonAction];
    count.text = [NSString stringWithFormat:@"%i",counter];
}

-(IBAction)minus {
    counter=counter - 1;
    [self buttonAction];
    count.text = [NSString stringWithFormat:@"%i",counter];
}
于 2012-07-06T14:11:15.820 に答える