按钮检查正确的答案目标-c

时间:2013-02-04 18:20:32

标签: objective-c ipad nsstring uibutton nsarray

我有一个UIViewController,里面我有NSArray,我把我的正确答案,我也有3个按钮,3个NSArray为他们的标题,我想检查我的按钮标题是否等于正确答案然后去正确的页面会转到错误的页面

请你帮我解决这个问题:

提前致谢!

这是我的代码:

NSString *correctOne = @"test1";
NSString *correctTwo = @"test2";
NSString *correctThree = @"test3";
NSString *correctFour = @"test4";
NSString *correctFive = @"test5";
NSString *correctSix = @"test6";
NSString *correctSeven = @"test7";
NSString *correctEight = @"test8";



correctComments = [[NSArray alloc] initWithObjects: correctOne, correctTwo, correctThree, 
correctFour, correctFive, correctSix, correctSeven, correctEight, nil];

int rand = arc4random()%8;
  NSString *correct = [correctComments objectAtIndex:rand];
[test setTitle:(firstAnswer) forState:UIControlStateNormal];
[test setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[test setTag:0];

[ansONE setTitle:(secondAnswer) forState:UIControlStateNormal];
[ansONE setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[ansONE setTag:1];

[ansTWO setTitle:(threeAnswer) forState:UIControlStateNormal];
[ansTWO setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[ansTWO setTag:2];

 - (IBAction)suivant:(id)sender {

//我的问题是如何检查我的按钮标题和correctComments

 }

2 个答案:

答案 0 :(得分:0)

- (IBAction)suivant:(id)sender {
    UIButton *button = (UIButton *)sender;
    int correctIndex = ...;

    BOOL thisIsTheCorrectButton = (button.tag == correctIndex);
    // Do whatever
}

答案 1 :(得分:0)

首先,您必须将正确答案从您的代码段添加到属性中:

self.correctAnswer = correct;

然后执行这些字符串的比较

- (IBAction)suivant:(UIButton *)sender {
    // You wanted to check for title:
    if ([sender.currentTitle isEqualToString:self.correctAnswer]) {
        // Correct...
    }
    else {
        // Wrong...
    }
}