我已将自定义 View 添加到警报 View 。
MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier: @"MyView"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit", nil];
[alertView setValue: myViewController.view forKey:@"accessoryView"];
[alertView show];
myViewController.view
中有一些文本字段。警报 View 有两个按钮“提交”和“取消”。
点击“提交”按钮后,我需要验证文本字段中的输入。如果输入无效,我不想解除警报。例如。如果电子邮件无效,我将显示另一个警告“电子邮件无效”。我该如何实现?到目前为止,我尝试了以下操作:
1) 在以下方法中编写验证逻辑:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
但是执行此方法后警报 View 消失。
2) 子类化 UIAlertView。我试图在子类 UIAlertView 中覆盖以下方法:
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
虽然我点击警报 View 上的任何按钮,但不会调用此方法。苹果说,
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
我今天花了很多时间来解决这个问题,但没有运气..!!请帮我。
最佳答案
我的建议是用其他东西替换 UIAlertView
。 TTAlertView和 SDCAlertView两者都在积极开发中,应该允许您以任何需要的方式灵活地对 View 进行子类化和/或皮肤处理。
关于ios - 自定义警报 View 的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23110177/