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.
Related
I need to create the UIToolbar and the UIBarButtonItem programmatically. I'm using Storyboard for the layout.
How is now a new view opened by pressing the toolbar button which was programmatically created? (I know how to do it with Segue when the button is created in the Interface Builder but I can't use this approach here).
First of all, create the UIBarButtonItem using the following code :
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonItemStylePlain target: self action: #selector(showNewView)];
Then,
-(void)showNewView
{
UIStoryboard *story =[UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *newViewController = [[postDetailViewControllerIphone alloc]init];
newViewController=[story instantiateViewControllerWithIdentifier:#"newView"];
[self presentViewController:newViewController animated:YES completion:nil];
}
This code will allow the barButtonItem to open the new view controller.
If u haven't added barButtonItem to the toolbar then do this after creating barButtonItem.
NSArray *toolBarItems = [[NSArray alloc] initWithObjects:barButtonItem,nil];
[self.toolBar setItems:bottomtoolBarItems];
This will add your barButtonItem to the toolbar.
When you create your UIBarButtonItem make sure you set the target and action. Then in the called method, present your next view controller.
Create the bar button item.
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonItemStylePlain target: self action: #selector(presentVC:)];
Present the view controller.
-(void)presentVC:(id)sender
{
[self presentViewController: otherViewController animated: YES completion:^{
// COMPLETION BLOCK
}];
}
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 have the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath(NSIndexPath*)indexPath
{
audiChassisInputViewController = [[myAudiChassisInputViewController alloc] init];
[self.navigationController pushViewController:audiChassisInputViewController animated:YES];
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:#"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:#selector(popViewControllerAnimated:)];
[self.navigationController.navigationBar.topItem setLeftBarButtonItem:retourButton];
[self.navigationController.navigationBar.topItem setTitle:#"Chassis Input"];
[retourButton release];
[audiChassisInputViewController release];
}
and this workes...the new view is showed.
in the new view:
myAudiChassisInputViewController.h
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
chassisInputTextView.layer.cornerRadius = 15;
chassisInputTextView.clipsToBounds = YES;
[chassisInputTextView becomeFirstResponder];
UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(chassisOkPressed)];
[self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton];
[okButton release];
}
I have no error, but there is no right bar button shown.Anyone, any idea why?
Change this line:
[self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton];
with this line:
[[self navigationItem] setRightBarButtonItem:okButton];
The thing is, by the time viewDidLoad is executed, the top item of the navigation bar (self.navigationController.navigationBar.topItem) is still pointing to the navigation item of the back view controller.
The back view controller is the one that used to be the top view controller before the current top view controller was pushed onto the stack ([[viewControllers objectAtIndex:[viewControllers count] - 2] navigationItem]). The following snippet shows how the top item of the navigation bar is still pointing to the navigation item of the back view controller in viewDidLoad and it is for illustration purposes only:
// the view controllers currently on the navigation stack
NSArray *viewControllers = self.navigationController.viewControllers;
// The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.
UIViewController *backViewController = [viewControllers objectAtIndex:[viewControllers count] - 2];
// get the navigation item of the back view controller
UINavigationItem *backNavigationItem = backViewController.navigationItem;
UINavigationItem *topItem = self.navigationController.navigationBar.topItem;
if (backNavigationItem == topItem) {
NSLog(#"This gets logged to the console");
}
Go to your
myAudiChassisInputViewController.m file
place following code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:#"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:#selector(popViewControllerAnimated:)];
UIBarButtonItem *itemOkey=[[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(chassisOkPressed)];
self.navigationItem.rightBarButtonItem=itemOkey;
self.navigationItem.leftBarButtonItem=retourButton;
}
I have the valid output as follows that you want to have
Hope it helps to you.
If you have xib file of your class, then add the navigation controller and add the navigation bar and under that add the UIBarButton.
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.