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

前端 未结 10 1318
情歌与酒
情歌与酒 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:56

    Anyone supporting iOS 5.0 onwards with ARC, there's this direct alertViewStyle property you can set:

    -(void)showAlertWithTextField{
        UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Name" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];    
        [dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
    
        // Change keyboard type
        [[dialog textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
        [dialog show];
    }
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex == 1)
            NSLog(@"%@",[[alertView textFieldAtIndex:0]text]);
    }
    
    0 讨论(0)
  • 2020-11-28 06:00

    For those who may care, here's a working solution:

    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Enter Name"];
    [dialog setMessage:@" "];
    [dialog addButtonWithTitle:@"Cancel"];
    [dialog addButtonWithTitle:@"OK"];
    
    nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    [nameField setBackgroundColor:[UIColor whiteColor]];
    [dialog addSubview:nameField];
    CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
    [dialog setTransform: moveUp];
    [dialog show];
    [dialog release];
    [nameField release];
    

    Make sure you've created UITextField * nameField; in your .h file, then you can get at the text the user typed in by doing: inputText = [nameField text];

    in the - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method.

    important note: for iOS 4.0+, the CGAffineTransform is done automatically so you can leave that bit out if you're only targeting 4.0+. Otherwise, you will need to check which OS you are on and handle it accordingly.

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

    A quite nice approach is, you can use the delegate methods of the UITextFieldDelegate to move the dialog up only when the keyboard is activated. See below.

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5]; 
        [UIView setAnimationDelegate:self];
    
        CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, -20);
        [dialog setTransform: moveUp];
    
        [UIView commitAnimations];
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5]; 
        [UIView setAnimationDelegate:self];
    
        CGAffineTransform moveDown = CGAffineTransformMakeTranslation(0.0, 0.0);
        [dialog setTransform: moveDown];
    
        [textField resignFirstResponder];
    
        return YES;
    }
    
    0 讨论(0)
  • 2020-11-28 06:07

    Found more clear solution. Check this code snippet out: Username and Password UITextFields in UIAlertView prompt

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