NavigationController from Modal View? - iphone

I'm using a Navigation Controller. My RootViewController pushes a number of views but it also presents a modal view. That modal view presents a tableView. So what I'm trying to do is figure out of I can push the Navigation Controller across the modal view, then use it from that modal view to push the view with the tableView? or barring that, is there a way to implement a second Navigation Controller from the modal view?
The real issue is that I want to present the view with the tableView using a right to left transition, which of course is not available with modal views.
I have this code that SORT OF provides the right to left transition used by the Navigation Controller:
NewViewController *newViewController = [[NewViewController alloc] init];
[self presentModalViewController:newViewController animated:NO];
CGSize theSize = CGSizeMake(320, 460);
newViewController.view.frame = CGRectMake(0 + theSize.width, 0 + 20, theSize.width, theSize.height);
[UIView beginAnimations:#"animationID" context:NULL];
[UIView setAnimationDuration:0.5];
newViewController.view.frame = CGRectMake(0, 0 + 20, 320, 460);
[UIView commitAnimations];
[newViewController release];
The problem is that the OldViewController (the one calling the NewViewController) disappears immediately so the NewViewController transitions across a blank screen, instead of covering the OldViewController.

Presenting a UIViewController modally creates a whole new navigation stack. You could do something like this:
//Create the view you want to present modally
UIViewController *modalView = [[UIViewController alloc] init];
//Create a new navigation stack and use it as the new RootViewController for it
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:modalView];
//Present the new navigation stack modally
[self presentModalViewController:nav animated:YES];
//Release
[modalView release];
[nav release];
This can be done because UINavigationController is a subclass of UIViewController. When the new view is loaded, you can use it however you wish (push another view on top of it, us UIView animations like Waqas suggeste, etc').
I hope I got your question correctly. Do tell if I didn't.

Related

Present UIViewController one after another

UIToolbar button on the MainViewController presents UIViewController, now i want to display multiple UIViewControllers one after the other via this button.What is the best way to do this.
- (void)displayviewsAction:(id)sender
{
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
}
when button is pressed it displays this UIViewController now right after this UIViewController i want to present multiple UIViewControllers one after the other.
Any ideas please.
Say you want to display view controllers A,B,C by tapping on main view controller's tool bar button. To do so write
[self presentModalViewController:a animated:YES];
on button action. In A's viewDidAppear: method, write
[self presentModalViewController:b animated:YES];
Writing same for all view controllers will display viewControllers one after another.

Child view offset by 20 pixels in TabBar application

I have a tab bar application and when I display a modal view controller, the content screen is offset by about 20 pixels to the top and left. It looks like this:
I'm presenting this view from the child view controller (detail view) of navigation controller (main view) of the tabview.
When I show the view, I'm hiding the tab bar and navigation bar but status bar is kept visible. Adjusting the view to be centered (through Interface Builder's Simulated Interface Elements->View Mode : Center) and changing the view's frame after a call to 'viewDidLoad' in the controller doesn't seem to shift it.
- (void)viewDidLoad {
// this still doesn't cause it to shift
self.view.frame = CGRectMake(0, 20, 320, 460);
}
What's the way to adjust this so that the content area is shown correctly?
I launch the child view like this:
[detailController presentModalViewController:tvc animated:NO];
The app's view controller hierarchy:
Tab view with two child navigation controllers are created in the app delegate and the nav controllers added to the TabBar's view controllers:
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:tab1ViewController,
tab2ViewController, nil];
[window addSubview:tabBarController.view];
Each view controllers of the tab is created as a NavigationController with 1 view controller:
// MainViewController inherits from UIViewController
[MainViewController *viewController = [[MainViewController alloc] initWithNib..];
tab1ViewController.viewControllers = [NSArray arrayWithObject:viewController];
A detail view controller is launched with 'pushViewController' as a result of some action on tab1ViewController :
DetailController *detailController = [[DetailController alloc]
initWithNibName:#"DetailView"
bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:detailController animated:YES];
[detailController release];
It's from the detailController that I'm trying to launch the problem controller.
Some things to check right off: is "viewDidLoad" actually getting called?
If so, what is self.view.frame set to after the assignment?
Put an NSLog at the end that prints out the x, y, width, height, and see what's there.
Also, since the trouble vc is modal, it will occupy the entire screen.
"On iPhone and iPod touch devices, the view of modalViewController is always presented full screen."
HTH,
Mike

UITabBar's navigationbar hides navigationbar when view is presented modally

I have app with UItabBarTemplate with navigation controller.
On selecting tab bar ViewControllerA is shown which on button touch pushes UIPieChartTabController which inherits "UIViewController".
Now I want another tab bar in UIPieChartTabController.
so in viewDidLoad of UIPieChartTabController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
UITabBarController *tabbar= [[UITabBarController alloc] init];
tabbar.view.frame = CGRectMake(0, 0, 320, 460);
piechartViewController *pr=[[piechartViewController alloc]init];
pr.tagInAction=1;
pr.title=#"Type";
pr.tabBarItem.image=[UIImage imageNamed:#"trend.png"];
pr.sDate=sDate;
pr.nDate=nDate;
piechartViewController *pr1=[[piechartViewController alloc]init];
pr1.title=#"category";
pr1.tagInAction=4;
pr1.sDate=sDate;
pr1.nDate=nDate;
piechartViewController *pr2=[[piechartViewController alloc]init];
pr2.title=#"paidWith";
pr2.tagInAction=3;
pr2.sDate=sDate;
pr2.nDate=nDate;
//tabbar.tabBar.delegate=self;
//this gave me error
ExportRep *pr3=[[ExportRep alloc]init];
pr3.tabBarItem.image=[UIImage imageNamed:#"database.png"];
pr3.title=#"Export Expenses";
[tabbar setViewControllers:[NSArray arrayWithObjects:pr,pr1,pr2,pr3,nil]];
[self.view addSubview:tabbar.view];
[pr release];
[pr1 release];
[pr2 release];
}
This piece of code worked but now when I select tab of viewController ExportRep type I tried
[self presentModalViewController:objMFMailComposeViewController animated:YES];
but navigationController of objMFMailComposeViewController hides behind navigationController of view that is presenting objMFMailComposeViewController.
Also viewWillAppear viewDidAppear of all the view controller which are bound to tab bar never gets called.
But none of this problem occurs for tabbar and viewcontroller which gets created by UITabbarTemplate.
Why Is it so? Whats wrong when I create Tab bar?
placing another tabBar within a tabbarVC, is not recommended. Why not use a UIToolBar to do the swapping of Views in your PieChartVC instead?
-apart from that, the reason your code doesn't call viewWillAppear,viewDidAppear is because of this:
[tabbar setViewControllers:[NSArray arrayWithObjects:pr,pr1,pr2,pr3,nil]];
[self.view addSubview:tabbar.view];
here only loadView of those prs will be called.
the viewControllers you assign to the tabBars should instead be wrapped around UINavigationControllers.
So something like this instead would do the trick
UINavigationController *nc1 = [[UINavigationController alloc]initWithRootViewController:pr];
[nc1.view setFrame:CGRectMake:("the frame in which you wnt prs to be displayed")];
[pr.view setFrame:nc1.view.frame];
. // similarly assign NavControllers for all prs
.
.
.
[tabbar setViewControllers:[NSArray arrayWithObjects:nc,nc1,nc2,nc3,nil]];
[self.view addSubview:tabbar.view];
The reason why new view which is being presented modally got its navigation bar hidden lies in [self.view addSubview:tabbar.view];
so it got room to present its view only in parent controllers view thus got cut its navigation bar.
so to hack it I kept tab selected and instead of presenting it in selected view controller,have presented it in main view controller only.

Navigating to another view controller without a navigation controller

As you guess am still a newbie, getting my head around iphone development.
I am just trying out basic view loading on demand, which i cant get to work
I have an app, with 2 view controllers, each view controller connected a different nib file.
I am trying to switch between view manually; there is no navigation control involved.
How can i manually push the second view to the first view?
self.navigationController pushViewController wont work since there is no navigation controller.
How else can I push the second view on top of the first view and destroy the first view; and ofcourse vice versa?
I have done this in the first view's button action:
SecondView *sv=[[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
[self.navigationController pushViewController:sv animated:YES];
obviously, it didn't work.
window addSubView didn't work either, because the first view controller is the root view controller (not sure i said that right). In other words, when i run the app, the first view is what I see with a button that is supposed to load the second view.
I have spent hours searching for a simple example, and I couldn't find any.
Any suggestions?
in the first view controller you need this:
- (IBAction)pushWithoutViewController:(id)selector {
NextNavigationController *page = [[NextNavigationController alloc] initWithNibName:NextNavigationController bundle:nil];
CGRect theFrame = page.view.frame;
theFrame.origin = CGPointMake(self.view.frame.size.width, 0);
page.view.frame = theFrame;
theFrame.origin = CGPointMake(0,0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8f];
page.view.frame = theFrame;
[UIView commitAnimations];
[self.view addSubview:page.view];
[page release];
}
and then link it to the button in nib. :)
try :
SecondView *sv=[[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
[self presentModalViewController:sv animated:YES];
IF you have first xib and you want to give navigation to another controller then you have to declare navigation in to appdelegate.m
write following code to app
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
then in ViewController.m
- (IBAction)NextButtonClicked:(id)sender
{
yourNextViewController *objyourNextViewController = [[yourNextViewController alloc] initWithNibName:#"yourNextViewController" bundle:nil];
[self.navigationController pushViewController:objStartUpViewController animated:TRUE];
}

Custom UITabBarController Problems with View Controllers and Views

I'm writing a custom UITabBarController so I can fully control appearance of the tab bar. I've got it all working so I have an array of view controllers that it handles.
The controller has a main view which fills the screen, and inside it it has a UIView at the bottom for the tab bar. That tab bar view has a button for each view controller. When buttons are pressed I add the view controller's view to the main view, and set it's frame so that it doesn't cover the tab bar view:
controller.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - kTabBarHeight);
This all works fine, and I can flick between the view controllers just fine. However, when I present a modal view controller, and then dismiss it, the current view controller's view becomes full screen and covers up my tab bar! I've tried setting the autoresizing masks to not resize, but is keeps happening.
I have also tried adding the view controllers view's to the bottom (below the tab bar) by using:
[self.view insertSubview:controller.view atIndex:0];
But when I do that, the tab bar is even visible above any modal views! Which is strange. I think there's something I'm not understanding so I would be grateful if someone can explain what I'm missing!
Thanks,
Mike
Try setting
controller.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - kTabBarHeight);
in the controller's viewWillAppear method
Try this out. I think you want dynamic view controllers within tab bar controller.
-(void)applicationDidFinishLaunching:(UIApplication *)application {
// Add the tab bar controller's current view as a subview of the window
tabBarController.delegate=self;
tabBarController=[[UITabBarController alloc] init];
mainDashBoard=[[DashBoard alloc] initWithNibName:#"DashBoard" bundle:nil];
mainSearchView=[[SearchView alloc] initWithNibName:#"SearchView" bundle:nil];
mainMoreView=[[MoreView alloc] initWithNibName:#"MoreView" bundle:nil];
UINavigationController *nvCtr0=[[[UINavigationController alloc] init] autorelease];
UINavigationController *nvCtr1=[[[UINavigationController alloc] initWithRootViewController:mainDashBoard] autorelease];
UINavigationController *nvCtr2=[[[UINavigationController alloc] initWithRootViewController:mainSearchView] autorelease];
UINavigationController *nvCtr3=[[[UINavigationController alloc] initWithRootViewController:mainMoreView] autorelease];
UINavigationController *nvCtr4=[[[UINavigationController alloc] init] autorelease];//[[[UINavigationController alloc] initWithRootViewController:nil] autorelease];
tabBarController.viewControllers=[NSArray arrayWithObjects:nvCtr0,nvCtr1,nvCtr2,nvCtr3,nvCtr4,nil];
nvCtr0.tabBarItem.enabled=NO;
nvCtr4.tabBarItem.enabled=NO;
[window tabBarController.view];
}
I've managed to find a better way to control the appearance of the tab bar by simply inserting subviews to the top of the tab controllers tab bar. It's worked a treat!