iOS模态叠加菜单

时间:2015-05-20 17:34:02

标签: ios swift menu modal-dialog alert

enter image description here

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

编辑:

我知道UIAlertController,只是在你添加2个以上后它的按钮垂直堆叠,这就是我想要的风格。只是为了清除,因为按钮只将标题和消息设置为nil。

4 个答案:

答案 0 :(得分:2)

是。它被称为UIAlertController

答案 1 :(得分: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];

答案 2 :(得分:2)

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

答案 3 :(得分:1)

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