2012-03-17 5 views
9

ユーザー入力を取得するアプリケーションにUIAlertViewを追加しましたが、3番目のボタンを追加する方法は不明です。理想的には、3つのボタンは警告の横方向に横になり、2つのボタンは「キャンセル」ボタンの上にあります。以下のコードスニペットは、UIAlertViewを追加するために使用しています。UIAlertStylePlainTextInputスタイルでUIAlertViewの内部に2つ以上のボタンをどのようにフォーマットしますか?

- (IBAction)initiateSave{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Archive" 
              message:@"Enter a name to save this as:" 
              delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"Save Session",@"Save",nil]; 
    alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
    UITextField * alertTextField = [alert textFieldAtIndex:0]; 
    alertTextField.keyboardType = UIKeyboardTypeDefault; 
    alertTextField.placeholder = @"eg. My awesome file..."; 
    alert.tag = 1; 
    [alert show]; 
    [alert release]; 
    self.name = [[alert textFieldAtIndex:0]text]; 
} 

enter image description here

答えて

2

アップルは本当にあなたがUIAlertViewを使いこなすことを望んでいません。自然にフォーマットする方法がニーズを満たしていない場合は、代わりにカスタム表示(「モーダル」)表示を行うことを検討してください。

+0

私の答えは唯一可能な正解ではありませんが、それでも正しい答えです。あなたは本当に質問者がしたいことをするつもりはありません。 – matt

1

私はあなたが手動でUIAlertViewのサイズを変更することができるとは思わないが、私はUIActivityIndi​​catorViewを含むために使用するトリックは、「メッセージ」を作るためにメッセージ文字列に「/ n」を使用することですより大きい(各 "/ n"に1行追加する)ので、他のすべてのために十分なスペースを確保します。

2

これは間違いなく達成できます。まず、このような設定が必要です。あなたはこのようなキーボードが呼び出されUIAlertViewアップ...何かを動かすアニメーション化したいと思う その後https://stackoverflow.com/a/412618/716216

から引用

// Create Alert 
UIAlertView* av = [UIAlertView new]; 
av.title = @"Find"; 
// Add Buttons 
[av addButtonWithTitle:@"Cancel"]; 
[av addButtonWithTitle:@"Find & Bring"]; 
[av addButtonWithTitle:@"Find & Go"]; 
[av addButtonWithTitle:@"Go to Next"]; 
// Make Space for Text View 
av.message = @"\n"; 
// Have Alert View create its view heirarchy, set its frame and begin bounce animation 
[av show]; 
// Adjust the frame 
CGRect frame = av.frame; 
frame.origin.y -= 100.0f; 
av.frame = frame; 
// Add Text Field 
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; 
text.borderStyle = UITextBorderStyleRoundedRect; 
[av addSubview:text]; 
[text becomeFirstResponder]; 

... ...

-(void)keyboardWillShow: (id) notification { 

if(showAlert==YES) 
{ 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [createNewAlert setTransform:CGAffineTransformMakeTranslation(0,-60)]; 
    [createNewAlert show]; 
    [UIView commitAnimations]; 
} 
} 

-(void)keyboardWillHide: (id) notification { 

if(showAlert==YES) 
{ 


     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 
     [createNewAlert setTransform:CGAffineTransformMakeTranslation(0,+60)]; 

     [UIView commitAnimations]; 

} 
} 

お見積もりからhttps://stackoverflow.com/a/3844956/716216

もちろん、カスタムUIAlertViewsの追加情報はth e Appleのサンプルコードに従ってください! https://developer.apple.com/library/ios/#samplecode/UICatalog/Introduction/Intro.html

Good Luck !!!!

+0

ありがとうございました。他の回答は、3つのボタンを使用しているときは本当に有効ではありませんでした。私はコードをテストしましたが、それはまだきつく詰まっていました。私はセッションを保存するための別の警告を作成しました。この答えは、一般的な 'UIAlertViews'に関する多くの有益な情報につながりました。 – erran

+0

あなたの質問に答えたような気がするなら、他の人の参考にしてください。 –

1

