2012-03-17 5 views
9

मैंने अपने आवेदन में UIAlertView जोड़ा है जो उपयोगकर्ता इनपुट को पकड़ता है लेकिन मुझे यकीन है कि मैं एक तीसरा बटन कैसे जोड़ सकता हूं। आदर्श रूप से तीन बटन क्षैतिज रूप से अलर्ट में होंगे या दो "रद्द करें" बटन से ऊपर होंगे। नीचे दिए गए कोड स्निपेट का उपयोग मैं UIAlertView जोड़ने के लिए कर रहा हूं।UIAlertStylePlainTextInput शैली के साथ UIAlertView के अंदर दो से अधिक बटन कैसे प्रारूपित करते हैं?

- (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 आकार बदल सकते हैं, लेकिन एक चाल मैं एक UIActivityIndicatorView सहित के लिए उपयोग "संदेश" बनाने के लिए संदेश स्ट्रिंग में उपयोग करने के लिए है "/ n" बड़ा (प्रत्येक "/ n" प्रति एक अतिरिक्त पंक्ति), इसलिए बाकी सब कुछ के लिए पर्याप्त जगह बनाना।

2

यह निश्चित रूप से हासिल किया जा सकता है, सबसे पहले आपको इस तरह कुछ स्थापित करने की आवश्यकता होगी।

// 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]; 

से उद्धृत ... https://stackoverflow.com/a/412618/716216

तो फिर तुम आगे बढ़ UIAlertView जब कीबोर्ड को कहा जाता है ... कुछ इस तरह चेतन चाहता हूँ ...

-(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 पर अतिरिक्त जानकारी पा सकते हैं ई ऐप्पल नमूना कोड का पालन करें! https://developer.apple.com/library/ios/#samplecode/UICatalog/Introduction/Intro.html

शुभकामनाएं !!!!

+0

जानकारी के लिए धन्यवाद, अन्य बटन 3 बटन का उपयोग करते समय वास्तव में मान्य नहीं थे। मैंने कोड का परीक्षण किया लेकिन यह अभी भी कसकर पैक किया गया था। मैं सत्रों को बचाने के लिए एक अलग अलर्ट बना रहा।इस जवाब ने मुझे सामान्य रूप से 'UIAlertViews' पर बहुत उपयोगी जानकारी दी। – erran

+0

सुनना अच्छा है, अगर आपको ऐसा लगता है कि यह आपके प्रश्न का उत्तर देता है तो कृपया इसे हर किसी के संदर्भ के लिए सही के रूप में चिह्नित करें। –

1

यह कुछ हद तक मुश्किल है, यहाँ समाधान this.You संदेश स्ट्रिंग में \ N उपयोग करने के लिए UIAlertView की ऊंचाई बढ़ाने की जरूरत है के लिए है।

- (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" केवल दो बटनों के साथ काम करता है, एक बार दो से अधिक बटन, जो अलर्टव्यू के फ्रेम को नहीं बदल सकता – Paradise

2

यह मेरे लिए ठीक काम करता है। धागे के बहुत दिखा के बाद अंत में मैं देर से उपस्थित alertview के बाद समाधान

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]; 

और बटन और पाठ फ़ील्ड संरेखित पाया

-(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 के शीर्षक के लिए है।

संबंधित मुद्दे