开发者

enabling UIBarButtonItem when textfield has input

开发者 https://www.devze.com 2023-02-16 10:06 出处:网络
I am trying to disable my send button (UIBarButtonItem within a toolbar) whenever there is no input in the \"userInput\"开发者_StackOverflow中文版 UITextField, and enable it when there is. Here is the

I am trying to disable my send button (UIBarButtonItem within a toolbar) whenever there is no input in the "userInput"开发者_StackOverflow中文版 UITextField, and enable it when there is. Here is the code I've written - I can't quite figure out why it isnt working.

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

if (userInput.text.length >0) {
    [sendButton setEnabled:YES];
}else{
    [sendButton setEnabled:NO];
}

}

Im also getting a warning that says "control reaches end of non-void function."

Im very new to xcode and programming so I'm not sure what that is telling me.

Thanks for any help!!


The text field object sends the message shouldChangeCharactersInRange to its delegate asking whether the change (adding the new character or removing a character) should be permitted or not. Since you don't want to refuse any changes made to the text field itself, you must return YES from this method. The warning

control reaches end of non-void function

means you have a non-void function so you are supposed to return something. Since nothing was being returned, this message popped up.

One important thing to remember is that this delegate method is called before the change is made, which makes sense because the delegate is being asked for permission to allow or disallow the change. If the delegate refuses, the text field's value will remain the same regardless of what the user types.

So calling userInput.text is useless because it will give the old value back as the change hasn't been made yet. However, there is enough information in the parameters of this method to construct the new to-be value of the text field.

NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string]);

The complete method would look like,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string]; 
    BOOL isFieldEmpty = [newText isEqualToString:@""];
    sendButton.enabled = !isFieldEmpty;
    return YES;
}


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

    if (userInput.text.length >0) {
        [sendButton setEnabled:YES];
    }else{
        [sendButton setEnabled:NO];
    }
    return YES;
    }


ok you need in set enable NO in vieWillAppear

-(void)viewWillAppear:(BOOL)animated
{
  [self.sendButton setEnabled:NO];

}

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

if (userInput.text.length >0) {
    [self.sendButton setEnabled:YES];
}else{
    [self.sendButton setEnabled:NO];
    }
   return YES;
}

make UIBarButton as property and IBOUtlet and make proper connection

.h

UIBarButton *sendButton;

@property(nonatomic,retain) IBOutlet UIBarButton *sendButton;

and in .m

@synthesize sendButton;

and

-(void)dealloc
{
  [self.sendButton release];
  [super dealloc];
}

and also remember to make connection for textField from IB.


In your controller add (weak) outlets for both the UITextView and UIBarButtonItem and hook them up to the items in view.

Add another IBAction to controller for example:

- (void)textChanged:(id)sender
{
     if ( [self.someTextField.text length] > 0 )
         [self.someBarButtonItem setEnabled:YES];
     else [self.someBarButtonItem setEnabled:NO];
}

Hook up UITextView's Edit Changed event to the textChanged: you've just created. The Edit Changed even fires up when characters are typed into UITextField, you check the length of the text and enable/disable UIBarButtonItem when the text field has some text.

You can also in viewDidLoad: check the initial text length and enable/disable the button depending on the text.

Hope that helps. It works for me in iOS 6.1.

0

精彩评论

暂无评论...
验证码 换一张
取 消