OC中UIAlertController和UIAlertView用法

//iOS8以上抛弃了UIAlertView 使用UIAlertController代替

        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"总分:%d",_count*100] preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"再玩一次" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            _count = 0;

            [self addTimer];

            _second = 0;

            _countLabel.text = @"总分:0";

        }];

        [alertController addAction:cancelAction];

        [alertController addAction:okAction];

        [self presentViewController:alertController animated:YES completion:nil];




//UIAlertView iOS8以下的用法

        UIAlertView * alterView = [[UIAlertView alloc]initWithTitle:@"游戏结束" message:[NSString stringWithFormat:@"总分:%d",_count*100] delegate:self cancelButtonTitle:@"再玩一次" otherButtonTitles:@"确定", nil];

         [alterView show];


//UIAlertView还需要添加代理方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 0) {

        _count = 0;

        [self addTimer];

        _second = 0;

        _countLabel.text = @"总分:0";

    }else{

        

    }

}

你可能感兴趣的:(iOS笔记)