0

我正在将 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];

我用文本字段、取消和确定按钮对其进行纵向显示。但在横向中,它只出现文本字段、取消和确定按钮不出现。

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

4

2 回答 2

2

像这样使用警报视图

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

并像这样访问文本字段的值

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

    if(buttonIndex==1)
    {
        UITextField *textField=[alertView textFieldAtIndex:0];
}
}
于 2013-08-02T13:12:43.727 回答
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];
于 2015-11-09T06:32:07.403 回答