【发布时间】:2014-04-16 12:58:20
【问题描述】:
我已将自定义视图添加到警报视图。
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 中有一些文本字段。警报视图有两个按钮“提交”和“取消”。
点击“提交”按钮时,我需要验证文本字段中的输入。如果输入无效,我不想关闭警报。例如。如果电子邮件无效,我将显示另一个警报,提示“电子邮件无效”。我将如何实现这一目标?到目前为止,我尝试了以下事情:
1) 用以下方法编写验证逻辑:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
但执行此方法后警报视图消失。
2) 继承 UIAlertView。我试图在子类 UIAlertView 中覆盖以下方法:
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
虽然我点击警报视图上的任何按钮,但不会调用此方法。苹果说,
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.
我今天花了很多时间来解决这个问题,但没有运气..!!请帮我。
【问题讨论】:
-
只需创建自己的 AlertView,因为 alertview 只能按原样使用。您正在使用私有方法来设置您的自定义视图,这可能会导致您的应用被拒绝。
-
感谢您的建议@rckoenes..!!
-
我认为你可以使用委托方法之外的东西。 - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
-
或者您可以将“提交”UIButton 放在 myViewController 中,它看起来类似于具有相同尺寸的取消按钮,位于 myViewController.view 的底部,并在 alertView 中只保留一个按钮来取消警报。
标签: ios objective-c ios7 uiviewcontroller uialertview