How to disable "save" button until user adds input

Discussion in 'iOS Programming' started by AndyElliott, Oct 26, 2010.

  1. AndyElliott macrumors newbie

    Joined:
    Oct 26, 2010
    #1
    I have programmatically added a save and cancel button to my AddItemViewController which includes a textField and textView.
    This is presented after a user taps a "+" button on the previous view.

    The problem is; if no text is added to the text field I can still save. A blank item is added to the list. Also, because the buttons were created programmatically I can't use IB and I'm flying blind!

    How can I require text to be input to enable the save function programmatically?
    I've included what I've got so far.

    Thanks!

    Code:
    -(IBAction) save:(id)sender {
    	NSLog(@"Save pressed");
    	if (aTitle !=nil) {
    		[titleArray removeObject:aTitle];
    		self.aTitle = nil;
    	}
    	
    	
    	//create a new title dictionary for the new values
    	NSMutableDictionary* newTitle = [[NSMutableDictionary alloc] init];
    	[newTitle setValue:titleTextField.text forKey:NAME_KEY];
    	[newTitle setValue:titleDetailView.text forKey:TITLEINFO_KEY];
    	
    	//add it to the master title array and release our reference
    	[titleArray addObject:newTitle];
    	[newTitle release];
    	
    	NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:NAME_KEY ascending:YES selector:@selector(caseInsensitiveCompare:)];
    	[titleArray sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
    	[nameSorter release];
    	
    	[self dismissModalViewControllerAnimated:YES];
    	
    }
    
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    UIButtons are UIControls. So they have an enabled property. Set the property to false when you create the button. Take a look at the documentation for whichever control you are using for the text field to see how to get delegate messages or notifications whenever the text changes and set the enabled property to true when there is text.

    And before you ask: no I will not post "sample code".
     
  3. AndyElliott thread starter macrumors newbie

    Joined:
    Oct 26, 2010
    #3
    Thanks robbieduncan

    Please feel free to not answer any of my questions in the future.

    I appreciate your response but won't tolerate rudeness.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I have answered your question: I have provided everything required for you to solve the problem you presented. I have not been rude. Please tell me exactly which part you think is rude?
     
  5. AndyElliott thread starter macrumors newbie

    Joined:
    Oct 26, 2010
    #5
    It's not my practice to ask people for "examples". For you to presume that of me is uncalled for. I can bet you DO that request often though!
    I've been learning programming for a few weeks now and often have to piece together complete answers.

    Your answer does give me a start but doesn't address the second piece of the puzzle. That is: If I disable the button by default, the question is how to enable it only when a user adds text to a text field.

    Learning this stuff is intense!

    Thanks
     
  6. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #6
    Robbie is right on with the enable property and the UITextView delegate. I just wanted to point out, you may want to be a little more descriptive on your method names this will help you when reviewing your code later down the road.


    The enable property will help you disable/enable the Save/Cancel buttons, while the UITextView delegate will tell you when to enable\disable the buttons
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Great. What do you bet? I'll be happy to collect any time. I think you'll find pretty much zero requests from me for code. You will find numerous requests from people for me to give them code.

    As I said: open the documentation for the control you are using and look for delegate methods or notifications you can subscribe to. Specifically ones that will tell you when the text changes. You can then examine the text and decide whether to enable the save button or not. For example if you are using UITextField I'd look at the declared delegate protocol for useful looking methods that would get called when the text field should changes some characters in a specific range.
     
  8. AndyElliott thread starter macrumors newbie

    Joined:
    Oct 26, 2010
    #8
    Correction.

    I need to correct something I said.
    My comment about requesting examples was meant to be that I'm sure lots of people ask you to supply the code certainly not the other way around.
    Sorry about that.
     
  9. AndyElliott thread starter macrumors newbie

    Joined:
    Oct 26, 2010
    #9
    I can not figure this out!

    Based on this thread can someone please explain the logic to apply to this problem?

    More than anything that's what I think I'm missing here.

    To explain simply,
    I have a text field that I want to send a message to enable a "save" button.

    I've made the view controller a <UITextFieldDelegate>
    I can see that - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string is the instance method I should use.
    What I can't get my head around is how to start off with the save button disabled then enable it when text is added.

    Frustrated!!!!!
     
  10. kayloh20 macrumors regular

    Joined:
    Apr 8, 2010
    Location:
    Chicago, IL
    #10
    Now that you have the delegate method, you'll need to figure out the logic behind checking whether a user has inputted something.

    For example, depending on how much you want to optimize, you could simply enable the save button when the replacement string's length is greater than zero (though that means it will essentially enable the save button every single time a key is pressed) or you could check the textField's text's length and if it is zero and the replacement string's length is greater than zero, then enable the save button.

    To disable the save button in case they delete all the text, you'll check to see if the replacement string's length is zero and check to see whether the range matches the textField's text length (in case they select all and delete).
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Seems to me that your logic is right here is this post from robbieduncan:
    Break your problem down into smaller problems. For example:
    Problem 1) Get the delegate message whenever the text changes. You seem to have found the correct method. Just confirm, maybe through an NSLog or a breakpoint, that it is being called as expected.
    Problem 2) "When there is text": Construct a condition that tests the existence of text or not.
    Problem 3) Set the enabled property of your save button.

    Once you have solutions to all three, combine them to form your overall solution.

    Hope that helps.
     
  12. AndyElliott thread starter macrumors newbie

    Joined:
    Oct 26, 2010
    #12
    Kayloh20 you make a good point.

    Like most things I'm over-thinking this.
    The view has both Cancel and Save buttons already.
    I figure most folks are smart enough to cancel if they don't want to keep what they've typed.
    In other words, enabling/disabling the save button is over-kill.
    Would you agree with that logic?

    Thanks!
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    I wouldn't, necessarily. You need to be sure that saving when there's no text doesn't cause problems down the line (for example, consider the case where the text is saved to a non-null field in a database).
     
  14. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    Absolutely 100% no. If save causes unwanted effects then it should be disabled. In fact, (whilst I don't have time to read the entire iPhone HIG again to find the section), I think the HIG say that the button should not even be visible unless it can be used.

    If it was me I'd use the delegate method to show/hide the save button as it became applicable.
     
  15. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #15
    Disabled buttons can be confusing for some users plus they take up precious space. However disabling things in the correct context works nicely.
     
  16. kingthong macrumors member

    Joined:
    Sep 20, 2010
    Location:
    Somewhere but not here.
    #16
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    Your blog article is titled "UIButton hack"? If it uses proper, built-in functionality to achieve the desired result I'm not sure how it's a "hack".
     

Share This Page