2012-01-18 69 views

回答

0

你可以在文件所有者从UIAlertViewDelegate方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
0

绑定文本字段事件“编辑真的结束”,或类似的打电话给你的validate方法,一种用于处理验证。该方法是您在controller.m文件中编写并在controller.h文件中声明的方法。控制器文件的确切名称取决于应用程序代码库的结构。

如何处理验证失败的情况,例如,内容为空,取决于您的应用程序的需求。例如,如果内容为空,则需要提醒用户,然后将焦点重置到文本字段。

如果你对iOS编程有点新鲜,你可能会发现Ray Wnderlich的教程很有用。 http://www.raywenderlich.com/

我发现“iOS学徒”做得很好。此外,Dave Mark撰写的一本新书“iOS 5开发入门”可能会有所帮助。

0

设置alertView的代表到您当前的viewController
然后在委托方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0)return; //for cancel button 

    UITextField *textField = [alertView textFieldAtIndex:0]; // since there is only one UITextField in alertView 
    if ([textField.text length] > 0) // checking the length of the text in UITextField 
    { 
      // Your code goes here 
    } 
} 

我希望这有助于。
BR,哈日

0

1:获取在alertView的UITextField

self.alertViewTextField = [alertView textFieldAtIndex:0]; 

2:检查文本长度时,文本框的编辑更改:

[self.alertViewTextField addTarget:self action:@selector(alertViewTextFieldDidChanged) forControlEvents:UIControlEventEditingChanged]; 

-(void)alertViewTextFieldDidChanged{ 
    if(self.alertViewTextField.text.length == 0){ 
     // ... 
    } 
} 
11

让我们假设你有一个“确定”按钮(或类似的东西)与UIAlertView的其他按钮中的第一个按钮相同。进一步假设,如果且仅当文本字段中的文本长度大于0时,才希望启用该按钮。然后验证的解决方案很简单。在UIAlertView中的委托实现:

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView 
{ 
    return [[alertView textFieldAtIndex:0].text length] > 0; 
} 

这样做的好处比一些其他的答案(使用clickedButtonAtIndex :),即用户直接感知的文本字段是否包含有效的输入。

这个委托消息在Apple的文档中没有得到很好的解释,但它工作得很好。对文本字段值的任何更改都会导致发送此消息,并且相应地启用或禁用“确定”按钮。