iphone - 检查 UIViewController 是否有任何文本字段、 TextView 或标签更改..?

标签 iphone objective-c ios uiviewcontroller

我有一个 View Controller ,它包含 n 个 UITextFieldsUItextViewsUILabels,无论如何我可以得到通知吗他们的变化..?

我可以通过在各自的 TextDidChangeNotification 和类似的地方手动查看它们中的每一个来做到这一点,但我正在寻找一种更优化的方法来做到这一点,我不必担心每个其中 .

最佳答案

// Assumes you don't use tag values now - if you do small change to create
// and index set, add the ones you use, so all new ones assigned are unique.
// assumes ARC

1) 新伊娃:

{
    NSMutableDictionary *savedValues; 
}

1) 当您想要设定值的基线时:

savedValues = [self snapshot];

2) 最初将其称为基线当前值,然后在以后的任何时候调用:

- (NSMutableDictionary *)snapshot
{
    NSInteger tag = 1;
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:[self.view.subviews count]];

    for(UIView *v in self.view.subviews) {
        if([v isKindOfClass:[UILabel class]] || [v isKindOfClass:[UITextField class]] || [v isKindOfClass:[UITextView class]]) {
            if(v.tag == 0) v.tag = tag++; // will happen once
            // EDIT below
            [dict setObject:[NSString stringWithString:[(id)v text]] forKey:[NSNumber numberWithInteger:tag]];
        }
    }
    return dict;
}

4) 当您想查看是否有任何变化时:

- (BOOL)anyChanges
{
    NSDictionary *currentDict = [self snapshot];
    return [currentDict isEqualToDictionary:savedValues];
}

关于iphone - 检查 UIViewController 是否有任何文本字段、 TextView 或标签更改..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12142197/

相关文章:

ios - 需要访问包含CLLocation信息的NSArray对象

objective-c - 后台应用程序 : NSAlert before shutdown

iphone - navigationBar 设置背景图像 :nil forBarMetrics:UIBarMetricsDefault not working

iphone - 资源库兼容哪些视频格式?

iphone - iPhone 应用程序中的 EXC_BAD_ACCESS - 内存管理问题

iphone - UILocalNotification的几个soundName

android - 澄清 Firebase 连接

iphone - 在 iphone 中使用 ASIHTTPRequest

html - 如何强制 UIWebView 在浏览器中打开链接?

ios - 如何从 Storyboard 中删除堆栈 View