IOS Display different views from dynamically created nav bar - ios

I need to dynamically add a UIViewController that contains a nav bar with multiple buttons. When one of the buttons is pressed i need to swap the displayed view for another, while keeping the nav bar.
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
ViewControllerOne* viewOne = [[ViewControllerOne alloc] init];
[viewOne.view setFrame:appFrame];
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44.01)];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(back:)];
[buttons addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc] initWithTitle:#"Two" style:UIBarButtonItemStyleBordered target:self action:#selector(showTwo:)];
[buttons addObject:bi];
[bi release];
[tools setItems:buttons animated:NO];
[buttons release];
viewOne.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
m_navViewController = [[UINavigationController alloc] initWithRootViewController:viewOne];
[m_navViewController.view setFrame: appFrame ];
[self addSubview: m_navViewController.view];
Now when someone presses the Two button i would like to remove viewOne and add a ViewControllerTwo object
- (void) showTwo:(id)sender{
ViewControllerTwo* viewTwo = [[ViewControllerOne alloc] init];
[viewOne.view setFrame:[[UIScreen mainScreen] applicationFrame]];
// remove viewOne from m_navViewController and add viewTwo
}
In other words i want to show different views by pressing one of items in the nav bar but keep the same nav bar for all views.
Please note the nav bar will actually hold five buttons. I have simplified it for explanation purposes.
Thanks in advance.

You could use the nav controller template; add the buttons to the nav bar in IB (or in the code as you do above), then when one of them is tapped you push on the corresponding view controller.

Related

UIPickerView Only Responds Within Area of Tab Bar

I'm updating my app to iOS 8 and trying to recreate the UIPickerView inside a UIActionSheet with UIAlertController as described here. Using the answer from Nada Gamal, I was able to recreate the functionality, but the UIPickerView only responds within the tab bar (even though the tab bar is hidden behind the picker view). The tab bar controller is the root view controller.
-(void)loadPickerView{
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.backgroundColor = [UIColor whiteColor];
[pickerView selectRow:pickerViewRow inComponent:0 animated:NO];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
pickerToolbar.tintColor = self.navigationController.navigationBar.tintColor;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
UIAlertController * searchActionSheet=[UIAlertController alertControllerWithTitle:#"" message:#"" preferredStyle:UIAlertControllerStyleActionSheet];
[ searchActionSheet.view setBounds:CGRectMake(8, 208, self.view.frame.size.width, pickerView.frame.size.height+pickerToolbar.frame.size.height)];
[searchActionSheet.view addSubview:pickerToolbar];
[searchActionSheet.view addSubview:pickerView];
[self presentViewController:searchActionSheet animated:YES completion:nil];
}
The presentViewController in the last line seems to be the issue. I've tried many different ways to get the pickerView to respond outside the tab bar area, including:
self
self.tabBarController
self.navigationController (pickerView doesn't show up at all)
[[UIApplication sharedApplication] keyWindow].rootViewController
The other option is to make a separate view controller for this, but I'm concerned that won't work either because UIAlertController is a subclass of UIViewController, but perhaps this is an issue with adding subviews to UIAlertController.

ios6 UINavigationBar with more than 2 buttons

in my app i have a uinavigationbar with 3 buttons, but the code i used for ios5 does not seem to be good for ios6 anymore.
You can see a wrong-colored rect underneath my custum-view with two buttons on the right side.
heres the code i used, i found it somewhere on stackoverflow:
UIToolbar *tools = [[[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 110.0f, 44.01f)] autorelease]; // 44.01 shifts it up 1px for some reason
tools.clearsContextBeforeDrawing = NO;
tools.clipsToBounds = NO;
tools.translucent = self.headerbar.translucent;
tools.barStyle = self.headerbar.barStyle;
tools.backgroundColor = self.headerbar.backgroundColor;
tools.tintColor = self.headerbar.tintColor;
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create a standard refresh button.
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithTitle:#"heute" style:UIBarButtonItemStylePlain target:self action:#selector(showThisWeek)];
bi.style = UIBarButtonItemStyleBordered;
bi.width = 0;
[buttons addObject:bi];
//[bi release];
// Create a spacer.
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(showThisWeek)];
[buttons addObject:bi];
//[bi release];
// Add profile button.
bi = [[UIBarButtonItem alloc] initWithTitle:#"vor" style:UIBarButtonItemStylePlain target:self action:#selector(showNext)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
//[bi release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
//[tools release];
header.rightBarButtonItem = twoButtons;
[header.leftBarButtonItem setAction:#selector(showPrevious)];
can someone hint me to a more elegant solution that works fine on both ios5 and ios6?
cheers
lukas
Did you try leftBarButtonItems and rightBarButtonItems? You can add array of buttons in left and right side nav bar using this.
If you use this, there will be no need for UIToolbar and their subviews.
Have you tried bar.style = -1?

UIToolbar Items are not showing in popover

I am trying to show a custom tableview inside a popover view in my iPad App. This is working fine. I wanted to add buttons on toolbar (at the bottom of tableview). Toolbar is shown empty. Any suggestion?
Note that following code is triggered when user touches the button in main view controller.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.customTableViewController];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"item 1" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"item 2" style:UIBarButtonItemStylePlain target:nil action:nil];
[navigationController setToolbarHidden:NO];
navigationController.navigationBar.topItem.title = #"Some Title";
NSArray *array = [[NSArray alloc] initWithObjects:item1, item2, nil];
[navigationController setToolbarItems:array];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
self.popoverController = popover;
popoverController.delegate = self;
[popoverController presentPopoverFromRect:[sender bounds] inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
I'm hitting a similar issue, the tableview shows fine in the popover, but the buttons below are not visible. I'll let you know if I figure something out.
EDIT: My issue was that I was re-sizing the popover, and the buttons were being pushed out of the view. I fixed this by changing the autosizing to lock the position relative to the bottom of the frame. To do this, look at your xib in interface builder, go to the Ruler tab on the upper right and use the Autosizing GUI. For mine this meant that only the bottom anchor was selected on the GUI.
On ipad apps, you've got to set toolbar items to the "topViewController" (yes this is counter-intuitive).
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:catView];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"item 1" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"item 2" style:UIBarButtonItemStylePlain target:nil action:nil];
[nav setToolbarHidden:NO animated:YES];
// WRONG: [nav setToolbarItems:[NSArray arrayWithObjects:addButton, nil]];
// CORRECT (for ipad apps):
[nav.topViewController setToolbarItems:[NSArray arrayWithObjects:item1, item2, nil] animated:NO];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:nav];
See:
http://www.kevatron.co.uk/tag/uipopovercontroller/

