Disabling UINavigation Buttons In Array

Discussion in 'iOS Programming' started by Sweetooth44, Nov 7, 2013.

  1. Sweetooth44 macrumors newbie

    Joined:
    Sep 18, 2012
    #1
    I've got two buttons on the right side of my UINavigationController that are working as expected. This is the code I'm using to make this happen:

    Code:
    // Share Button
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareButton setFrame:CGRectMake(0,0,19,21)];
    [shareButton addTarget:self action:@selector(shareButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *shareBarButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButton)] autorelease];
    shareBarButton.tintColor = [UIColor whiteColor];
    
    // Snapback Button
    UIButton *navSnapbackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [navSnapbackButton setFrame:CGRectMake(0,0,26,21)];
    [navSnapbackButton setImage:[UIImage imageNamed:@"Snapback.png"] forState:UIControlStateNormal];
    [navSnapbackButton addTarget:self action:@selector(snapbackButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *navSnapbackBarButton = [[[UIBarButtonItem alloc] initWithCustomView:navSnapbackButton] autorelease];
    navSnapbackButton.tintColor = [UIColor whiteColor];
    
    // Right Toolbar Button Setup
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:navSnapbackBarButton, shareBarButton, nil]];
    I have a simple request: how do I disable a button in this array? For example say under a specific condition I wanted the Share button disabled, how would I go about doing this? Thanks in advance!
     
  2. ViviUO, Nov 8, 2013
    Last edited: Nov 8, 2013

    ViviUO macrumors 6502

    Joined:
    Jul 4, 2009
    #2
    I would declare it as an instance variable so that it can be referenced from anywhere in your class when you need to do something with it.

    Interface:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface MyViewController : UIViewController <UINavigationControllerDelegate>
    {
         UIButton *shareButton;
    }
    
    Implementation:

    Code:
    shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareButton setFrame:CGRectMake(0,0,19,21)];
    [shareButton addTarget:self action:@selector(shareButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *shareBarButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButton)] autorelease];
    shareBarButton.tintColor = [UIColor whiteColor];
    
    // Repeat for other buttons, etc, then add them to your nav the same way
    
    Then when you want to do something with that button somewhere else in your controller:

    Code:
    if (myCondition) [shareButton setEnabled: BOOL];
    
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    The code you posted above for the share button doesn't make sense. You create a UIButton called shareButton, and then you ignore it and create a system UIBarButtonItem. The shareButton object is never used. You set up the UIBarButtonItem using a selector called "shareButton", but that has nothing to do with the share button itself. If you skipped creating the UIButton the result would be identical.

    As for your question, you set up the navSnapbackButton using the initWithCustomView method, which lets you put a regular UIButton inside a navbar button item. If you do that and save pointers to the buttons inside the navbar, you could then enable or disable the buttons at will.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    I would save the buttons or bar button items as ivars and enable them as needed. You could probably also troll through the rightBarButtonItems array at any time to find the item you want.
     
  5. Sweetooth44 thread starter macrumors newbie

    Joined:
    Sep 18, 2012
    #5
    I need someone to write it out for me if possible. I'm still learning.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    Code:
    @property (nonatomic, weak) UIButton* navSnapbackButton;
    
    self.navSnapbackButton = navSnapbackButton;
    
    self.navSnapbackButton.enabled = shouldBeEnabled;
    
    That's about it. Just put those lines in the right places and you're done.
     
  7. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    Here's what you need to do:

    Refactor your code so you create both bar button items using the initWithCustomView: method (change the code that creates the share button so it uses initWithCustomView rather than initWithBarButtonSystemItem: )

    Create 2 instance variables, shareUIButton and snapUIButton. Save the UIButtons that you create to those instance variables in the code that is setting up the nav bar buttons.

    Then write methods like this:

    Code:
    - (void) enableShareButton: (BOOL) enable;
    {
      shareUIButton.enabled = enable;
    }
    
    Write method just like that for your snapBack button. Call those methods as needed to enable/disable your two buttons.
     

Share This Page