showAlert方法中未声明的标识符

时间:2011-11-28 21:42:37

标签: ios xcode

我的showAlert方法中出现了很多错误,对我来说没有意义。已经创建了我的标题局部变量,但仍然得到未声明的标识符错误消息。在代码之后注释掉了错误消息。

你能帮忙吗?

以下是BullseyeViewController.m文件中的代码:

- (IBAction)showAlert
{
    int difference = abs(targetValue - currentValue);
    int points =  100 - difference;
    score += points;

    NSString *title;
    if (difference == 0) {
        title = @"Perfect!";
        points += 100;
    } else if (difference < 5) {
        if (difference == 1) {
            points += 50;
        }
        title = @"You almost had it!";
    } else if (difference < 10) {
        title = @"Pretty good!";
    } else {
       title = @"Not even close...";
    }


    NSString *message = [NSString stringWithFormat:@"You scored %d points", points];  //    unused variable 'message'
    }


    UIAlertView *alertView = [[UIAlertView alloc]
                          initWithTitle:title // use of undeclared identifier 'title'
                          message:message
                          delegate:self 
                          cancelButtonTitle:@"OK" // Expected ';' after top level    declarator 2 
                          otherButtonTitles: nil];

    [alertView show]; // Missing '[' at start of message send expression


} // Expected external declaration

1 个答案:

答案 0 :(得分:1)

在消息声明后,你有一个大括号。只是删除它。 这就是变量标题超出范围的原因。

- (IBAction)showAlert
{
    ...

    NSString *message = [NSString stringWithFormat:@"You scored %d points", points];  //    unused variable 'message'
    // REMOVE THIS => }


    UIAlertView *alertView = [[UIAlertView alloc]
                          initWithTitle:title // use of undeclared identifier 'title'
                          message:message
                          delegate:self 
                          cancelButtonTitle:@"OK" // Expected ';' after top level    declarator 2 
                          otherButtonTitles: nil];

    [alertView show]; // Missing '[' at start of message send expression


} // Expected external declaration