How to add buttons to the top bar of a detail view from a UITableView

When an item from a UITableView is selected, a detail view is loaded which has a banner bar at the top with a back button on it to navigate back to the table.
How do I add other buttons to that banner bar?
As Andrew said, you can add custom views to the navigation bar. For example, if you are looking to add multiple buttons to the right side of the navigation bar, you can do something like this:
// right side of nav bar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 106, 44)];
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelAction:)];
cancelButton.style = UIBarButtonItemStylePlain;
[buttons addObject:cancelButton];
[cancelButton release];
[toolbar setItems:buttons animated:NO];
toolbar.barStyle = -1;
[buttons release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];
Make sure to adjust the width (106 above) if you need more or wider buttons, and then provide methods for the selectors (deleteAction: and cancelAction: above).
From your description, it sounds like you have a UINavigationController correctly set up. That "banner bar" you are describing is properly called a navigation bar.
From within the detail view, you can use:
UIBarButtonItem* button = [[[UIBarButtonItem alloc] initWithTitle:"HiMom" style:UIBarButtonItemStylePlain target:self action:#selector(onHiMom:)] autorelease];
self.navigationItem.rightBarButtonItem = button;
You can also add custom views (instead of a button) plus a few other settings. I suggest poking around the documentation for UINavigationItem, UINavigationBar, and UIBarButtonItem for ideas.

How to get a UIBarButtonSystemItem on a UIToolBar to take me to another view

Accept my early apologies as totally new to this so my terminology may not be accurate, hope you can understand what it is that i'm asking.
I am having issues getting a UIBarButtonSystemItem to take me back to a different view when pushed. I have defined a UIToolBar with a UIBarButtonSystemItem on it which compiles and works perfectly. However, when the button is pushed the app crashes.
This is the code i have used to define the UIToolBar and UIButton:
//create toolbar using new
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 410, 320, 50);
//Add buttons
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(pressButton1:)];
I have then added flexitem to this and added the button to the array using the following code:
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, nil];
This is where I fall down and I have attempted to get the button to take me to a previous view when pushed by using the following code:
-(void)pressButton1:(id)sender {
BMR_TDEE_CalculatorViewController *controller1 = [[BMR_TDEE_CalculatorViewController alloc] initWithNibName:#"BMR_TDEE_ViewController" bundle:nil];
controller1.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller1 animated:YES];
[controller1 release];
}
Like I said, this just crashes my simulator. It compiles without issue but Im guessing the code is fundamentally wrong as i am new to this. Any help explained dumbed down would be fantastic ;-)
Thanks All
This:
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(pressButton1:)
should be:
UIButton *button=[[UIButton alloc] init];
button.frame=CGRectMake(0, 0, 65, 32);
[button addTarget:self action:#selector(pressButton1:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
For the (void)pressbutton code it should now be:
-(IBAction)pressButton1:(id)sender {
BMR_TDEE_CalculatorViewController *controller = [[BMR_TDEE_CalculatorViewController alloc] initWithNibName:#"BMR_TDEE_CalculatorViewController" bundle:nil];
[self.navigationController presentModalViewController:controller animated:TRUE];
}

Resources