2014-01-22 72 views
-1

我有一些核心數據代碼需要運行IF textView1 textViewDidEndEditing。然後停止並不從textView2 swell 運行代碼,截至目前,我只是運行textViewDidEndEditing中的所有代碼。即使我有一個if語句。但我做錯了。因爲它運行來自兩個IF語句的代碼...UITextView +2在viewcontroller中。 textViewDidEndEditing調用兩次,如何管理它?

它是如何使它僅在textView1完成編輯時才運行? 這就是做編輯

- (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"); 
} 
+4

如果(TextView的== textView1之間的唯一區別真)。 (看到區別?) –

+0

@ nickfalk是啊我注意到了區別。但它會工作,或者我應該做什麼igrekde設置爲答案? – KennyVB

+0

它應該工作得很好。我不知道你打算讓應用最終做什麼,但是檢查一個對象實例可能沒問題。我寧願讓你給他們更明確的名字,比使用標籤,因爲標籤不會真的讀得那麼好... –

回答

2

正如評論中所述,if語句是罪魁禍首。此外還有大量的代碼重複。我做了快速的嘗試,以簡化你的邏輯,因爲它似乎兩者是你使用的屬於關聯屬性和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]; 
} 
+0

工作正常。如果我只想要textView1.text TextView2.text怎麼樣?因爲它是我處理 – KennyVB

+0

的2個UITextViews在我的代碼中存在缺陷。在textView2代碼中它應該是textView2.text。但從來沒有那麼簡單的代碼如何工作呢?我很欣賞你的行爲。我編輯了我的帖子以反映我的代碼 – KennyVB

+0

您不需要參考textView1和textView2。方法參數textView指向完全相同的內存位置。據我所知,沒有任何地方可以改變任何超出方法範圍的對象或變量。 –

0

我建議你使用的財產時,再次運行textView2代碼標籤創建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) 
... 
+0

它也工作,如果我在storyboard中製作textViews? – KennyVB

+0

不是上述解決方案的巨大粉絲,因爲1,2等不會很好地閱讀。我寧願有更少的通用名稱爲textViews。 –

+0

@KennyVB,是的,你可以在Attributes Inspector - > View - > Tag – etolstoy

相關問題