多个警报按钮出错并且无效

时间:2012-08-20 20:06:32

标签: xcode timer alert void

-(void)checkCollision{
if (CGRectIntersectsRect(player.frame, enemy.frame)) {

    [randomMain invalidate];
    [start setHidden:NO];

    CGRect frame = [player frame];
    frame.origin.x = 137.0f;
    frame.origin.y = 326.0;
    [player setFrame:frame];

    CGRect frame2 = [enemy frame];
    frame2.origin.x = 137.0f;
    frame2.origin.y = 20.0;
    [enemy setFrame:frame2];


    [randomMain invalidate];
    [start setHidden:NO];

    [timer invalidate];
    time.text = @"0";

你好,我面临的问题是我尝试在下面制作一个警报视图但是我得到一个错误说明如下:无效的参数类型“void”到一元表达式。在线阅读: - (void)alert1:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)ButtonIndex { 干杯

    time.text = @"0"; 
    [timer invalidate];




          NSString *timemessage = [NSString stringWithFormat:@"Unlucky! You survived for %i seconds!", timenumber];
              UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"You've Been Caught!" message:timemessage delegate:nil cancelButtonTitle:@"Try Again" otherButtonTitles:@"Show Leaderboard", @"Submit To game Center",nil];
    [alert1 show];




              -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)ButtonIndex {
        if (ButtonIndex == 1) {
        GKScore *myScore = [[GKScore alloc]
                            initWithCategory:@"*****"];
            myScore.value = timenumber;



        }
        }










                }}
            ;

1 个答案:

答案 0 :(得分:0)

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)ButtonIndex {是新方法的开头。它不能包含在checkCollision内。您需要移动它及其所有代码。如果您愿意,可以在checkCollision结束后立即使用。您还需要将UIAlertView的委托设置为要接收alertView:clickedButtonAtIndex:回调的对象。通常是self。像这样:

-(void)checkCollision {
    // Blah, blah, ....
    NSString *timemessage = [NSString stringWithFormat:@"Unlucky! You survived for %i seconds!", timenumber];
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"You've Been Caught!" message:timemessage delegate:self cancelButtonTitle:@"Try Again" otherButtonTitles:@"Show Leaderboard", @"Submit To game Center",nil];
    [alert1 show];
}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)ButtonIndex {
    if (ButtonIndex == 1) {
        GKScore *myScore = [[GKScore alloc] initWithCategory:@"*****"];
        myScore.value = timenumber;
    }
    // Handle other button choices ....
}