Button push to specific tab bar item? - objective-c

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.)

Related

UISplitViewController - Multiple Detail View Controllers via storyboard segues

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.

How come my uitextfield(s) keeps being disabled after model?

I made an app for my wife to track inventory. it has a couple of view controllers to enter a new item, display all items, search items, and display selected item. After switching through each view a couple of times (no particular order) all of the textfields become disabled (you cannot select them to change the value) across ALL of the view controllers. The buttons still work however. I have it setup so that when the user enters info into a particular cell, it saves it into memory so that if they switch views and then come back, the cells will get the data in the viewdidload method. Please Help!
*edit - I am using storyboard with a bunch of segues (i'm new to programming and self taught, so idk if this is the right way or not)... here is a pic of my storyboard
http://i.imgur.com/aT3PR0i.png
Ok, with a look at your storyboard, I think I see your problem. If your storyboard looks like somebody spilled spaghetti on it, it's not designed correctly. Specifically, it looks like you're going "backwards" (to controllers you've already been to) using segues -- that's not good. SEGUES ALWAYS INSTANTIATE NEW CONTROLLERS! So, if you go around in a circle a few times, you're adding more and more controllers to your project. To go backwards using segues, you need to use an unwind segue -- it's the only one that is an exception to the rule that segues always instantiate new controllers. The other ways to go backwards, without segues, are popping with navigation controllers, and dismissing modal controllers.
I can't really diagnose your immediate problem without a lot more information, but redesigning the storyboard using unwind segues would probably fix your problem.

Increasing number of living Views

I've set up a really simple project using storyboards including two views as shown here: http://i.stack.imgur.com/iRx21.png. The navigation can be done by either selecting a cell in the custom table view or hitting the back button labelled with "<<". Everything works fine except the following:
when I switch between the views, every time an instantiation happens. The profiling shows an increasing number of view objects. I would like to keep only one of each view and instantiation should be happen only once. What am I doing wrong? (I'm using ARC.)
Thanks in advance!
You should not link your back button to the parent view controller. This is what causes the new instantiation.
The way to go is to embed the table view into UINavigationController (in IB, choose Editor -> Imbed In -> Navigation Controller. Then change your segue to a Push segue. You can of course hide the navigation bar etc. to make things look exactly as you like. Then, link the back button to the controller with an IBAction and in the handler do a simple
[self.navigationController popViewControllerAnimated:YES];
This would be the appropriate logic of what you are doing. Of course, you can also push the web view modally and then handle the button click with
[self dismissModalViewControllerAnimated:YES];

UINavigationController inside UITabBarController - Two Navigation Bars?

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.

Can I use UITabBarController as a simple viewController switcher?

I'm creating an iPad app based on a UINavigationController (with the bar hidden) so I can push and pop other viewControllers for navigation around the app. However, I am now wanting to add a section in which there are two viewControllers that I want to be able to switch between, so in other words they are side-by-side, rather than hierarchical.
Is it okay to use a UITabBarController for this? I am aware that on the iPhone it is recommended they are used only at the root level of the app, but since this is an iPad app I wondered if I could use it? Also, I guess I need to create an empty viewController, create a UITabBarController within it and set the delegate to it, then add the two viewControllers to it... So in effect I will have a viewController within another viewController, and when I have done that in the past the results have been very flaky.
Can I do it this way? The only other way I can think of doing it is to have two plan UIViews within a UIViewController, but that also means I shouldn't really put any business logic in them (bad MVC!), and not being able to will be a right pain in the a**.
Any ideas?
Thanks!
:-Joe
EDIT: I also need to be able to swipe-animate between the two VCs within the TabBarController, AND have a menubar over the top which doesn't animate... Can I do this?
Sure.
I do this kind of thing all over the place in an app I'm working on. I actually have several different types of "toolbars" that can be optionally shown at different times.
What I do is create a "parent" member in my toolbar's class - and when a button is pressed, I have the toolbar call a method in the parent class to do whatever needs to be done - (i.e. display another view).
This avoids the whole mess of creating a view inside another view (or viewcontroller inside another viewcontroller - or whatever) - the toolbar can take the button hits, but all the views are opened by the root view/controller.
Hope this helps/makes sense!