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.
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
When pressing infobutton it is not displaying ModalView
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(displayModalView:)];
- (void)displayModalView
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[Infoviewcontroller alloc] init];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[_window addSubview:navigationController.view];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
Anyone can help me please what is the problem in this.
Thanks a lot in advance for helping me out
In your question you didn't specify how you created your objects (the toolBar and the buttons on it), are you creating them from Xcode by dragging and dropping or from pure code, therefore I will try to point out the common issues for both cases.
First, I am assuming that you are using Xcode and dragging the components that you like. In this case you need to create in the .h file an Outlet that will be linked to the button on the bar as follows:
#interface yourViewController : UIViewController
{
UIBarButtonItem *barButton;
}
#property (nonatomic, retain) IBOutlet UIBarButtonItem *barButton;
- (void) barButtonPress;
Notice that I added a function that will handle the bar button press. Now you need to link this Outlet to the bar button item, simply in Xcode in the Connection Inspector where it says New Referencing Outlet drag to the File's Owner box (the yellow cube).
Now in the viewDidLoad add the following:
[barButton setTarget:self];
[barButton setAction:#selector(barButtonPress)];
This code will link your bar button to the function that you want to be called when you press it. Now for the view that you like to view Modal, I assume that you already #import it also in the .h file, lets call it MyViewModal.
Inside the function that will be called when you press the bar button:
- (void) barButtonPress
{
MyViewModal *myViewModal = [[MyViewModal alloc] initWithNibName:#"MyViewModal" bundle:nil];
[self presentModalViewController:myViewModal animated:YES];
}
That's all, it will be displayed in Modal View. Keep in mind the allocating the new view is done based on your needs, here I did the simplest case just for illustration.
UPDATE: If not using Xcode
If you are not using Xcode then you should have a toolbar already defined say it is named myToolBar. To add buttoms to the tool bar we use the myToolbar.items way therefore we need to prepare the buttons with their targets before adding them. Here is a workflow:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(barButtonPress) forControlEvents:UIControlEventAllEvents]; //same function as above
UIBarButtonItem *btn = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];
myTool.items = [NSArray arrayWithObjects:btn, nil];
This should do it for you.
My application fit inside a UINavigationController.
Simply I setted up a MyUIViewController as rootViewController of navigationController and than I set up a customTitleView for the navigationItem of myViewController inside viewDidLoad.
Now when I push a newViewController I expect to see the previous customTitleView (as described by Apple NavigationController reference) but it doesn't.
What's wrong?
A little part of code below and the Apple UINavigationController Reference
"If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller’s navigation item."
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationItem setTitleView:customTitleView];
}
Maybe the "default title view" means "nothing"? I interpreted it as the previous titleView.
EDIT
I'm trying a work around but I have some other issues.
The work around is:
In every ViewController that I push inside the NavigationController I setup in viewDidLoad the titleView getting it from the rootViewController
UiView *originalContainer = [[[self.navigationController.viewControllers objectAtIndex:0] navigationItem] titleView]
I create a newTitleView and I put inside the originalContainer.subviews (UIButton) cause I need the Target action from it.
All works perfectly for the first pushed controller but if I push another viewController in the stack (the second one pushed) I loose every reference to the navigationController. Every instance variables are nil.
This values are getted inside the viewDidLoad
firstViewControllerPushed.navigationController = (UINavigationController*)0x6693da0
firstViewControllerPushed.parentViewController = (UINavigationController*)0x6693da0
secondViewControllerPushedFromFirstViewControllerPushed.navigationController = 0x0
secondViewControllerPushedFromFirstViewControllerPushed.parentViewController = 0x0
It seems that the secondViewControllerPushed lives nowhere!!
How it's possible?
I double checked that I correctly push the viewController instead of present it modally
Couse this issue I'm not able to make a right setup of the newTitleView for the secondViewControllerPushed.
This is a hack in a half but I feel it gives you a bit more control in the viewController you are working in. So below is a small method I have setup and then I just call it in viewDidLoad - [self setUpNavBar];
And using [UIButton buttonWithType:101] is perfectly fine as it passed Apple Validation.
- (void) setUpNavBar
{
[self.navigationController setNavigationBarHidden: NO animated:YES];
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.title = #"Settings";
UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Logout"
style:UIBarButtonItemStylePlain target:self
action:#selector(logoutAction)] autorelease];
}
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.
I am currently adding a UISegmentedControl to the toolbar in the navigation controller programmatically (as seen below).
This approach works fine, I have my UISegmentedControl, it fires the selector that I have setup no problems.
Problem is - I would like to use the selectedIndex of this control in order to query my data model and present a different view of data for each 'segment' - but I am having trouble getting the selectedIndex.
In my travels I have been consulting the 'Top Songs' example code provided by Apple.
In this code they build up their interface via UISegmentedControl object in the view controller and IB. In doing so they can access the UISegmentedControl's selectedIndex. I am adding mine programmactically and do not have this freedom.
'SHOULD' I have a UISegmentedControl defined in my view controller? If so, if I want to continue building my menu programmactically, how do I go about accessing the information from the control buried within the navigation controller's UIToolBar?
I'm clearly missing something basic. Any assistance is always greatly appreciated :)
[self.navigationController setToolbarHidden:NO];
// Set up the edit and add buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
NSArray *tabitems = [NSArray arrayWithObjects:#"ONE", #"TWO", #"THREE", #"FOUR", nil];
UISegmentedControl *tabs = [[UISegmentedControl alloc] initWithItems:tabitems];
[tabs addTarget:self
action:#selector(pickedSegment:)
forControlEvents:UIControlEventValueChanged];
tabs.segmentedControlStyle = UISegmentedControlStyleBar;
tabs.frame = CGRectMake(60, 8, 180, 30);
tabs.selectedSegmentIndex = 0;
//[self.navigationController.navigationBar addSubview:tabs];
[self.navigationController.toolbar addSubview:tabs];
[tabs release];
You need to have tabs defined in your .h file -
#interface YourViewController : UIViewController
....
UISementedControl *tabs;
....
#end
....
#property (nonatomic, retain) UISegmentedControl *tabs;
Then, after the [tabs release]; line, you should still be able to access the object, as it is a retained property - access the selectedItemIndex as normal.