【问题标题】:iOS modal overlay menuiOS 模态叠加菜单
【发布时间】:2015-05-20 17:34:02
【问题描述】:

我想知道 XCode 中是否有内置视图可以为 iOS 应用程序显示像这样的弹出菜单?类似于警报的东西,除了只有一组垂直堆叠的按钮?

编辑:

我知道 UIAlertController,只是它的按钮在添加超过 2 个后垂直堆叠,这是我想要的样式。只是为了清除,对于将标题和消息设置为 nil 的按钮。

【问题讨论】:

  • 如果其中一个答案对您有所帮助,您应该接受它以将问题标记为已回答:)

标签: ios swift menu modal-dialog alert


【解决方案1】:

是的。它被称为UIAlertController

【讨论】:

    【解决方案2】:

    你可以使用 UIAlertController

    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:@""
                                          message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *searchAction = [UIAlertAction
                               actionWithTitle:NSLocalizedString(@"Search for an image", @"search action")
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action)
                               {
                                   //Add your code                                 
                               }];
    
    UIAlertAction *choosePhotoAction = [UIAlertAction
                                actionWithTitle:NSLocalizedString(@"Choose Photo", @"choosePhoto action")
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction *action)
                                {
                                   //Your code
                                }];
    
    UIAlertAction *takePhotoAction = [UIAlertAction
                                actionWithTitle:NSLocalizedString(@"Take Photo", @"takePhoto action")
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction *action)
                                {
                                   //Your code
                                }];
    
    UIAlertAction *cancelAction = [UIAlertAction
                                      actionWithTitle:NSLocalizedString(@"Cancel", @"cancel action")
                                      style:UIAlertActionStyleDefault
                                      handler:^(UIAlertAction *action)
                                      {
                                          NSLog(@"cancel action");
    
                                      }];
    
    [alertController addAction:searchAction];
    [alertController addAction:choosePhotoAction];
    [alertController addAction:takePhotoAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
    

    【讨论】:

      【解决方案3】:

      是的,UIAlertController 用于设置警报视图上的其他控件。

      UIAlertController * alert=   [UIAlertController
                                    alertControllerWithTitle:@"My Title"
                                    message:@"Enter User Credentials"
                                    preferredStyle:UIAlertControllerStyleAlert];
      
      UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                 handler:^(UIAlertAction * action) {
                                                     //Do Some action here
      
                                                 }];
      UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {
                                                         [alert dismissViewControllerAnimated:YES completion:nil];
                                                     }];
      
      [alert addAction:ok];
      [alert addAction:cancel];
      
      [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
          textField.placeholder = @"Username";
      }];
      [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
          textField.placeholder = @"Password";
          textField.secureTextEntry = YES;
      }];
      
      [self presentViewController:alert animated:YES completion:nil];
      

      【讨论】:

        【解决方案4】:

        @Richa 解决方案,但在 swift 版本中

        let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
            let searchAction = UIAlertAction(title: NSLocalizedString("Search for an image", comment: "search action"), style: .default, handler: {(action: UIAlertAction) -> Void in
                //Add your code
            })
            let choosePhotoAction = UIAlertAction(title: NSLocalizedString("Choose Photo", comment: "choosePhoto action"), style: .default, handler: {(action: UIAlertAction) -> Void in
                //Your code
            })
            let takePhotoAction = UIAlertAction(title: NSLocalizedString("Take Photo", comment: "takePhoto action"), style: .default, handler: {(action: UIAlertAction) -> Void in
                //Your code
            })
            let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: "cancel action"), style: .default, handler: {(action: UIAlertAction) -> Void in
                print("cancel action")
            })
            alertController.addAction(searchAction)
            alertController.addAction(choosePhotoAction)
            alertController.addAction(takePhotoAction)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-09
          • 1970-01-01
          • 2021-08-19
          • 1970-01-01
          • 2015-01-31
          • 1970-01-01
          • 2012-01-14
          相关资源
          最近更新 更多