2016-06-22 20 views
1

コードごとに、すべての可能性を試しましたが、シーケンスを変更できないように、アラートアクションボタンのシーケンスを変更したいと思います。 HIG 1としてUIalertcontrollerボタンのシーケンス

、キャンセルは右側に https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Alerts.html#//apple_ref/doc/uid/TP40006556-CH14-SW1

である。この結果、以下の私を与えるだろう、ここで

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"My alert message" preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction* noButton = [UIAlertAction 
           actionWithTitle:@"Cancel" 
           style:UIAlertActionStyleCancel 
           handler:^(UIAlertAction * action) 
           { 
            [alertController dismissViewControllerAnimated:YES completion:nil]; 
           }]; 

UIAlertAction* yesButton = [UIAlertAction 
           actionWithTitle:@"OK" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) 
           { 

            [alertController dismissViewControllerAnimated:YES completion:nil]; 

           }]; 


[alertController addAction:yesButton]; 
[alertController addAction:noButton]; 
[self presentViewController:alertController animated:YES completion:nil]; 

を自分のコードを参照してください。私は右手でキャンセルbuttonを変更し、左側でOK buttonを変更したいと思います。

enter image description here

私はあなたの時間を感謝しています。

+0

キャンセルボタンは、他のオプションが破壊的なアクションである場合にのみ、そのアクションに 'UIAlertActionStyleDestructive'を使用する必要があります。それを左に置くことは、右側が 'UIAlertActionStyleDefault'のときの正しい振る舞いです。 – dan

答えて

9

変更代わりにUIAlertActionStyleCancel

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"My alert message" preferredStyle:UIAlertControllerStyleAlert]; 


UIAlertAction* yesButton = [UIAlertAction 
          actionWithTitle:@"OK" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 

           [alertController dismissViewControllerAnimated:YES completion:nil]; 

          }]; 

UIAlertAction* noButton = [UIAlertAction 
          actionWithTitle:@"Cancel" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 
           [alertController dismissViewControllerAnimated:YES completion:nil]; 
          }]; 

[alertController addAction:yesButton]; 
[alertController addAction:noButton]; 


[self presentViewController:alertController animated:YES completion:nil]; 

UIAlertActionStyleDefaultにボタンタイプを取り消すそして今、あなたは、単に

[alertController addAction:yesButton]; //If you yesButton to appear first (left) 
[alertController addAction:noButton]; 

UIAlertActionsの位置をしている変更することにより、ボタンの順序を変更することができますためのアクションスタイル並べ替えるのに十分な柔軟性。彼らは修正されていません。

+0

私は両方の方法を試しましたが、うまくいきませんでした。ありがとう – Manoj

+0

今すぐお試しください! @Manoj –

+0

その作品、ありがとうが、私はなぜスタイルが(UIAlertActionStyleDefault)両方のアラートアクションのために同じであるのだろうか?私はUIAlertActionStyleDefaultと2番目のUIAlertActionStyleCance lを試していました! – Manoj

2

人は少し古いスレッドですが、右のボタンに太字のフォントを表示するには、そのアクションをアラートオブジェクトの '.preferredAction'プロパティとして設定する必要があります。これが助けてくれることを願って。私はちょうど3/iOS10の速さでそれを証明し、うまく動作します。

関連する問題