So I have programatically created a tabbarcontroller that uses navigation controllers(based on Jeff Fithian's example). Now I need to push an additional page onto the navbarcontroller (ie I have clicked on an item on the list) but I can't quite figured out how to get a reference to the navigation controller of the current view from it's view controller.
I went with the programmatic approach, because it was the only way to start out the project that worked, but now I am getting really bogged down.
Any ideas?
I am sure there is a tutorial somewhere that addresses this, but after four days of looking (I am in the middle of the MidAtlantic "snow" emergency) I have found nothing...
You can find the navigation controller from the view controller with the .navigationController property.
[my_view_ctrler.navigationController pushViewController:other_view_ctrler animated:YES];
Related
I'm trying to do a project for the iPad in which I'd like to utilized the split view controller. I'll be having different detail view controllers for each of the cells in the master view controller.
I saw one solution how to do this via storyboard segues in this site.
He basically linked each of his UITableViewCell to different detail view controllers. But I'd like to know if this is a "stable" or a "good" way of doing this. I mean, is it any better or as stable as doing it programmatically? What would be the consequences of doing his method, if there are any?
Here is the link to the solution I found
This is kind of a tricky one, even though it's an incredibly common use case.
1) One idea is to have an empty root view controller as your detail and it handles managing segues under the hood to quickly segue to the detail view you actually care about, utilizing the "replace" segue. This should "technically" fix having the "back" button at the top left and still allow you to pop to root and not have it show the empty controller. Haven't tested these though, so I'm not sure.
Edit: In Xcode 6, the "replace" segue is conveniently handled by a "show detail" segue which is used specifically for this type of view handling on Split View Controllers. I recommend using this method exclusively in new projects. See sample code.
2) The other idea is to have separate navigation controllers in your storyboard (one connected, the rest all stranded). One for each detail view type and tapping on the master menu will simply swap the navigation controller for the detail view to the one you care about.
Code similar to this in AppDelegate:
self.detailNavigationController = [self.masterNavigationController.storyboard instantiateViewControllerWithIdentifier:#"MyChosenNavigationControllerStoryboardId"];
self.splitViewController.viewControllers = #[self.splitViewController.viewControllers[0], self.detailNavigationController];
self.splitViewController.delegate = (id)self.detailNavigationController.topViewController;
The downside to this second way is that in memory tests, it doesn't appear that swapping a new nav controllers in frees up all of the memory that the old nav controller was using. So it's good to use for simple apps but not for anything crazy complex.
I thought this is quite simple but I can't seem to find the answer. I have a view controller with 3 buttons, one of these buttons has a push segue to a tab bar controller. When pressed I presume it loads the '0th' tab item which is the fine for the first button but how do I then get the other two buttons to push segue to the other two tabs?
I have read a lot of articles but none of them seem to have the answer that I'm looking for. I already have the tab bar controller and all the sub-views created in the storyboard, I just can't seem to get an instance of the UITabBarController.
I have 2 logical ways of doing it in my mind but I am quite new to objective-c and can't work either of them out for the life of me.
The first is to programmatically load the uitabcontroller and set which item I'd like to load but I can't seem to find a way of invoking an already existing tab bar controller. Something like this (I know it loads a random view which is not what I exactly want):
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:view1ViewController];
My other idea was to either override / hijack the segue or the load event of the tab bar controller and find out who called the segue and then if they are one of my other buttons and then switch to the relevant tab item using:
[myTabBarController setSelectedIndex:index]
I just can't seem to put it all together. I suspect the latter idea requires app delegates which I don't quite understand yet.
I apologise for this in advance - I realise that I'm in completely way over my head! (btw... I have moderate PHP and C# OOP experience so I understand 'some' things.)
Relevant Data:
I'm making a simple game, using OpenglES. The game itself is done, however I would like to have a main menu as well as some other screens designed in IB. So far I have a death/score screen that is displayed with a simple modelviewcontroller.
I haven't done a ton with GUI building or much programming on the platform outside of C code (posix sockets) and some examples from some books. So I'm not sure how I would go about having lots of views- usually I just use a model view, and it's gotten me along just fine so far. However I don't think that would be the best route here.
Situation:
I have a view controller that shows my main menu- the main menu branches off to the main game, a settings screen, and a high score screen. The main game is made in opengl, and I haven't made the settings view yet, but it likely will be as well. How should I switch between the views? Presenting the main view from the app delegate is as simple as setting the root view controller = newly created view controller, should I do the same thing here? If I do that can I remove the resources from the menu view controller?
Sorry if this is an extremely simple question, I just don't quite get the process.
I'm not entirely sure what you want to do, but an easy way to show a new view controller is:
SecondViewController *aSecondViewController = [[SecondViewController alloc]
initWithNibName:#"SecondView" bundle:nil];
[self presentModalViewController:aSecondViewController animated:YES];
I hope that helps.
How should I switch between the views?
In the vast majority of cases, you should be using a UINavigationController. Your initial controller would be the main menu. When you want to go into a particular section of your application, you push a new view controller onto the stack. When you want to come back out, you pop it off the stack.
Besides navigation and presenting modally that others have mentioned, another option is to swap out views. May or may not fit your app's flow but wanted to point out another option for you to consider
Best practice for switching iPhone views?
If you are already limiting the game to iOS 5 for some other reason you should look into UIStoryboard. If you don't currently require iOS5 the "simplest" way is to use table views, but that isn't a very "gamey" UI.
Here are the steps I've followed - pretty much this: http://www.youtube.com/watch?v=cFNtD098Bbk&feature=player_embedded
Create new Tab Bar Application
Looking at MainWindow.xib I drag a UIViewController in
Drag one of the tabs View Controllers onto the UIViewController
Set up buttons and actions top push the new view
Note I have no idea how he did the last step in the video, dragging one tab object onto another won't work for me
The navigation works fine, the new view gets pushed and all that jazz. But when I go to the tab that has the navigation controller it has two navigationBar's.
I can't find where to get rid of this for the life of me. It must be something really simple but I've been searching for way too long.
None of the many tutorials I've found have this problem, not does the project files of those tutorials even though my project looks pretty much the same as theirs in IB.
Does anyone know how I can get rid of the useless navigation bar?
I'm using Xcode version 4.0.1
Not sure how I missed it but the answer was stupidly simple.
Just needed to delete the UIToolbar from the nib file, can't believe I didn't find that earlier.
I have a fairly hefty project, where I am loading a few view controllers, one after the other. First, a splash screen, followed by a menu system, and when the user clicks on the menu it goes through to an article view controller.
Putting all these in with shouldAutorotate... set to YES for all rotations, this works fine. However, I have a menu bar I need to slide down over the top when a tap gesture has been recognised. I have one for the main menu, and one for the article view.
If I put one of these in, it still auto-rotates fine. However, as soon as I put the next one in, the auto-rotate stops working. I've tried putting the menu bars in the app delegate, as well as nesting them inside the menu/article view controllers. The Menu Bar view controller also has shouldAutorotate... set to YES. In fact, every single view controller in the project (all 7 of them) have it set to YES. And yet, when I add my second Menu bar controller, it stops auto-rotating. It doesn't even trigger the "shouldAutorotate" method to ask it.
The code is way too large to post here, but if you'd like to see anything in particular then just ask. I'm totally stumped! I'm about to pull the menu bars out of their view controller and code them up in each of the view controllers individually. This will be a hideous amount of code duplication, but I can't think of any other way round it!
Any ideas? Thanks!
The answer appears to be... Don't put view controllers within other view controllers! One view controller = one screen, seems to be the rule. I have a lot to learn!