これはややトリッキーで、ここではthis.YouためのソリューションですUIAlertViewの高さを高くするメッセージ文字列に\ n個を使用する必要があります。

- (IBAction)initiateSave{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Archive" 
              message:@"\n\n\n\n\n" // Trick to increase the height 
              delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"Save Session",@"Save",nil]; 
    alert.alertViewStyle = UIAlertViewStylePlainTextInput; 

After increasing the height of Alert, now you can add a label to set the message in the label : "Enter a name to save this as:" at appropriate position. 

[alert addSubView: messageLbl]; 
    UITextField * alertTextField = [alert textFieldAtIndex:0]; 
    alertTextField.keyboardType = UIKeyboardTypeDefault; 
    alertTextField.placeholder = @"eg. My awesome file..."; 
    alert.tag = 1; 
    [alert show]; 
    [alert release]; 
    self.name = [[alert textFieldAtIndex:0]text]; 
} 
+1

こんにちは、 "\ n"は、アラートビューのフレームを変更できない2つのボタン、2つ以上のボタンでのみ動作します – Paradise

2

私にとってはうまくいきます。最終的にはスレッドの多くを示した後、私は

UIAlertView* getWebUrl = [[UIAlertView alloc] initWithTitle:@"Enter URL" 
               message:nil 
               delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:@"Save", 
                   @"Save Url", nil]; 
getWebUrl.transform=CGAffineTransformMakeScale(1.0, 0.75); 
getWebUrl.alertViewStyle=UIAlertViewStylePlainTextInput; 
[getWebUrl show]; 

ソリューション

を発見し、少し遅れて、ここで存在alertview後

-(void)willPresentAlertView:(UIAlertView *)alertView { 
    for (UIView *view in alertView.subviews) { 
     if ([view isKindOfClass:[UITextField class]]|| 
      [view isKindOfClass:[UIButton class]] || view.frame.size.height==31) { 
      CGRect rect=view.frame; 
      rect.origin.y += 65; 
      view.frame = rect; 
     } 
    } 
} 
0

をボタンやテキストフィールドを揃えるが、トリックを行う必要があります。 (私のために働く)。 はUIAlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TITLE" 
              message:@"BODY" 
              delegate:self 
            cancelButtonTitle:@"CANCEL" 
            otherButtonTitles:@"OK",@"SEARCH",nil]; 

    alert.tag = kAlertTag; 
    alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
    UITextField *alertTextField = [alert textFieldAtIndex:0]; 
    alertTextField.keyboardType = UIKeyboardTypeDefault; 
    alertTextField.placeholder = @"ENTER STH"; 
    [alert show]; 
    [alert release]; 

を作成し、デリゲートメソッドを実装します。

-(void)willPresentAlertView:(UIAlertView *)alertView { 
    if (alertView.tag == kAlertTag) { 
     [alertView setFrame:CGRectMake(17, 30, 286, 188)]; 
     NSArray *subviewArray = [alertView subviews]; 
     UILabel *messageLB = (UILabel *)[subviewArray objectAtIndex:2]; 
     [messageLB setFrame:CGRectMake(10, 46, 260, 20)]; 
     UIButton *cancelBT = (UIButton *)[subviewArray objectAtIndex:3]; 
     [cancelBT setFrame:CGRectMake(10, 130, 100, 42)]; 
     UIButton *okBT = (UIButton *)[subviewArray objectAtIndex:4]; 
     [okBT setFrame:CGRectMake(194, 130, 80, 42)]; 
     UIButton *searchBT = (UIButton *)[subviewArray objectAtIndex:5]; 
     [searchBT setFrame:CGRectMake(112, 130, 80, 42)]; 
     UITextField *plateTF = (UITextField *)[subviewArray objectAtIndex:6]; 
     [plateTF setFrame:CGRectMake(10, 80, 266, 50)]; 
     UITextField *placeTF = (UITextField *)[subviewArray objectAtIndex:7]; 
     [placeTF setFrame:CGRectMake(15, 70, 256, 50)]; 
    } 
} 

FYI [subviewArray objectAtIndex:1]はalertviewのタイトルです。

関連する問題