viewcontroller中的UITextView +2。 textViewDidEndEditing调用两次,如何管理呢?

时间:2014-01-22 11:53:09

标签: ios objective-c uitextview

我有一些需要运行IF textView1 textViewDidEndEditing的核心数据代码。然后停止并且不从textView2 swell运行代码 截至目前,我只运行textViewDidEndEditing中的所有代码。即使你有一个if声明。但我做错了。因为它运行来自两个IF语句的代码......

仅当textView1完成编辑时,它如何才能运行? 完成编辑后再次运行textView2的代码

- (void)textViewDidEndEditing:(UITextView *)textView{

if ((textView = textView1)){
 NSManagedObjectContext *localContext = [NSManagedObjectContext MR_defaultContext];
    TextView *aTextView = [TextView MR_createEntity];
    aTextView.belongsTo = @"jegKomTil1";
    // Build the predicate to find the person sought
    NSString *predicateString = [NSString stringWithFormat:@"jegKomTil1"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"belongsTo == %@", predicateString];
    TextView *aTextView1 = [TextView MR_findFirstWithPredicate:predicate inContext:localContext];
    // If a person was founded
    if (aTextView1) { // Update its age
        aTextView1.textDataView = textView1.text;
       }
    [localContext MR_saveToPersistentStoreAndWait];
}
if ((textView = textView2)){
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_defaultContext];
    TextView *aTextView = [TextView MR_createEntity];
    aTextView.belongsTo = @"jegKomTil2";
    // Build the predicate to find the person sought
    NSString *predicateString = [NSString stringWithFormat:@"jegKomTil2"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"belongsTo == %@", predicateString];
    TextView *aTextView1 = [TextView MR_findFirstWithPredicate:predicate inContext:localContext];
    // If a person was founded
    if (aTextView1) { // Update its age
        aTextView1.textDataView = textView2.text;
    }
    [localContext MR_saveToPersistentStoreAndWait];
    NSLog(@"textViewDidEndEditing: 2");
}

2 个答案:

答案 0 :(得分:2)

正如评论中所提到的,if语句是罪魁祸首。还有大量的代码重复。我已经快速尝试简化你的逻辑,因为它们之间的唯一真正区别就是你为belongsTo属性和NSPredicate实例使用的字符串......

BOOL textViewMatchFound = (textView == textView1 || textView == textView2);

// The BOOL / outer if won't be necessary if you textView1 and textView2 are the ONLY textViews triggering this method... 
if (textViewMatchFound) {
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_defaultContext];
    TextView *aTextView = [TextView MR_createEntity];

    NSString *arrivalText = @"jegKomTil1";
    if (textView == textView2) {
        arrivalText = @"jegKomTil2";
    }

    aTextView.belongsTo = arrivalText;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"belongsTo == %@", arrivalText];
    TextView *anotherTextView = [TextView MR_findFirstWithPredicate:predicate inContext:localContext];

    // If a person was found
    if (anotherTextView) { // Update its age
        anotherTextView.textDataView = textView.text;
    }
    [localContext MR_saveToPersistentStoreAndWait];
}

答案 1 :(得分:0)

我建议您在创建textviews时使用属性标记

UITextView *textView1 = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
textView1.tag = 1;
UITextView *textView2 = [[UITextView alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
textView1.tag = 2;

然后使用:

if (textView.tag == 1)
...