Adding TextField to UIAlertView

前端 未结 9 1355
不知归路
不知归路 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 13:16

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

    Also, you 'll need to implement UITextFieldDelegate, UIAlertViewDelegate protocols.

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

    You can try:

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

    Follow this link for detail.

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

    I'm using BlockAlertsAndActionSheets instead of the Apple components for AlertViews and ActionSheets as I prefer the blocks-approach. Also contains a BlockTextPromptAlertView in the source, which might be what you want. You can replace the images of that control to get the Apple-style back.

    Project on gitgub

    Tutorial which gets you started

    Example:

    - (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 讨论(0)
提交回复
热议问题