Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
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.
 
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 ;)
 
Last edited by a moderator:
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 ;)

Hi jnoxx,

I got an error: request for member "setHidden" in something not a structure or union. :(
 
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
 
I still got the same thing error: request for member "Hidden" in something not a structure or union. :confused:
What type is startGame defined as?

Capitalization etc... Has to be exactly the same as your object.

Use start_game instead of startGame. (assuming your first post is correct.
start_game is a method name and not a UIView subclass (if the first post is correct). ;)
 
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.
 
Code:
[startGame setHidden:YES];
;)

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.
 
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.

er... how do I write that?
The method that I did was in InterfaceBuilder, I drag the button to the bar one.
 
Last edited:
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
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:
 
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;
}
 
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.
 
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.
 
So you never even made an IBOutlet for your button? You could try and make another Nav Bar , NOT Toolbar.

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.
 
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
 
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.
 
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:
 
Last edited:
What type is self?
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.