2010-10-06 28 views
0

我試圖在執行UIControlEventEditingChanged事件時清除UITextField。嘗試設置UITextField文本時正在執行選擇器

但是,當我將文本設置爲無,UIControlEventEditingChanged事件再次被調用,這是它繼續前進的方式。

這是我的代碼:

- (void)updateText { 
    //other code 
    textfield.text = @""; 
    //other code 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [textfield addTarget:self action:@selector(updateText) forControlEvents:UIControlEventEditingChanged]; 
} 

我希望能夠將文本字段的文本沒有它無限循環!

任何幫助表示讚賞。

回答

3

不知道這是最好的方式,但是在對文本字段進行程序化更新之前,您可以刪除UIControlEventEditingChanged偵聽器,然後再將其添加。

1

- (void)updateText { 
    //other code 
    if (![textfield.text isEqualToString:@""]){ 
     textfield.text = @""; 
    } 
    //other code 
} 

它可能不是最完美的解決方案,但它是非常快,一定會努力

0

的替代註銷該事件,並重新註冊是放在一個BOOL到警惕這種特殊的遞歸。

@interface MyClass :() { 
    BOOL clearText; 
} 

和在該方法中:

- (void)updateText { 
    if (clearText){ 
     clearText = NO; 
     return; 
    } 
    //other code 
    clearText = YES; 
    textfield.text = @""; 
    //other code 
} 
相關問題