2015-05-20 69 views
2

enter image description hereiOS的模式疊加菜單

我在想,如果有一個內置的視圖XCode中顯示彈出菜單這樣的iOS應用程序?除了只有一組垂直堆疊的按鈕之外,它就像一個警報?

編輯:

我知道UIAlertController,只是沒有那麼它的按鈕垂直堆疊新增超過2之後,這是我要的風格。只是爲了清除,只是按鈕設置標題和消息爲零。

+0

如果答案的人幫助你,你應該接受它標記了一個問題,回答:) –

回答

2

是的。這就是所謂的一個UIAlertController

+0

好吧,我是啞巴。當你添加兩個以上的動作時,沒有意識到動作(按鈕)垂直疊加。 –

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

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

@Richa解決方案,但在迅疾版本

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)