How to hide Bar Button Item?

Discussion in 'iOS Programming' started by newbie80, Mar 21, 2011.

  1. newbie80 macrumors member

    Joined:
    Mar 2, 2011
    #1
    Hi,

    I would like to know how do I hide the "Bar Button Item" on a "Navigation bar".

    I did this in "Interface Builder". First I drag the "Navigation bar" to my view, then I drag the "Bar Button Item" and put it on "Navigation bar".

    In my Header file, I have this:
    Code:
    -(IBAction)start_game; // which is the button that I want to hide
    
    In my Main file, I have this:
    Code:
    -(IBAction)start_game{
    	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/30.0];
    	[[UIAccelerometer sharedAccelerometer] setDelegate:self]; 
    }
    
    What I need is to hide this "start_game" button when I click on it.
    Hope anyone can help me :(, thank you.
     
  2. jnoxx, Mar 21, 2011
    Last edited by a moderator: Mar 21, 2011

    jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Make a reference to the button
    Code:
    IBOutlet UIBarButtonItem *startGame;
    then go like this in ur .M file.

    Code:
    startGame.setHidden = YES;
    i think that's what u want ;)
     
  3. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #3
    Hi jnoxx,

    I got an error: request for member "setHidden" in something not a structure or union. :(
     
  4. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    or
    Code:
    [startGame setHidden:YES];
    ;)
     
  6. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #6
    I still got the same thing error: request for member "Hidden" in something not a structure or union. :confused:
     
  7. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    Capitalization etc... Has to be exactly the same as your object.

    Use start_game instead of startGame. (assuming your first post is correct.)

    NOTE: startGame would be more typical for Objective C so try to go that way in the future.

    B
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    What type is startGame defined as?

    start_game is a method name and not a UIView subclass (if the first post is correct). ;)
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    A barbuttonitem isn't a view and it doesn't have a hidden property.

    You could use the enabled property to make it so that it can't be tapped.

    You could remove the item from the toolbar.

    If you make the item from a custom view then you could manipulate the custom view inside the bar button item.
     
  10. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #10
    I tried this:
    Code:
    [startGame setHidden:YES];
    
    It has a warning: 'UIBarButtonItem' may not respond to '-setHidden'. (Message without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)

    And and also when run iphone simulater, when I click on the button, it open my code editor and shows ___TERMINATING DUE TO UNCAUGHT EXCEPTION___ :(

    I'm doing now, just saw new replies here.
     
  11. dejo, Mar 21, 2011
    Last edited: Apr 6, 2011

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    D'oh. I feel stupid. :eek:
     
  12. newbie80, Mar 21, 2011
    Last edited: Mar 21, 2011

    newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #12
    er... how do I write that?
    The method that I did was in InterfaceBuilder, I drag the button to the bar one.
     
  13. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #13
    First time code was like this:
    header file:
    Code:
    -(IBAction)start_game;
    
    main file:
    Code:
    -(IBAction)start_game{
    .............
    }
    
    And I drag the start_game from the connection to the button in InterfaceBuilder.

    Now my code like this, but I still get the warning:
    header file:
    Code:
    @interface....{
    IBOutlet UIBarButtonItem *start_game;
    }
    -(IBAction)start_game;
    
    main file:
    Code:
    -(IBAction)start_game{
    	[start_game setHidden:YES];
    }
    
    Then in InterfaceBuilder I drag another one start_game from connection to the button. So now my START button has two links from the connection. But I still get the warning UIBarButtonItem may not respond to setHidden.:confused:
     
  14. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #14
    Just create a second Toolbar and duplicate the buttons except for the ones you want "hidden" and then:

    Code:
    .h
    IBOutlet UIToolbar *toolbar2;
    
    .m
    -(IBAction)start_game{
    	toolbar2.hidden=NO;
    }
    
    viewDidLoad{
    toolbar2.hidden=YES;
    }
     
  15. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    start_game isn't a button, it's an action. Specifically, start_game is a method, and it is marked as an IBAction.

    Code:
    -(IBAction)start_game; // which is the button that I want to hide  [COLOR="Red"](misleading comment)[/COLOR]
    
    ...
    
    -(IBAction)start_game{
    	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/30.0];
    	[[UIAccelerometer sharedAccelerometer] setDelegate:self]; 
    }
    
    You connected the start_game action to a button in IB, but that doesn't mean the action is the button.

    start_game is not an object, so it doesn't respond to any messages. If it were an object, you could mark it as an IBOutlet. Important: an IBOutlet is not the same as an IBAction.

    I suggest posting the complete header, rather than isolated fragments. We need to see the IBOutlets and the IBActions.
     
  16. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #16
    My original code:
    My Header:
    Code:
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    @interface mainView : UIView <UIAccelerometerDelegate> {
    
    }
    -(IBAction)start_game;
    @end
    
    My Main:
    Code:
    #import "MainView.h"
    #import <AVFoundation/AVFoundation.h>
    
    @implementation mainView
    -(IBAction)start_game{
    	
    }
    @end
    
    What I did in InterfaceBuilder is drag the IBAction start_game from the connection to the start button(Bar Button Item - drag from object library). And this Bar Button Item is on the right side of the Navigation Bar, NOT Toolbar.
     
  17. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #17
    So you never even made an IBOutlet for your button? You could try and make another Nav Bar , NOT Toolbar.
     
  18. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #18
    Yup, originally I didn't make an IBOutlet for my startbutton. The code that you provided didn't show anything on my iphone simulator, it shows a blank screen then terminate already. __TERMINATING DUE TO UNCAUGHT EXCEPTION____

    Just now I tried to make another IBOutlet in .h:
    Code:
    IBOutlet UIBarButtonItem *startGameBtn;
    
    in .m
    Code:
    -(IBAction)start_game{
    [startGameBtn.setHidden:YES];
    }
    
    Still got the error like I mentioned in early post.
     
  19. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #19
    Right. As PhoneyDeveloper pointed out "A barbuttonitem isn't a view and it doesn't have a hidden property." That's why you should make a custom Nav Bar with a UIButton to hide or a second Nav bar to cover the first.
    Good Luck,
    Nick
     
  20. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #20
    You could also just set your leftBarButtonItem (or rightBarButtonItem, depending which side it's on) to nil, as long as you remember to keep track of the UiBarButtonItem it's set to so that you can restore it when you want to "unhide" it.
     
  21. newbie80, Mar 21, 2011
    Last edited: Mar 21, 2011

    newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #21
    Thanks everyone. I already remove the start button on my Navigation bar(Navigation bar drag from library in InterfaceBuilder). And I write the code for the button to appear on my navigation bar.My code goes like this:
    header:
    Code:
    @interface mainView : UIView <UIAccelerometerDelegate> {
    UIBarButtonItem *startGameBtn;
    }
    @property (nonatomic, retain) UIBarButtonItem *startGameBtn;
    @end
    
    main:
    Code:
    - (void)dealloc {
    	[super dealloc];	
    	[startGameBtn release];
    }
    - (void)viewDidLoad {
    	startGameBtn = [[UIBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStylePlain target:self action:@selector(lockScreen)];
    	self.navigationItem.rightBarButtonItem = startGameBtn;
    }
    
    Then I got an error: request for member 'navigation item' in something not structure or union.:( what does action:mad:selector(lockScreen) mean actually?

    2nd question: How do I include the process inside -(IBAction)start_game{ } to the code above? :confused:
     
  22. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #22
    @selector is the action being called. You want @selector(startgame).

    as for the action
    Code:
    .h
    -(IBAction)startgame;
    
    .m
    -(IBAction)startgame{ 
    
    }
     
  23. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #23
    Thanks :) Now I still looking at the error. (T_T)
     
  24. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #24
    What type is self?

    P.S. Release your button before you call super's dealloc.
     
  25. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #25
    Sorry, I don't get what you mean. I thought 'self' is load at the same page?
    And after I wrote the add button code, how is it going to know which navigation bar I'm placing on?
     

Share This Page