2016-07-22 47 views
1

這是我在textField中輸入任何內容的方法,但標題按鈕在標題中不可見。在UITextField中輸入時,代碼不起作用該按鈕不可見?

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 

    NSUInteger finalLenght = textField.text.length - range.length + string.length; 
    //SearchBtn.hidden = (finalLenght == 0); 

    if (finalLenght==0) { 
     self.navigationItem.rightBarButtonItem = nil; 
    }else{ 
     UIButton *SearchBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [SearchBtn setTitle:CustomLocalizedString(@"FIND CVS") forState:UIControlStateNormal]; 
     [SearchBtn addTarget:self action:@selector(actionFindJobs:) forControlEvents:UIControlEventTouchUpInside]; 
     SearchBtn.frame = CGRectMake(0, 0, 80, 30); 
     SearchBtn.hidden = YES; 
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:SearchBtn]; 
    } 
    return YES; 
} 
+0

當我點擊UITextField並在UITextField中輸入任何內容時,這個方法不會調用,所以我永遠不會理解的原因是什麼? –

+0

你設置了textField.delegate = self; ? – Hussein

+1

是的,這是代表問題,我已經解決了這個問題的朋友 –

回答

0

如何改變這樣的:

SearchBtn.hidden = YES; 

到:

SearchBtn.hidden = NO; 
+0

我嘗試但不工作, –

0

請檢查下面的代碼

[SearchBtn setEnabled:YES]; 

[SearchBtn setEnabled:NO]; 
+0

不工作這也是朋友 –