Adding TextField to UIAlertView

前端 未结 9 1354
不知归路
不知归路 2021-02-12 12:38

I need to add a TextField to an UIAlertView. I understand that apple discourage this approach. So is there any library that i could make use of to add

相关标签:
9条回答
  • 2021-02-12 12:56

    Try something like this:

    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];
    
    0 讨论(0)
  • 2021-02-12 12:58

    Unfortunately the only official API for this is iOS 5 and up, it's a property called alertViewStyle which can be set to the following parameters:

    UIAlertViewStyleDefault
    UIAlertViewStyleSecureTextInput
    UIAlertViewStylePlainTextInput
    UIAlertViewStyleLoginAndPasswordInput
    

    UIAlertViewStylePlainTextInput being the one you want.

    Messing with the view hierarchy as described above is strongly discouraged by Apple.

    0 讨论(0)
  • 2021-02-12 13:00

    adding to answer from 'Shmidt', the code to capture text entered in UIAlertView is pasted below (thank you 'Wayne 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");
            }
    
        }
    }
    
    0 讨论(0)
  • 2021-02-12 13:02

    first of All Add UIAlertViewDelegate into ViewController.h File like

    #import <UIKit/UIKit.h>
    
    @interface UIViewController : UITableViewController<UIAlertViewDelegate>
    
    @end
    

    and than Add Below Code where you wants to alert Display,

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

    and it's delegate method which returns what input of UItextField

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    NSLog(@"%@", [alertView textFieldAtIndex:0].text);
    }
    
    0 讨论(0)
  • 2021-02-12 13:03

    As of iOS 8 UIAlertView has been deprecated in favor of UIAlertController, which adds support for adding UITextFields using the method:

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

    See this answer for an example.

    0 讨论(0)
  • 2021-02-12 13:13

    refer this... http://iosdevelopertips.com/undocumented/alert-with-textfields.html this is private api and if you use it for app in appstore it might get rejected but it is fine for enterprise development.

    0 讨论(0)
提交回复
热议问题