【问题标题】:UIAlertViewController of type action sheet with dynamic list of buttonsUIAlertViewController 类型的动作表,带有动态按钮列表
【发布时间】:2015-10-13 10:54:51
【问题描述】:

由于 UIAlertViewUIActionsheet 从 iOS 8 中已弃用。因此建议不要同时使用这两个类。使用旧的操作表方法,我可以在 for 循环的帮助下动态添加选项卡。

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];
}

我可以在委托方法中使用按钮索引来执行适当的操作。这样我可以动态创建操作表。所以UIAlertController 类如何实现这一点,我问这个是因为在UIAlertController 类中我们必须添加动作处理程序及其动作块。

【问题讨论】:

  • 你是怎么做到的?
  • 请参考下面接受的答案

标签: ios ios8 uialertcontroller


【解决方案1】:

由于 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

【讨论】:

  • 我可以使用这种方式,但动作的数量是动态的,所以我可以在 for 循环中的 uiactionsheet 中添加动作,但如何将动作处理程序链接到同一循环中的那些动作。
  • 正如我所提到的,您可以使用 UIAlertAction 作为参数向处理程序调用相同的函数,并根据操作标题在函数中处理它
  • 再次感谢您的建议...我做到了。
【解决方案2】:

可以通过以下方式完成..

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];

【讨论】:

    【解决方案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/

    【讨论】:

    • 感谢您的建议,但我怀疑如何在同一 for 循环或任何其他循环中添加特定于特定操作的操作处理程序。如何映射哪个操作用于哪个操作处理程序。
    【解决方案4】:
     let arrayList = ["Agra","Banglore","delhi","Mumbai"]
            let optionMenu = UIAlertController(title: nil, message: "Choose City", preferredStyle: .actionSheet)
            for i in arrayList{
                let cityName = UIAlertAction(title: i, style: .default, handler:
                {
                    (alert: UIAlertAction!) -> Void in
    
    
    
               print(i)
    
            })
            optionMenu.addAction(cityName)
    
        }
    
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler:
        {
            (alert: UIAlertAction!) -> Void in
            print("Cancelled")
        })
        optionMenu.addAction(cancelAction)
        self.present(optionMenu, animated: true, completion: nil
    

    【讨论】:

      【解决方案5】:

      斯威夫特 4

      UIAlertController 与动态UIAlertAction

      • 为按钮标题创建一个 String 数组
      let buttonTitles = ["First", "Second", "Third"]
      
      • 关闭
      typealias completionWithValueAndIndex = (_ actionTitle :String, _ index: Int) ->()
      
      • 定义一个方法showAlertsList
      func showAlertsList(title: String, message: String, buttonsTitle:[String], completion: completionWithValueAndIndex? = nil) {
              let alert = UIAlertController(  title: title,  message:message, preferredStyle: .alert)
              let handler = { (action: UIAlertAction) -> Void in
                  let index = alert.actions.firstIndex { $0 == action}
                  completion?(action.title ?? "", index ?? 0)
              }
              for i in 0..<buttonsTitle.count {
                  alert.addAction(UIAlertAction(title: buttonsTitle[i], style: .default, handler: handler))
              }
              
              self.present(alert, animated: true, completion: nil)
       }
      
      • 使用方法
      showAlertsList(title: "Test title", message: "Test message", buttonsTitle: buttonTitles) { actionTitle, index in
           print(actionTitle)
           print(index)
      }
      

      【讨论】:

        【解决方案6】:

        简单明了,以防您确实需要索引(或 PK 或类似...) 导入 UIKit

        class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
        
            showAlertWith(msg: "when to start:", btnTitles:
                [
                    "Now",
                    "15 secs",
                    "30 secs",
                    "1 min.",
                ])
        }
        
        
        
        class TaggedAlertAction: UIAlertAction{
            var tag = 0
        }
        
        final private func showAlertWith(msg: String, btnTitles: [String]){
        
            let alert = UIAlertController(
                title: title,
                message:msg,
                preferredStyle: .alert)
        
            let handler = { (act: TaggedAlertAction) in
                let ta = act as! TaggedAlertAction
                let tag = ta.tag
        
                print(tag)
            }
        
            for i in 0..<btnTitles.count {
                let a = TaggedAlertAction(title: btnTitles[i], style: .default, handler: handler
                a.tag = i+1 // or Whatever number You need...
                alert.addAction(a)
            }
            self.present(alert, animated: true, completion: nil)
        }
        

        }

        【讨论】:

          【解决方案7】:

          您可以根据任何动态列表向 UIAlertViewControler 添加操作。使用简单的 for 循环并在每次迭代中创建 UIAction。最好根据您定义的标签设置标签值并执行操作,这是示例代码。希望能帮助到你 https://github.com/Usman4Whizpool/UIAlertController

          【讨论】:

            猜你喜欢
            • 2012-06-01
            • 2010-11-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多