How can i add universal right / left UIBarButtonItem to a UINavigationController? I am trying to make a navigation based app with same left and right navigation bar button for all UIViewController
Thank You
create a base UIViewController
#interface BaseController : UIViewController
{
}
#end
#implementation BaseController
- (id)init
{
//set the universal barbuttonitem
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:yourView] autorelease];
}
#end
then all your viewController should extend from BaseController
that's done
You can add the rightbarButtonItem in this way :-
UIBarButtonItem *btnName= [[[UIBarButtonItem alloc initWithTitle:#"btnTitle" style:UIBarButtonItemStyleBordered target:self action:#selector(btnAction)]autorelease];
self.navigationItem.rightBarButtonItem = btnName;
Related
I manually embed a uitableviewcontroller inside a UINavigationController, as shown below.
navigationController = [[UINavigationController alloc] init];
jabBookModelTableViewController *vc = [[jabBookModelTableViewController alloc]init];
vc.booksPassed = booksToPass;
vc.user = user;
navigationController.viewControllers=[NSArray arrayWithObject:vc];
vc.navigationController = navigationController;
[self presentModalViewController:navigationController animated:YES];
My problem is that within my UITableViewController class I create a bar button:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Save Books"
style:UIBarButtonItemStylePlain
target:self
action:#selector(saveBooksAction)];
self.navigationItem.leftBarButtonItem = anotherButton;
and if I load 30 rows in the table, and scroll down the bar button scrolls up, so the user would then have to scroll to the top to reach the button.
How do I have the UITableViewController subclass scroll, but the UINavigationController stay in one place?
Should I somehow use a UIScrollView and put my UITableViewController subclass inside the scroll view?
UPDATE:
I removed the property I created for navigationController but I got this error:
jabFirstViewController.m:192:33: Assignment to readonly property
I want to build Grouped View in Modal View which is destination state from "+" button on the Navigation bar in main table view.
I have written this code:
-(void)viewDidLoad
{
//title
self.title = #"Set";
//addBtn
UIBarButtonItem *addBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(toggleEditing)];
self.navigationItem.rightBarButtonItem = addBtn;
[addBtn release];
}
-(IBAction)toggleEditing
{
}
create a subclass of UITableViewController named MyTableViewController
use presentModalViewController:animated: in your current viewController
-(IBAction)toggleEditing {
MyTableViewController *tableViewController = [[[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
[self presentModalViewController:tableViewController animated:YES];
}
And you will need a delegate method that tells the viewController from where you showed the modal viewcontroller that a row was selected.
How can a button be added to a navigation toolbar in an application which is navigation based?
The rootController is a tableView by default, and this is perfect. I would like to add a "share" button to the navigation controller so that I can attach it to my own method.
How is this done if the navigation bar is added in the code somewhere?
To do it in code, go to your views viewDidLoad method and create a UIBarButtonItem
This code will create a button that says share like you wanted and put it on the right hand side of the navigation bar.
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStyleBordered target:self action:#selector(share)];
self.navigationItem.rightBarButtonItem = shareButton;
[shareButton release];
}
UIBarButtonItem *shareButton = [[[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStylePlain target:self action:#selector(yourShareAction)] autorelease];
self.navigationItem.rightBarButtonItem = shareButton;
This makes an add button, but you get the idea.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:
self action:#selector(buttonPressed:)]
autorelease];
You can create an UIBarButtonItem and attach it to a root view controller's navigation item. You add a target-action pair when you initialize the UIBarButtonItem object and that action message will be sent to the target on a user tapping on the button.
So if you want the button when the table view is shown, set the UIBarButtonItem to your table view controller's navigationItem.rightBarButtomItem property.
I'm developing iphone app with UITabBarController as main view. Every ViewController in each tab is UINavigationController which must have a same button in leftBarButtonItem. Can I inherit some class from UINavigationController and override it's -(id) initWithRootViewController:(UIViewController *)rootViewController method to realize this ?
I made something like this. But this code doesn't work;
#implementation MainNavagaionController
-(id) initWithRootViewController:(UIViewController *)rootViewController {
if (self = [super initWithRootViewController:rootViewController]) {
// Set user name title
UIBarButtonItem * userNameButton = [[UIBarButtonItem alloc] initWithTitle:#"Title"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.leftBarButtonItem = userNameButton;
[userNameButton release];
}
return self;
}
#end
What is shown in the navigation bar is the navigationItem of the current viewcontroller in each navigation controller. You are one level too high when you are setting this left bar button.
I have an UITableViewController which I would like to add UIToolbar to with one button. In the
- (void)viewDidLoad;
method of UITableViewController I have:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressButton1:)];
self.navigationItem.title = #"Some title";
self.navigationItem.leftBarButtonItem = button;
}
Unfortunately I don't see the toolbar when I run my app.
Any hints? Should I do something more?
The navigationItem property of a view controller is useless if that controller is not displayed inside a UINavigationController.
If your view controller is inside a navigation controller I don't know what the problem is.
Otherwise you can use an UINavigationItem but you need to create a UINavigationBar yourself.
Either in the Interface Builder (add a UINavigationBar and add a UINavigationItem, then connect the UINavigationItem to a property outlet declared your view controller (you don't need to connect the Bar).
Or in your code :
UIBarButtonItem *item = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(pressButton1:)];
UINavigationItem* navItem = [[UINavigationItem alloc] init];
navItem.rightBarButtonItem = item;
navItem.title = #"Your title";
naviBar = [[UINavigationBar alloc] init];
naviBar.items = [NSArray arrayWithObject:navItem];
naviBar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0);
[self.view addSubview:naviBar];
[navItem release];
Your method requires an autorelease:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(pressButton1:)] autorelease];
self.navigationItem.title = #"Some title";
self.navigationItem.leftBarButtonItem = button;
}
There's nothing wrong with your code per se. Your question states you want to add an UIToolBar to your view? Really? Or do you just want to add a button to the NavigationItem for UITableView?
If you don't have to use a UITableViewController and are not using a UINavigationController in your app already, you can set your view controller up as a regular UIViewController with a toolbar and tableview.
To do this in IB, drag out a UIViewController object and add a toolbar and tableview. Hook up outlets for both and set the delegate and datasource of the tableview to Files Owner. Add any other toolbar items or buttons and give them outlets and methods if you need them for buttons, etc. In your ViewController.h file, make sure you sign it up to conform to the UITableViewDataSource and UITabBarDelegate:
#interface ViewController : UIViewController <UITableViewDataSource, UITabBarDelegate>
From there, just build out your tableview delegate and datasource methods like you normally would, and write your button action methods for any buttons you added to the toolbar.
You just didn't show the toolbar. It is hidden by default. To fix it, you just put this line of code:
self.navigationController.toolbarHidden = NO;
I tried it and it worked. Just make sure that you put in the implementation file's viewDidLoad method.