2012-04-01 15 views
21

पर टेक्स्टफ़िल्ल्ड जोड़ना मुझे TextField को UIAlertView में जोड़ने की आवश्यकता है। मैं समझता हूं कि सेब इस दृष्टिकोण को हतोत्साहित करता है। तो क्या कोई पुस्तकालय है जिसे मैं TextField को UIAlertView में समान रूप से फ्रेम जोड़ने के लिए उपयोग कर सकता हूं? इस तरहUIAlertView

उत्तर

4

कोशिश कुछ:

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title" 
               message:@"\n\n" 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"Save", nil] autorelease]; 
CGRect rect = {12, 60, 260, 25}; 
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease]; 
dirField.backgroundColor = [UIColor whiteColor]; 
[dirField becomeFirstResponder]; 
[alert addSubview:dirField]; 

[alert show]; 
+2

मुझे लगता है कि एप्पल को हतोत्साहित रिटर्न अपने दृष्टिकोण। – shajem

3

आप कोशिश कर सकते हैं:

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here!" message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
[myTextField setBackgroundColor:[UIColor whiteColor]]; 
[myAlertView addSubview:testTextField]; 
[myAlertView show]; 
[myAlertView release]; 

विस्तार के लिए इस link का पालन करें।

+0

हां, आपका कोड काम करता है। लेकिन एप्पल निजी एपीआई का उपयोग करते हुए इस दृष्टिकोण को हतोत्साहित करता है। तो क्या कोई और तरीका है जिसे मैं हल कर सकता हूं? – shajem

+0

यहां कोई निजी एपीआई नहीं है .. –

+0

@ अनामित ओपी दस्तावेज़ों से उस वाक्य को संदर्भित करता है: इस वर्ग के लिए दृश्य पदानुक्रम निजी है और इसे संशोधित नहीं किया जाना चाहिए। –

0

इसे देखें ... http://iosdevelopertips.com/undocumented/alert-with-textfields.htmlयह निजी एपीआई है और यदि आप इसे ऐपस्टोर में ऐप के लिए उपयोग करते हैं तो इसे अस्वीकार कर दिया जा सकता है लेकिन यह उद्यम विकास के लिए ठीक है।

+0

आपके उत्तर के लिए धन्यवाद, लेकिन मैं अपने ऐप को ऐपस्टोर पर प्रकाशित करने की उम्मीद कर रहा हूं: एस – shajem

+0

मैं सलाह नहीं दूंगा कि आपको इसे लागू करना चाहिए। –

6

मैं अलर्ट व्यू और एक्शनशीट्स के लिए ऐप्पल घटकों के बजाय BlockAlertsAndActionSheets का उपयोग कर रहा हूं क्योंकि मैं ब्लॉक-दृष्टिकोण पसंद करता हूं। स्रोत में BlockTextPromptAlertView भी शामिल है, जो आप चाहते हैं कि हो सकता है। ऐप्पल-शैली को वापस पाने के लिए आप उस नियंत्रण की छवियों को प्रतिस्थापित कर सकते हैं।

Project on gitgub

Tutorial which gets you started

उदाहरण:

- (IBAction)newFolder:(id)sender { 
    id selfDelegate = self; 
    UITextField     *textField; 
    BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle :@"New Folder" 
                    message   :@"Please enter the name of the new folder!" 
                    textField  :&textField]; 
    [alert setCancelButtonWithTitle:@"Cancel" block:nil]; 
    [alert addButtonWithTitle:@"Okay" block:^{ 
     [selfDelegate createFolder:textField.text]; 
    }]; 
    [alert show]; 
} 

- (void)createFolder:(NSString*)folderName { 
    // do stuff 
} 
+0

क्या आप मुझे एक उदाहरण दिखा सकते हैं जो 'BlockTextPromptAlertView' का उपयोग करता है। मैं इस धारणा के तहत हूं कि डेवलपर्स को UIAlertView में कोई टेक्स्टफील्ड इत्यादि जोड़ने की अनुमति नहीं है। – shajem

+0

आप अपनी इंप्रेशन के साथ सही हैं। यही कारण है कि BlockAlertsAndActionSheets ** ** ** UIAlertView का उपयोग कर ** नहीं है, यह किसी भी निजी एपीआई के इस्तेमाल किए बिना पूरी तरह से स्वतंत्र कार्यान्वयन है। मेरे पास अभी मेरे स्रोतों तक पहुंच नहीं है, लेकिन कुछ घंटों में कोड नमूना के साथ मेरा जवाब अपडेट कर सकता है। –

+0

उदाहरण के उपयोग के साथ अद्यतन –

14

दुर्भाग्य से इस के लिए केवल सरकारी एपीआई iOS 5 और ऊपर, यह एक संपत्ति alertViewStyle कहा जाता है जो निम्नलिखित मानकों सेट किया जा सकता है:

UIAlertViewStyleDefault 
UIAlertViewStyleSecureTextInput 
UIAlertViewStylePlainTextInput 
UIAlertViewStyleLoginAndPasswordInput 

UIAlertViewStylePlainTextInput जो आप चाहते हैं वह हो।

ऊपर वर्णित दृश्य पदानुक्रम के साथ मेसिंग ऐप्पल द्वारा दृढ़ता से निराश है।

+0

में 'एनिमेट विथ अवधि' की खोज करने का प्रयास करें, मुझे इसे आईओएस 4 और 5 के लिए बनाना होगा। तो क्या आप जानते हैं कोई अन्य लाइब्रेरी जहां मैं उपयोग कर सकता था – shajem

69

iOS5 के लिए:

UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Please enter someth" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
av.alertViewStyle = UIAlertViewStylePlainTextInput; 
[av textFieldAtIndex:0].delegate = self; 
[av show]; 

इसके अलावा, आप 'UITextFieldDelegate, UIAlertViewDelegate प्रोटोकॉल को लागू करने की आवश्यकता होगी।

0

'Shmidt', UIAlertView में दर्ज किए गए पाठ पर कब्जा करने के कोड से जवाब देने के लिए जोड़ने नीचे चिपकाया जाता है (धन्यवाद 'वेन Hartman' Getting text from UIAlertView)

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     self.userNumber = [alertView textFieldAtIndex:0].text; 
     if (self.userNumber) { 
      // user enetered value 
      NSLog(@"self.userNumber: %@",self.userNumber); 
     } else { 
      NSLog(@"null"); 
     } 

    } 
} 
4

iOS 8 UIAlertView के रूप में के पक्ष में पदावनत कर दिया गया है UIAlertController, जो UITextField पद्धति का उपयोग करके रों जोड़ने के लिए समर्थन जोड़ता:

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler; 

एक उदाहरण के लिए this answer देखें।

1

सभी में से पहले ViewController में UIAlertViewDelegate जोड़ें।

#import <UIKit/UIKit.h> 

@interface UIViewController : UITableViewController<UIAlertViewDelegate> 

@end 

की तरह और कोड जहां आप प्रदर्शन सचेत करने के लिए चाहता है नीचे जोड़े से ज फ़ाइल,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
              message:@"Message" 
              delegate:self 
            cancelButtonTitle:@"Done" 
            otherButtonTitles:nil]; 
alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[alert show]; 

और यह के प्रतिनिधि विधि है जो UItextField

किस इनपुट
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
NSLog(@"%@", [alertView textFieldAtIndex:0].text); 
} 
संबंधित मुद्दे