In my tableview xib i couldn't be able to show navigation bar on run time? My coding as follows
- (void)viewDidLoad
{
ar=[[NSMutableArray alloc]initWithObjects:#"When im happy..i...",#"When im sad.. i...",#"When i'm mad.i...",#"My worst habbit is....",#"The best thing about me is...",nil];
[super viewDidLoad];
self.title=#"Welcome";
self.navigationItem.leftBarButtonItem=self.editButtonItem;
}
The table view doesnt showing the navigation bar.Please help me out
Are you creating self.editButtonItem?
Use this code:-
[self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc]initWithTitle:#"Test" style:UIBarButtonItemStyleBordered target:self action:#selector(test:)]autorelaese]];
Related
I'm new on iOS development, i m trying to show a user profile inside a view, and in the same time i want to give a user a possibility to edit his profile by tapping an "Edit" button on the UINavigationBar like shown on Apple web site : Enabling Edit Mode in a View Controller
I tried to find a tutorial explaining all this, but i didn't fin anything. can somebody help me plz, by giving me a link for a tutorial, or a sample code ?
PS : I m using storyboard.
Thanks a lot !
Here and here are a couple of examples for a UITableview
The concept is the same. You add a UIBarButtonItem and change the current mode of the tableView and the status(text) of the buttonItem to show the editing dashes and other content if you choose.
Here is a simple edit mode button press to send the tableView into edit mode to allow for easy deleting. You can also
- (IBAction)editPressed:(id)sender
{
// If the tableView is editing, change the barButton title to Edit and change the style
if (_theTableView.isEditing) {
UIBarButtonItem *newButton = [[UIBarButtonItem alloc]initWithTitle:#"Edit" style:UIBarButtonSystemItemDone target:self action:#selector(editPressed:)];
self.navigationItem.rightBarButtonItem = newButton;
_buttonEdit = newButton;
[_theTableView setEditing:NO animated:YES];
}
// Else change it to Done style
else {
UIBarButtonItem *newButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonSystemItemEdit target:self action:#selector(editPressed:)];
self.navigationItem.rightBarButtonItem = newButton;
_buttonEdit = newButton;
[_theTableView setEditing:YES animated:YES];
}
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
// You could do other things in here based on whether editing is true or not
}
You can set default edit button to you navigationItem barbutton inside viewDidLoad as shown below.
-(void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
As give in the apple document
editButtonItem - Returns a bar button item that toggles its title and associated state between Edit and Done. The default button action invokes the setEditing:animated: method.
Override setEditing:animated: in your view controller as shown below.
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
}
You can make use of bool variable editing to achieve your requirements.
I'm trying to create a custom navigation bar with title and two custom baritems.
What I've done so far:
1. subclass a UINavigationBar
2. override -(void) drawRect:(CGRect)rect method, to draw image as a background
Now I get stuck when making right bar button item.
In my CustomBar class I've overriden pushNavigationItem method as follows:
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated {
[super pushNavigationItem:item animated:NO];
item.rightBarButtonItem.customView.frame = CGRectMake(250, 5, 60,30);
}
and in my view controller I've done something like:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationController.navigationItem.rightBarButtonItem = barButton;
[self.navigationController.navigationBar pushNavigationItem:self.navigationItem animated:NO]
but I always get SIGABRT when calling
[super pushNavigationItem:item animated:NO];
If I leave that line out, everything runs fine, but there is no button in the navigation bar.
What is the proper way to add custom bar button item to custom navigation bar?
EDIT:
For clarification, I've added an image.
This is what I need.
I'm trying to create the logout button on the right
Have you tried removingnavigationController while setting the item ? I used to do that for setting the title, never used to get the title.
Try [[self navigationItem] setRightBarButtonItem:yourButton];
In your case :
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = barButton;`
I am trying to show the UIToolBar in the RootView of a UISplitView application, the code is the following:
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refresh:)];
self.toolbarItems = [NSArray arrayWithObjects:refreshItem, nil];
[refreshItem release];
However, what I see is:
There's black bar on top (I don't know where this came from, I don't need this) also the bar at the bottom, is there a way to resize it?
What I want is to get something like this:
Using something like this you can add a bar button item to the top of the controller:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
self.navigationItem.leftBarButtonItem = refreshItem;
[refreshItem release];
You will make the button appear in the main view controller's title bar, as it's meant to be.
If you want to make the button appear in the bottom of the navigation controller you could try using this approach, instead:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
[self setToolbarItems:[NSArray arrayWithObjects:refreshItem, nil animated:YES]];
[self.navigationController setToolbarHidden:NO]; //optional, don't remember if it's required ...
[refreshItem release];
For this piece of code to work correctly the side controller has to be a UINavigationController, otherwise you wouldn't be able to create and handle the toolbar. I tried this approach in a clean project and the toolbar renders perfectly.
I had the same issue and Just fixed it, Due to moving the code out of the Viewdid Load to lower down the Page,
As I had previous put in
- (UIBarButtonItem *)barButtonItem {
Moving the Code You used to under that, Worked and fixed the issue
Stewart
Just a note for anybody else who stumbles upon this question. I was having the same issue as adit. The problem turned out to be I was setting up and unhiding the toolbar in the viewDidLoad method instead of the viewWillAppear method. Those gaps are caused by setting up the toolbar before the view knows it's being displayed in landscape mode.
The safest and easiest solution is to setup the UINavigationController to display the toolbar and navigation bar in Interface Builder.
If it looks as expected in IB, it is very unlikely it will change at run-time.
If the toolbar is to be shown/hidden when navigating you should add the cod to do so in viewWillAppear: and allways call the super implementation, or unexpected things may occurs. Something like this tends to give the best results in a consistent manner:
-(void)viewWillAppear:(BOOL)animated;
{
[super viewWillApplear:animated];
[self.navigationController setToolbarHidden:NO
animated:animated];
}
Also make sure to show/hide the toolbar as need in viewWillAppear: for all view controllers in your navigation stack for best result.
I am new in objective C. I don't know much more things about this. I am practicing on navigation controller. The problem is that whatever methods I am putting in action:#selector in shows SIGABRT error.
could you clarify me which types of methods i can put in action:#selector section.
Though I know it's a silly question but I think these will clear my concept over navigationViewController.
thank you.
- (void)viewDidLoad
{
UISearchBar *search=[[UISearchBar alloc] init];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchBarShouldBeginEditing:)];
[super viewDidLoad];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
UISearchBar *search=[[UISearchBar alloc] init];
[search resignFirstResponder];
return YES;
}
Try moving your '[super viewDidLoad]' to the beginning of the viewDidLoad method, not the end. YOu need to make sure the controls exist before you add the right bar button item.
When creating a new nav-based iphone app, how do you add a button to the nav bar?
I've tried editing the rootviewcontroller.xib and adding a nav bar and nav item and bar button item, but it doesn't seem to make any difference.
Is there a simple way of achieving this through IB, or do i have to duck into code somewhere?
Thanks
Unfortunately this requires a little bit of code. Create your UIBarButtonItem in interface builder and hook it up to an IBOutlet in your view controller. Then, in viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
// Assuming "myButton" is the UIBarButtonItem.
self.navigationItem.leftBarButtonItem = myButton;
}
Assigning to rightBarButtonItem works similarly. See the UINavigationItem Class Reference for more information.
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];