我有一个 View Controller ,它包含 n 个 UITextFields
和 UItextViews
和 UILabels
,无论如何我可以得到通知吗他们的变化..?
我可以通过在各自的 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/