2015-10-13 46 views
4

因爲UIAlertViewUIActionsheet已從iOS8中棄用。所以建議不要同時使用這兩個類。使用舊的動作表方法,我可以添加動態標籤for循環的幫助。UIAlertViewController類型的動作表與動態按鈕列表

UIActionSheet *actionSheet = [[UIActionSheet alloc]init]; 
actionSheet.title = @"Departure City"; 
actionSheet.delegate = self; 
actionSheet.tag = 1; 

for (int j =0 ; j<arrayDepartureList.count; j++) 
{ 
    NSString *titleString = arrayDepartureList[j]; 
    [actionSheet addButtonWithTitle:titleString]; 
} 

我可以使用按鈕索引在委託方法進行適當的action.This方法,我可以動態創建動作片。所以UIAlertController課程是如何實現這一點的,我在問這個問題,因爲在UIAlertController類中我們必須添加動作處理程序和動作塊。

+0

你是怎麼做到這一點的? – Madhu

+0

請參閱下面的接受的答案 – Vaibhav

回答

3

就像你在這裏做的一樣。

for (int j =0 ; j<arrayDepartureList.count; j++) 
{ 
    NSString *titleString = arrayDepartureList[j]; 
    UIAlertAction * action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:nil]; 

    [alertController addAction:action]; 
} 

如果您需要每個動作的自定義動作,您可以在處理程序部分定義它們。

如需進一步信息,看看這裏: http://hayageek.com/uialertcontroller-example-ios/

+0

感謝您的建議,但我有一個疑問,如何可以添加特定於特定操作的動作處理程序在相同的循環或任何other.How映射哪個動作是哪個動作處理程序。 – Vaibhav

+1

我想通了..再次感謝.. – Vaibhav

+0

你會把它標記爲接受的答案? – dirtydanee

7

由於UIActionSheet在iOS 8的已經過時,必須使用UIAlertViewController和添加的每一個動作,因爲這例子:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My Alert" 
     message:@"This is an action sheet." 
     preferredStyle:UIAlertControllerStyleActionSheet]; 
UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"one" 
     style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { 
      NSLog(@"You pressed button one"); 
     }]; 
UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"two" 
     style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { 
      NSLog(@"You pressed button two"); 
     }]; 

[alert addAction:firstAction]; 
[alert addAction:secondAction]; 

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

根據你的榜樣您可以在for循環中添加UIAlertAction,並且操作會導致處理選擇的某個函數。

另一種解決方案,我更喜歡: 使用ActionSheetPicker,選擇從選擇器,而不是爲每個城市一個按鈕,您的出發城市,你可以在這裏找到: https://github.com/skywinder/ActionSheetPicker-3.0

+0

我可以使用這種方式,但動作的數量是動態的,所以我可以在循環中的uiactionsheet中添加動作,但是如何將動作處理程序與同一循環中的動作鏈接起來。 – Vaibhav

+0

正如我所提到的,您可以使用UIAlertAction作爲參數調用與處理程序相同的函數,並根據操作標題 –

+0

在函數中處理它。再次感謝您的建議......我做到了。 – Vaibhav

3

它可以通過以下方式進行。 。

UIAlertController *departureActnSht = [UIAlertController alertControllerWithTitle:@"Departure City" 
                      message:@"Select your choice" 
                    preferredStyle:UIAlertControllerStyleActionSheet]; 

for (int j =0 ; j<arrayDepartureList.count; j++) 
{ 
    NSString *titleString = arrayDepartureList[j]; 
    UIAlertAction *action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 

     self.txtDepartureCity.text = arrayDepartureList[j]; 
    }]; 

    [departureActnSht addAction:action]; 
} 

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){ 

    [self dismissViewControllerAnimated:departureActnSht completion:nil]; 
}]; 

[departureActnSht addAction:cancelAction]; 
[self presentViewController:departureActnSht animated:YES completion:nil]; 
-1

您可以根據任何動態列表向UIAlertViewControler添加操作。使用簡單的for循環並在每次迭代中創建UIAction。更好地設置標籤值並根據您的定義標籤執行操作,下面是示例代碼。希望它有幫助 https://github.com/Usman4Whizpool/UIAlertController