UIAlertView与UITextField

时间:2013-08-02 10:14:59

标签: uialertview

我正在将UITextfield添加到UIAlertView中,如下所示:

UIAlertView *addingBookAlert=[[UIAlertView alloc]initWithTitle:@"Enter Note" message:NULL delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    addingBookAlert.tag=kAddNoteAlertTag;

    myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
    myTextField.placeholder=@"Book Mark";
    [myTextField becomeFirstResponder];
    [myTextField setBackgroundColor:[UIColor whiteColor]];
    myTextField.textAlignment=UITextAlignmentCenter;
    myTextField.delegate=self;
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
    [addingBookAlert setTransform:myTransform];
    [addingBookAlert addSubview:myTextField];
    [addingBookAlert show];

我用textfield,cancel和ok按钮显示它的画面。但是在横向显示它只出现的文本字段,取消和ok按钮不显示。

如何解决这个问题...... 在此先感谢。

2 个答案:

答案 0 :(得分:2)

使用像这样的alertview

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Enter  Name" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit", nil];
    alert.alertViewStyle=UIAlertViewStylePlainTextInput;
    UITextField *textField=[alert textFieldAtIndex:0];
    textField.delegate=self;
    [alert show];
    [alert release];

并像这样访问textfield的值

#pragma mark alert button view button
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if(buttonIndex==1)
    {
        UITextField *textField=[alertView textFieldAtIndex:0];
}
}

答案 1 :(得分:0)

在iOS 8中, UIAlertview 已被弃用。使用 UIAlertController 代替。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Add Fruit" message:@"Type to add a fruit" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"Fruit";
}];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                                   style:UIAlertActionStyleDefault
                                                 handler:^(UIAlertAction *action) {
                                                     UITextField *textFiel = [alert.textFields firstObject];
                                                     [fruits addObject:textFiel.text];
                                                 }];
[alert addAction:okAction];

[self presentViewController:alert animated:YES completion:nil];