iOS 模态叠加菜单

iOS modal overlay menu

我想知道 XCode 中是否有一个 built-in 视图来为 iOS 应用程序显示这样的 pop-up 菜单?类似于警报,只是只有一组 vertically-stacked 个按钮?

编辑:

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

是的。它被称为 UIAlertController

您可以使用 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];

是的,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];

@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)