2015-10-15 77 views
0

我必須在iOS8之前在一個viewController中顯示多個UIAlerts,我們可以將UIAlerts與標籤一起使用,並且我們可以在clickedButtonAtIndex中使用類似標籤來識別。如何在iOS中處理多個UIAlertControllers?

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if(alertView.tag == 1) 
{ 
//UIAlert1 button clicked 
} 

if(alertView.tag == 2) 
{ 
//UIAlert2 button clicked 
} 
} 

所以我們可以做的東西。 如何識別不同UIAlertControllers的按鈕點擊。 因爲一個alert1按鈕單擊我必須更改一些文本顏色和alert2按鈕單擊我必須彈出視圖控制器。

+0

這是一個很好的問題 – Jamil

+0

我曾經遇到過一次。 – Jamil

+0

請在這裏查看我的答案:http://stackoverflow.com/a/38875376/3412051 –

回答

0

您可以採用以下方法。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(alertView.tag == 1) 
    { 
     //UIAlert1 button clicked 
     if(buttonIndex==0){//say, **Cancel** button tag 
      //alert2 "Cancel" button has tapped 
      }else if(buttonIndex==1){say, **OK** button tag 
      //alert2 "OK" button has tapped 
      } 
    } 

    if(alertView.tag == 2) 
    { 
     //UIAlert2 button clicked 
     if(buttonIndex==0){//say, **Yes** button tag 
      //alert2 "YES" button has tapped 
      } 
    } 
} 
+0

此代理方法適用於UIAlertView,但不適用於UIAlertController,UIAlertController已從UIAlertview和UIActionSheet中接管iOS8 –

+0

yes ,它是UIAlertView代理方法 – Jamil

+0

@ jamil65able從iOS8開始,我們不能使用UIAlertView。 – lazyCoder

1

UIAlertController是基於塊的。

爲每個動作創建一個UIAlertAction實例,並在按鈕被點擊後傳遞要執行的塊。

欲瞭解更多信息,請閱讀Mattt Thompson的UIAlertController "review"

+0

是的,我可以通過塊來執行按鈕操作。就像我有兩個警報,如取消和完成按鈕,取消和保存。當用戶點擊完成我必須做一個功能,當用戶點擊保存我必須做其他功能。 – lazyCoder