UITextField in UIAlertView on iPhone - how to make it responsive?

前端 未结 10 1317
情歌与酒
情歌与酒 2020-11-28 05:27

I placed a UITextField into a UIAlertView and moved it up so the keyboard wouldn\'t cover it up with the following code:

[dialog setDelegate:self];
[dialog s         


        
相关标签:
10条回答
  • 2020-11-28 05:41

    It's worth checking out

    http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html?cmd=success#comment3870

    For a complete and comprehensive solution.

    0 讨论(0)
  • 2020-11-28 05:41

    This is for iOS5 and ARC.

    -(void)showAlert {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"New Album" 
            message:@"Enter a name for the album." 
            delegate:self 
            cancelButtonTitle:@"Cancel" 
            otherButtonTitles:@"Save", nil];
        alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    
        UITextField* textfield = [alertView textFieldAtIndex:0];
        textfield.placeholder = @"Title";
    
        [alertView show];
    }
    
    -(void)alertView:(UIAlertView *)alertView 
        didDismissWithButtonIndex:(NSInteger)buttonIndex 
    {
        if (buttonIndex != 1) {
            NSLog(@"Cancel");
            return;
        }
        UITextField* textfield = [alertView textFieldAtIndex:0];
        NSLog(@"Save. text: %@", textfield.text);
    }
    
    0 讨论(0)
  • 2020-11-28 05:42

    A simple way to locate the text field, without keeping an explicit reference to it, is to set its tag:

    nameField.tag = ALERTVIEW_NAMEFIELD;
    

    Make sure it is different from 0 and from other UIView object tags you may have, including the parent dialog!

    Then inside the alertView:clickedButtonAtIndex: handler, you can retrieve your text field this way:

    UITextField *nameField = (UITextField *)[alertView viewWithTag:ALERTVIEW_NAMEFIELD];
    
    0 讨论(0)
  • 2020-11-28 05:49

    Would like to add one minor tweak to iWasRobbed's answer:

    GenericAlertDialogs.m:

    + (void)displayInputDialog:(NSString*)title delegate:(id<UIAlertViewDelegate>)delegate textFiled:(UITextField*)txtField
    {
        UIAlertView* dialog = [[UIAlertView alloc] init];
        [dialog setDelegate:delegate];
        [dialog setTitle:title];
        [dialog setMessage:@" "];
        [dialog addButtonWithTitle:@"Cancel"];
        [dialog addButtonWithTitle:@"OK"];
    
        if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
        {
            txtField.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
        }
        else 
        {
            txtField.frame = CGRectMake(20.0, 30.0, 245.0, 25.0);
        }
    
        [txtField becomeFirstResponder];
        [txtField setBackgroundColor:[UIColor whiteColor]];
        [dialog addSubview:txtField];
        [dialog show];   
    }
    

    Some other .m file (implements UIAlertViewDelegate)

     self->txtChangeName = [[UITextField alloc] init];
     [GenericAlertDialogs displayInputDialog:@"Some title...:" 
                                    delegate:self 
                                   textFiled:txtChangeName];
    

    The UIAlertViewDelegate protocol handling:

    - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
        if(buttonIndex == 1)
        {
            if([txtChangeName.text length] > 0)
            {
                self->userInput = [txtChangeName text];
             }
         }
         self->txtChangeName = nil;
    }
    

    iOS 4+ compliant.

    0 讨论(0)
  • 2020-11-28 05:51

    This is actually only 1 more line to the regular UIAlertView code. Hope this helps!

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:@"This is a alert with a textfield" delegate:self cancelButtonTitle:@"YAY" otherButtonTitles:nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alert show];
        [alert release];
    

    only runs on iOS 5 or later

    0 讨论(0)
  • 2020-11-28 05:54

    Check out this implementation: https://github.com/josecastillo/EGOTextFieldAlertView

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