iOS UINavigationController Not Working - ios

I'm creating a tour screen where a user would swipe left and right to show some instructional pages.
However I'm having issues in loading pushing the next view. I've created a UINavigationController as below in a handleSwipes method:
TourViewController2 *tourView2 = [[TourViewController2 alloc] initWithNibName:#"TourViewController2" bundle:nil];
UINavigationController* navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:tourView2 animated:YES];
Any ideas?
Thanks.

As Aniket Kote wrote, you have to not only create UINavigationController, but also make it rootViewController in you application. After done this, UINavigationConroller became a main container and all others vies are placed inside it. For this reason you should not create UINavigationController every time you want to navigate through pages, but only one for all navigation path, eg. when you application in launching.
Next you can use this "global" navigation controller like
[self.navigationController pushViewController:tourView2 animated:YES];

Try this:
TourViewController2 *tourView2 = [[TourViewController2 alloc] initWithNibName:#"TourViewController2" bundle:nil];
UINavigationController* navigationController =[[UINavigationController alloc]initWithRootViewController:tourView2];
self.window.rootViewController=navigationController;

After reading your requirement, i will not go on your approach. I will suggest the use of "Page Control". As defined by Apple below :-
UIPageControl indicates the number of open pages in an application by
displaying a dot for each open page. The dot that corresponds to the
currently viewed page is highlighted. UIPageControl supports
navigation by sending the delegate an event when a user taps to the
right or to the left of the currently highlighted dot.
Hope this helps you !

Related

Convert storyboarded single view app to tabbed view with multiple of the same view

I've nearly perfected my first iOS app as a single view using a storyboard in Xcode 5. Now I would like to include a tab bar with three copies of that same view. I.e. there will be three different data sets displayed in exactly the same way, selectable from the tabs.
I'm struggling with the approach to make this conversion. As I understand it all my model and controller code can remain identical (save for fetching the data unique to each) but I am lost as to whether I modify the storyboard or do it programatically.
The storyboard approach seems wrong as each view wants its own definition, when they should share the same. The code approach makes more sense but I am struggling with finding all the pieces to make it work. I've had a couple of goes and cannot get the tabbed view to even display when launched in the simulator.
I understand my existing view will need to shrink to fit but I have auto layout working, so that should take care of itself.
Just to clarify, I built the app from the "single view" template and am using the storyboard currently to launch and connect everything.
As per the comment I left above, here's the solution I went with.
Attach the existing view controller to the tab bar controller by dragging.
Remove any default added views from the tab controller, leaving only mine.
Mark the tab controller as the initial view. (And unmark my original.)
Change my original view controller to cope with the space taken by the tab bar (in my case I don't want anything under it so I unticked "Under bottom bars".)
In my code, throw away the existing single attached view and instead establish three
Inside the app delegate's didFinishLaunchingWithOptions method...
// Add extra tabs
UIViewController *vc1, *vc2, *vc3;
vc1 = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"CBViewController"];
[vc1 setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"Tab A" image:[UIImage imageNamed:#"tabA_unselected"] selectedImage:[UIImage imageNamed:#"tabA_selected"]]];
vc2 = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"CBViewController"];
[vc2 setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"Tab B" image:[UIImage imageNamed:#"tabB_unselected"] selectedImage:[UIImage imageNamed:#"tabB_selected"]]];
vc3 = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"CBViewController"];
[vc3 setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"Tab C" image:[UIImage imageNamed:#"tabC_unselected"] selectedImage:[UIImage imageNamed:#"tabC_selected"]]];
_tabController = (UITabBarController *)self.window.rootViewController;
[_tabController setViewControllers:[NSArray arrayWithObjects:vc1, vc2, vc3, nil] animated:YES];

Moving from one view to another iOS

I'm relatively new to iOS development. I am to move from one viewController to another I use a modal segue transition on button click. This is a game so i want to allow the user to click images to essential move the the app menus.
I have a main page that displays several images, on clicking one i want to be able to move to another view. Currently doing this with a modal segue is causing odd problems with my touchesEnded event where if, for example, i navigate to a page 3 times the touchesEnded event is fired 3 times.
Is there a better way for me to do this or am i just missing thing fundamental?
Thanks
Yes, I think you must make the the navigation controller your root view controller then push views accordingly
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:YOUR_BASE_CONTROLLER]
self.rootViewController = nav;
this is in your app delegate.
Then in your action method
[self.navigationController pushViewController:secondViewController animated:YES]
Im assuming you are using the Storyboard to link VCs using segues.
Modal segues are great for simple transitions but really seem to limit what you can accomplish when they are just linked through SB. Ive found that creating an IBAction that includes the following for a VC segue will allow you to not only control your segues more efficiently but also allow you to have a clearer view of what is actually occurring during the transition.
-(IBAction)goToVc:(id)sender{
//Other code to take place during the segue here
//This will identify the Storyboard in use
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
//This will identify the View Controller to switch to
SecondViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewControllerID" ];
[self presentViewController:vc2 animated:YES completion:NULL];
}
Every time you perform a modal segue, you lose the UINavigationController that you were previously using. What you need to do is embed a UINavigationController in the view that you are performing a modal segue to.
Check out a question I answered the other day to help you visualize more clearly what I'm talking about.

I dont want to add UInavigationController and tabbarcontroller on root view controller

I have a single view based app, when it runs it shows 2 button. On tap of 1 button i want to switch to another view which must be uinavigationcontroller and on 2 button i want to switch to tabbarcontroller view. I know what uinav and tabbar controllers can do. I created uinav and tab based project and study all the code, searched on internet for tutorials but what i get is everyone telling to add like this
self.window.rootViewController = self.navigationController;
on rootviewcontroller.
I dont want to add UInavigationController and tabbarcontroller on root view controller. Please help me solving this issue.
Thanks.
Then create a UIViewController called RootViewController for example, add two buttons inside its view and handle the touch events for those buttons. The first button, when touched should present your UINavigationController and the second button should present your UITabBarViewController. Then in your AppDelegate's didFinishLaunchingWithOptions: method initialize your RootViewController and set the self.window.rootViewController = rootViewController;
(UPDATE)
Allright, create a SingleView application. Put the buttons and set up the outlets and actions for those buttons. You also need to create 2 more view controllers: one UINavigationController and one UITabBarController. When the first button is touched, in the method which handles the touch add:
-(void)button1Touched:(id)sender
{
MyNavivationController *navc = [[MyNavigationController alloc] init];
[self presentViewController:navc animated:YES completion:nil];
}
You can do the same with the other button, but there initialize your tab bar controller and do the same. If you are unfamiliar with those operations you may refer to the documentation on how to create view controllers, handle events and etc...
#pamy I can provide you complete source code please let me know your skype id or other way to send you the file, i don't know how to post the code on stackoverflow.
I did write many times but it's not accepting my code.

MainWindow.xib in Tabbed Application

I am a beginner in IOS field and I find it some what difficult to follow the old tutorials including videos which are not even 4-5 months old. The main reason is that I am working with xcode 4.2.1 and most of the tutorial is based on the earlier versions. So this is sort of letting me down to move with some examples which I want to work out.
The main problem is with MainWindow.xib file, where I found some nice tutorial and video of how one can manually reconstruct MainWindow.xib. I need to say I followed that and was good at recreating it. On the other hand it I have got a question, whatever new project I need to work on other than Empty Application, is it OK to create the MainWindow.xib file in the same way one created for the Empty Application, or it would be different for Tabbed Application or for some other application.
Can somebody throw some light on this!
Thanks
Maks
Following code for the creating the UItabbar.
In xcode 4.2.1 there is no any main.xib is created for that u need to apply the dynamically(code) thru create the tabbar and then call it.
-(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];
}
It may be helpful to implement your application
There are many ways to make an application with a tap bar. I prefer to create the tab bar controller in code and do the interfaces of the view controllers shown in the different tabs with xibs. But sometimes I find it more practical to create the interfaces completely in code.
I would alloc and init a UITabBarController in the AppDelegate and assign the view controllers I need to the tab bar controller. Then you have to assign the rootViewController of the window to the tab bar controller.

Change RootViewController inside UISplitView

I'm developing a little example for iPad from a UISplitView XCode Template. It's formed by a root controller which is shown in the left of the window and a detail view which is shown in the right.
What I want to achieve is very simple (at least I think so) but I can't find in the documentation the way to do it.
I'd like to substitute the root controller (which appears fixed in the left) with a new controller (for example as response to a event launched when you push a button). I've tried this:
ColorPicker *controlador = [[ColorPicker alloc] initWithNibName:nil bundle:nil];
[self.rootViewController presentModalViewController:controlador animated:YES];
[controlador release];
What happens with that is that the new pushed controller fills the entire window, whereas what I want is that appears fixed at the left with the two columns format that were at the beginning.
You need to set the modalPresentationStyle to an appropriate value,
controlador.modalPresentationStyle = UIModalPresentationCurrentContext;
UIModalPresentationCurrentContext instructs the view controller to appear modally within the frame of the rootViewController.
Use pushViewController:animated instead may fix this. About ModalViewController, check document http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

Resources