Add UIToolbar to UIViewController - iphone

How to add UIToolbar to the below UIViewController.
PageOneViewController *viewController = [[PageOneViewController alloc] init];
[self presentModalViewController:viewController animated:YES];
[viewController release];
This UIViewController is the view of the mainviewcontroller and UIToolbar is in the mainviewcontroller. So basically when this view is loaded i want UIToolbar to load as well.
Can someone give me an idea

Just add the UIToolbar as a subview of the PageOneViewController:
UIToolbar *toolbar = [[UIToolbar alloc] init];
[viewController addSubview:toolbar];
[toolbar release];
toolbar.frame = // Set this to position the toolbar correctly
[self presentModalViewController:viewController animated:YES];

Related

Nav Bar button loading random view before loading appropriate view

I have a back button on my answer view controller (a view controller that displays answers) if the user hits the back button I created it switches to a view that has the title of "Back" and just an empty tableview, before switching back to my main view of where all the questions to be answered are displayed. Why is this happening? Its a very brief thing, but definitely noticeable!
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 48)];
navBar.delegate = self;
UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:#"Back"];
[navBar pushNavigationItem:backItem animated:NO];
UINavigationItem *topItem = [[UINavigationItem alloc] initWithTitle:#"Question"];
[navBar pushNavigationItem:topItem animated:NO];
topItem.leftBarButtonItem = nil;
[self.view addSubview:navBar];
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
ViewController *controller = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
return true;
}
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
ViewController *controller = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
You're creating your own UINavigationBar and UINavigationItem instances and you probably shouldn't be. The situation you describe is exactly what a UINavigationController is for. When using a UINavigationController it creates the UINavigationBar and each UIViewController that you show on screen (push into the navigation controller) has its own UINavigationItem (with the title taken from the title of the view controller).
The reason you get an empty 'view' titled "Back" is that you're creating it:
UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:#"Back"];
[navBar pushNavigationItem:backItem animated:NO];
Dispense with all of this, create a UINavigationController and make your question view controller it's root view controller, then add the navigation controller to the screen. Then when a question is answered, push the answer view controller:
[self.navigationController pushViewController:answerViewController animated:YES];

How to add UINavigationBar in an UIViewController?

I have an UIViewController class(Say it is XXX). I present this view controller as modally by the code..
XXX *xxx = [ [XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];
I want to add a navigation bar on the top of the XXX view. So I used UINavigationBar object in XXX's loadView method.
UINavigationBar *navBar = [ [UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navBar];
[navBar release];
But, It throws an error as "EXC_BAD_ACCESS". Any help...?
Thanks
OPTION-1:
Try adding navigation bar from the XIB of viewController called XXX.
OPTION-2:
Add a UINavigationController and present it modally.
Replace your code :
XXX *xxx = [[XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];
with this code:
XXX *xxx = [[XXX alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigation animated:YES];
[navigation release];
Hope this helps you.
Replace your code with:
XXX *xxx = [[ [XXX alloc] init]autorelease];
[self presentModalViewController:xxx animated:YES];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigation animated:YES];
[navigation release];
I think it will solve your "EXC_BAD_ACCESS" problem.
you can try this by adding toolbar at the top of the view. In many cases i have seen for poping MODAL controller this is nice solution. but if you want to navigate more controllers from MODAL controller then you should use UINavigationController.
you do it like this:
XXX *xxx = [[XXX alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigationController animated:YES];
[xxx release];
[navigationController release];

How to show UIToolbar in the UIView

MainViewController have UIToolbar and when button is pressed on the UIToolbar, it displays view i want to show the UIToolbar in all my views.
I set this command in display view action
[self.navigationController setToolbarHidden:NO animated:NO];
but this statement is not helping in displaying UIToolbar in the view
- (void)displayviewsAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
[self.view addSubview:viewController.view];
[self.navigationController setToolbarHidden:NO animated:NO];
}
Please suggest how to show UIToolbar in views.
Thanks for help.
Added
[self.view addSubview:toolbar];
This helped in solving the issue.

How to flip vertically UIViewController which is presented modally

How can I vertically flip a UIViewController which is presented modally along with a UINavigationController and Done Button.
This is how I am presenting the UIViewController modally:
- (void)modalViewAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds] autorelease];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_viewController = [[ModalViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
[_viewController.navigationItem setLeftBarButtonItem:button animated:YES];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[self.navigationController presentModalViewController:self.viewController animated:YES];
[self.view addSubview:navigationController.view];
[navigationController release];
}
How can I flip this UIViewController whilst presenting it modally?
Thanks for your help.
You can specify the desired animation technique by setting the modalTransitionStyle property (before presenting the view controller).
Unfortunately this property can be set only to:
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl
So vertical flip it is not supported.
Take a look at UIModalTransitionStyle reference:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

Problem in showing subview as popover

I want to show the subview of a view in popover.
To elaborate,
I have a mainViewController.
I hava a subview 'songListView' in this mainViewController.
I have a button titled 'list' in the mainViewController' itself.
I want to show the songListView in popover on clicking the button 'list'.
So, how should I do this.
You could use the below code as a reference for showing PopOver from a UIButton
-(void) buttonAction:(id)sender {
//build our custom popover view
UIViewController* popoverContent = [[UIViewController alloc]
init];
UIView* popoverView = [[UIView alloc]
initWithFrame:CGRectMake(0, 0, 200, 300)];
popoverView.backgroundColor = [UIColor greenColor];
popoverContent.view = popoverView;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover =
CGSizeMake(200, 300);
//create a popover controller
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self.popoverController presentPopoverFromRect:popoverButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
//release the popover content
[popoverView release];
[popoverContent release];
}
My problem is solved.
I just created another View Controller class, i.e. 'TempPopoverView'.
Then I set the view of this TempPopoverView equal to the subview of my MainView
Here is the code snippet from my code:
TempPopoverView *viewController = [[TempPopoverView alloc]initWithNibName:#"TempPopoverView" bundle:nil];
[self. songListView setHidden:NO];//songListView is subview of MainView
viewController.view=self. songListView;
UINavigationController *navCont = [[UINavigationController alloc]initWithRootViewController:viewController];
navCont.navigationBar.tintColor = [UIColor colorWithRed:.102 green:.102 blue:.102 alpha:1];
[self showPopOverController:navCont andFrame:sender.frame andInView:self.view];//self.view is the MainView
[viewController release];
[navCont release];
U can use presentModalViewController on your main view.
For example
SongListView *songList = [[SongListView alloc] init];
[self presentModalViewController: songList animated: YES];
There are different animations possible as well.
as simple as you think to add subview in self.view
[popoverController.contentViewController.view addSubview:yourselfobject];