带有一个文本字段的UIAlertView(用于登录)

时间:2014-01-04 07:54:14

标签: ios cocoa-touch uitableview uitextfield uialertview

当我向下拉tableView时,alertView应显示一个登录文字字段。
警报必须到来,但不能选择textField 点击此textField后,键盘就会出现。

为此,我使用此代码:

- (void)toggleCells:(UIRefreshControl*)refreshControl
{
    message = [[UIAlertView alloc] initWithTitle:@"Create a new list"
                                                      message:@""
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:@"OK", nil];
    [message setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [message setTag:1001];
    //[message textFieldAtIndex:0].delegate = self;

    [refreshControl beginRefreshing];
    [message show];
    [refreshControl endRefreshing];
    //...
}

2 个答案:

答案 0 :(得分:0)

试试这个

UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"SayWhat" message:@"Please enter Email address" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    av.alertViewStyle = UIAlertViewStylePlainTextInput;
    [av setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [[av textFieldAtIndex:0] setPlaceholder:@"Email address"];
    [[av textFieldAtIndex:0] setDelegate:self];

[av show];
[[av textFieldAtIndex:0] resignFirstResponder];

答案 1 :(得分:0)

尝试:( 不太好,但快速修复

message = [[UIAlertView alloc] initWithTitle:@"Create a new list"
                                     message:nil
                                    delegate:self
                           cancelButtonTitle:@"Cancel"
                           otherButtonTitles:@"OK", nil];

[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message setTag:1001];
[[message textFieldAtIndex:0] setDelegate:self]; //you'll surely need it later

[message show];

[[message textFieldAtIndex:0] resignFirstResponder]; //after showing the alertView

注意:上面的方法并不是很好。


替代方法:

注意:在BOOL skipAlertTextField;

中声明“.h
- (void)toggleCells:(UIRefreshControl*)refreshControl {
    //...
    message = [[UIAlertView alloc] initWithTitle:@"Create a new list"
                                         message:nil
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                               otherButtonTitles:@"OK", nil];

    [message setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [message setTag:1001];
    [[message textFieldAtIndex:0] setDelegate:self];

    skipAlertTextField = YES;  //important
    [message show];
    //...
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if(textField.tag == 1001 && skipAlertTextField) {
        skipAlertTextField = NO;
        return NO;
    }

    return YES;
}