I want to custom the backBarButtonItem in the navigationItem,
here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:back];
}
but it didn't work while I used the leftBarButtionItem can work.
I don't know ,can anybody give me an answer?
- (void)viewDidLoad
{
[self.navigationItem setHidesBackButton:YES];
UIBarButtonItem *cancelNavButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissController)];
[self.navigationItem setLeftBarButtonItem:cancelNavButton];
}
- (void)dismissController
{
[self.navigationController popViewControllerAnimated:YES];
}
You shouldn't have to create the back button yourself if you are pushing your view controllers using the UINavigationController's pushViewController:animated: method. This will push the new ViewController on the NavigationControllers stack and give you an automatic back button with the title of the previous ViewController.
The reason your button doesn't work is the target: and action: parameters are nil you could bind them to the UINavigationController's popViewControllerAnimated: method to achieve the same action as the default back button.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:[self navigationController]
action:#selector(popViewControllerAnimated:)
];
[[self navigationItem] setBackBarButtonItem:back];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.leftBarButtonItem = back;
}
Related
I am new at iOS Development.I Have UINavigationController as rootviewcontroller for UIWindow.In the subclass i added UITabbarController programatically.I given default Tabbarcontroller selection as first controller. Here i am trying to add 3 buttons to the navigationbar .
Can any send me the corrent code.
Thanks In advance
To add more than one button on the left or the right side you have to call one of the following methods:
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated
- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated
The array items contains instances of the UIBarButtonItem class. You can instantiate them like this
UIBarButtonItem *FirstButton = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(buttonTapped:)];
Then you have to implement the selector buttonTapped: which is called when the button has been tapped.
-(void)buttonTapped:(UIBarButtonItem *)button
{
// do the things that should happen when the button is pressed
}
If you don't want to get them on the left or right side it is also possible to create a UIView by yourself containing the buttons and set the view to be the titleView of the UINavigationItem. This effect is similar to the buttons in the Facebook App
[self.navigationItem setTitleView:yourView];
Here is simple code for add UIButton on UINavigationBar
In Following code you can add Button With FlexibleSpace
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnFirst = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:#selector(FirstTapped:)];
[barItems addObject: btnFirst];
UIBarButtonItem *btnSecond = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:#selector(SecondTapped:)];
[barItems addObject:btnSecond];
UIBarButtonItem *btnThird = [[UIBarButtonItem alloc] initWithTitle:#"Third" style:UIBarButtonItemStyleBordered target:self action:#selector(ThirdTapped:)];
[barItems addObject: btnThird];
self.navigationItem.rightBarButtonItems = barItems;
Button Related Methods
-(void) FirstTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) SecondTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) ThirdTapped:(UIBarButtonItem *)sender{
//perform your action
}
NOTE : self.navigationItem.rightBarButtonItems Works only in iOS 5 or Latter.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
// here is custom button setup //
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customButton];
[self.navigationItem setLeftBarButtonItem:item animated:YES];
I have created button for home like this:
UIBarButtonItem * addButton=[[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleBordered target:self action:#selector(GoToHome:)];
[navItem setLeftBarButtonItem:addButton];
GoToHome function has only one line i.e:
[self.navigationController popToRootViewControllerAnimated:YES];
but when i click the Home button it shows no action (not working)
try this inside your GoToHome method
[self.parentViewController.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
have you connected the button code with the IB button? that can happen a lot and with everyone..!!
Try this,
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStylePlain target:self action:#selector(GotoTopScreen)];
self.navigationItem.rightBarButtonItem = addButton;
-(void)GotoTopScreen{
[self.navigationController popToRootViewControllerAnimated:YES];
}
The best way to create a navigation controller is to create it in appDelegate and access it as appDelegate.navigationController. Then those nil object issues do not come.
Use this method:
UIBarButtonItem *flipButtons = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered target:self
action:#selector(GotoTopScreen)];
self.navigationItem.leftBarButtonItem = flipButtons;
[flipButtons release];
-(void)GotoTopScreen
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
I have an app with a navcontroller created in the appdel. Each vc pushed in has a block of code in the viewdidload that sets up the toolbar. The toolbar is always the same. Is there a way for me to just create this code once - and not put it in every vc?
[self.navigationItem setHidesBackButton:YES];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleBordered target:self action:#selector(backClicked)];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *storyBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Sto" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
storyBtnItem.tag = 1;
UIBarButtonItem *renderBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Ren" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
renderBtnItem.tag = 2;
UIBarButtonItem *amenBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Ame" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
amenBtnItem.tag = 3;
UIBarButtonItem *availBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Availability" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
availBtnItem.tag = 4;
UIBarButtonItem *eopBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Eq" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
eopBtnItem.tag = 5;
UIBarButtonItem *stkBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"St" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
stkBtnItem.tag = 6;
UIBarButtonItem *movBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Fi" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
movBtnItem.tag = 7;
NSArray *items = [NSArray arrayWithObjects:flexibleSpaceLeft, stoBtnItem, reBtnItem, ameBtnItem, avaBtnItem, eBtnItem, stBtnItem, mvBtnItem, nil];
[self setToolbarItems:items];
[self.navigationController.toolbar setTintColor:[UIColor colorWithRed:79.0/255.0 green:145.0/255.0 blue:205.0/255.0 alpha:1.0]];
Just do vc.toolbarItems = self.toolbarItems (where vc is the view controller to be pushed) in the method where you are pushing the next view controller.
eg:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *vc = [UIViewController new];
vc.toolbarItems = self.toolbarItems
[self.navigationController pushViewController:vc animated:YES];
[vc release]; // if not using ARC
}
Also, you don't need to do this in the -viewDidLoad method, setting the navigation items and toolbar items does not require the view to be loaded and can thus be done in your init or awakeFromNib method. If you do it in -viewDidLoad you are potentially setting the items multiple times.
The way i usually handle those sort of things is to create a base view controller, and the code to setup the toolbar will be in the base view controller, and all other view controllers inherit from this base controller.
You could make an abstract sub-class for your viewControllers that has this function in its ViewDidLoad. Then you just adjust it in one place.
For example, make a view controller class called myMasterViewController. All you have to set up in it is the viewDidLoadMethod. Then in your other controllers make them inherit from myMasterViewController instead of UIViewController. Make sure you have [super viewDidLoad]; in your other VC's.
Here in my application i created a leftbarbuttonitem programatically in viewWillAppear methos, i have two click twice to work this, my code is as follows.
Code for creating UIBarButtonItem(left bar button)
goingBackButton = [[UIBarButtonItem alloc] init];
goingBackButton.title = #"Back";
goingBackButton.target = self;
goingBackButton.action = #selector(backAction);
self.navigationItem.leftBarButtonItem = goingBackButton;
[goingBackButton release];
Action code
- (IBAction) backAction {
NSLog(#"Inside the backAction of uploadViewController");
[self.navigationController popViewControllerAnimated:YES];
NSLog(#"Inside the backAction1 of uploadViewController");
}
Try the below code:
UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithTitle:#"back" style:UIBarButtonItemStyleDone target:nil action:#selector(back)];
self.navigationItem.leftBarButtonItem = bar;
[bar release];
-(void)back
{
[self.navigationController popViewControllerAnimated:YES];
}
This doesn't seem to be working. What am i doing wrong?
-(void)awakeFromNib{
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showNewEventViewController)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
NSLog(#"awaked");
[rightBarButtonItem release];
}
my guess is, that you add the UIBarButtonItem to the wrong object!
you need to add it, to the rootViewController (instead to the UINavigationController, as you probably did)
YourRootViewController *theRootController = [[YourRootViewController alloc] init];
UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismiss)];
theRootController.navigationItem.rightBarButtonItem = btnCancel
[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
I would normally put this code in the viewDidLoad method rather than the awakeFromNib method; I'm not sure if that's where your problem lies. What does "not working" mean?
Try this instead:
- (void) initUI {
UIBarButtonItem *btnCancel = [[[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(dismiss)]autorelease];
self.navigationItem.rightBarButtonItem = btnCancel;
//[btnCancel release]; no need to explicitly release the item
}