2013-03-20 2 views
0

내보기에 textField 및 버튼이 있고 텍스트 필드가 비어 있으면 버튼을 클릭 할 수 없으므로 원하지 않습니다. 사용자가 텍스트 필드에 텍스트를 입력하면 버튼이 클릭 할 수 있습니다. 어떻게해야합니까? 고맙습니다.UITextField 및 UIButton

답변

1

, 당신은 설정할 수 있습니다 : 당신의 편집에 그런

[textField addTarget:self action:@selector(editing:) forControlEvents:UIControlEventAllEditingEvents]; 

:이 :

-(void)editing:(UITextField *)sender { 
    myButton.enabled = ![sender.text isEqualToString:@""]; 
} 
+0

이 작동하지 않도록해야한다에서 사용할 수 위임 감사합니다. – jxdwinter

1

텍스트 필드의 변경 내용을 듣습니다. 텍스트가 변경되면 텍스트가 있는지 여부에 따라 버튼의 enabled 속성을 업데이트하십시오. 물론 처음부터 버튼의 상태도 설정해야합니다.

당신의 UITextField에 대한
// Setup the text field change listener (this can be done in IB if appropriate) 
// Put this in viewDidLoad if not using IB. 
UITextField *myTextField = ... // a reference to the text field 
[myTextField addTarget:self action:@selector(textFieldChangedAction:) forControlEvents:UIControlEventEditingChanged]; 

// Initialize the button's state (put this in viewDidLoad) 
myButton.enabled = myTextField.text.length > 0; 

// The method called as the text changes in the text field 
- (void)textFieldChangedAction:(UITextField *)sender { 
    myButton.enabled = sender.text.length > 0; 
} 
0

당신이 원하는 경우에, 당신이 할 수있는 "숨기기"버튼을 설정하여 버튼의 알파 값을 0으로 설정하고 텍스트 필드가 적어도 하나 이상의 문자 인 경우 버튼의 알파 값을 1로 설정하여 버튼을 "표시"합니다. 나는 개념적으로이 일을하기가 쉽고 사용자에게 매우보기 좋다고 생각한다.

0

textfielddidbeginediting 대의원에 그런 자기 에 textfield.delegate

그리고 거기 ( 확인이 텍스트 필드가이다는 @ "" 다음 버튼 사용 버튼 그렇지

해제) 설정 하나 더 있습니다. , 유

Textdidchangecharacters

그리고 버튼의 viewDidLoad에 처음 상태