2016-01-24 122 views
0

我得到一個UIAlert返回一個布爾值時遇到了困難,我不能在網上找到任何東西,所以我希望你們可以幫忙。首先,這是app的基本設置。基本上,當用戶點擊「點擊」按鈕時,會彈出一個警告,選擇「是」和「否」。這裏是我已經設置的按鈕到目前爲止的代碼:從UIAlertAction返回一個布爾值

UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Choose" 
                   message:@"Answer the question" preferredStyle:UIAlertControllerStyleAlert]; 
UIAlertAction *defaultAction= [UIAlertAction actionWithTitle:@"YES" 
                 style:UIAlertActionStyleDefault 
                handler:nil]; 
UIAlertAction *noAction= [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:nil]; 
[alert addAction:defaultAction]; 
[alert addAction:noAction]; 
[self presentViewController:alert animated:YES completion:nil]; 

我該如何精確地使用完成選項來返回一個布爾?

+0

你不能找到任何東西嗎?真?以下話題如何? http://stackoverflow.com/questions/28831468/x-code-obj-c-retrieve-user-inputed-text-from-alert-box-ios8我可以找到更多。 –

+0

您不能從操作處理程序返回任何內容,但可以設置控制器關閉後檢查的變量。 – Avi

回答

0

在UIAlertAction本身中使用完成處理程序。例如,

UIAlertAction *noAction= [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) 
           { 
            NSLog(@"NO"); 
            [alert dismissViewControllerAnimated:YES completion:nil]; 

           }];