1

我试图使用以下代码在 UIAlertView 中包含一个 UITextField:

UIAlertView *newLabel = [[UIAlertView alloc]initWithTitle:@"New Label" message:@"\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UITextField *newLabelNameField = [[UITextField alloc]initWithFrame:CGRectMake(12.0, 45.0, 260.5, 25.0)];
newLabelNameField.placeholder = @"Label Name";
[newLabelNameField setBackgroundColor:[UIColor whiteColor]];
[newLabelNameField resignFirstResponder];
[newLabel addSubview:newLabelNameField];
[newLabel show];

我面临的主要问题是它resignFirstResponder不起作用,即按下返回键时键盘没有隐藏。
其次,有什么方法可以编写一个在OK按下按钮时必须执行的方法,比如Label Name使用文本字段将接收到的内容添加到数据库中。OK只有当我们按下按钮而不是按钮时,才必须执行该方法Cancel

4

2 回答 2

2

1)对于第二个问题,首先将一些标签设置为UIAlertViewlike newLabel.tag = 5

UIAlertView2)然后通过检查tag&编写以下委托方法buttonindex

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 5 && buttonIndex == 1)//OK button clicked
    {
        //Write code 
    }
}

UITextField3)对于第一个问题,编写like的委托方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
于 2013-07-22T07:11:28.180 回答
1

为什么不使用 UIAlertview 中的默认文本字段尝试一次,

    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Save" message:@"Please Enter the Name" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [alertView show];

编辑:你会在这里得到数据

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
       NSLog(%@"[[alertView textFieldAtIndex:0] text]");
    return  ([[[alertView textFieldAtIndex:0] text] length]>0)?YES:NO;

}
于 2013-07-22T07:23:27.550 回答