保持显示UIAlertView [英] Keep UIAlertView displayed

查看:96
本文介绍了保持显示UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有textField的UIAlertView和两个按钮:Save&取消。点击保存按钮时,我正在检查文本字段是否为空,如果是,我只想将textFields占位符更改为:@请输入名称并在屏幕上保留警报视图。但它会自动被驳回。

I have a UIAlertView with a textField on it and two buttons: Save & Cancel. When the Save button is tapped I am checking if the text field isn't empty and after if it is I simply want to change the textFields placeholder to: @"enter a name please" and KEEP the alert view on screen. However it is automatically dismissed.

如何覆盖?

推荐答案

将目标添加到子类alertView中的文本字段。您可以将alertView子类化,而不是忽略,如本文所述

Add a target to the textfield in a subclassed alertView. You can subclass the alertView and not dismiss as described in this post

[[alertView textFieldAtIndex:0] addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];

然后编写一个名为textFieldDidChange的函数,它检查alertView的当前文本字段并设置一个布尔值,这样你就可以了知道是否解除警报。

Then write a function called textFieldDidChange that checks the current textfield of your alertView and set a boolean value so you know whether or not to dismiss the alert.

- (void) textFieldDidChange
{
  NSString *alertViewText = [[alertView textFieldAtIndex:0] text];

  if ([alertViewText isEqualToString:@""]) {
    [alertView setMessage:@"Enter a name please."];
  } else {
    [alertView setMessage:@"Default Message"];
  }
}

*或者,我建议禁用保存当它为空时,不必子类。 *

- (void) textFieldDidChange
{
  NSString *alertViewText = [[alertView textFieldAtIndex:0] text];

  if ([alertViewText isEqualToString:@""]) {
    [alertView setMessage:@"Enter a name please."];
    for (UIViewController *view in alertView.subview) {
       if ([view isKindOfClass:[UIButton class]]) {
          UIButton *button = (UIButton *)view;
          if ([[[button titleLabel] text] isEqualToString:@"Save"])
             [button setEnabled:NO];
       }      
    }
  } else {
    [alertView setMessage:@"Default Message"];
    for (UIViewController *view in alertView.subview) {
       if ([view isKindOfClass:[UIButton class]]) {
          UIButton *button = (UIButton *)view;
          if ([[[button titleLabel] text] isEqualToString:@"Save"])
             [button setEnabled:YES];
       }      
    }
  }
}

这篇关于保持显示UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