2

I have one UIViewController with two UIViews on it. In the Navigation bar, when one button is pushed one of the UIViews is displayed and when the other button is pushed the other UIView is displayed. I want to put a UITableView on one of the views. However, the UITableView requires the UIViewController to use the UITableViewDelegate and UITableViewDataSource. Having implemented this for my UIView (subview) containing the UTTableView, when I click on the button for the other view, which does not contain a table, I get errors and the application croaks.

I am assuming (possibly incorrectly) that my issue is that I am trying to use the same UIViewController for both subviews, but only one contains a table.

Question 1) Is it possible to do what I described above? Meaning, if I had a problem then something was not connected up correctly.

So, I went down a path of creating two separate UIViewControllers; one for each view. Not sure this is the smart approach. Now I am just looking for advice on the best way to do this. Thank you in advance for your help.

enter image description here

To be more clear about what I am trying to do. I want the blue view to be put where the pink view is when the first button on the bar is clicked and I want the yellow view to be put where the pink view is when the second button is clicked. Essentially the pink view will never be displayed and may not even need to be on the UtilityViewController.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Patricia
  • 5,019
  • 14
  • 72
  • 152
  • 2
    *"I went down a path of creating two separate UIViewControllers"* this is utterly correct. Say one view controller is "ShowDogs". Say the other view controller is "ExoticCarClubSignUp". This is exactly how you do iOS apps. Each of those would be a subclass of UIViewController. (**Note:** very often, people would call them "ShowDogsViewController" and "ExoticCarClubSignUpViewController". For me that is a waste of energy and contributes to global warming. I mean ... "what else could they be?" it's an iOS app ... everything is a view controller!) Go for it! – Fattie Apr 11 '14 at 17:14
  • 1
    Just to be crystal clear. When you click the button, Apple sort of automatically gives you a generic "UIViewController". You know? Arguably, this is *quite a bad idea*. (Note that in the real world, you'd never use that, ever - at all.) What they should do is kind of give you a generic "YourFunctionViewController" ... which you would then edit to be "ShowDogsViewController" and "ExoticCarClubSignUpViewController". I hope that makes sense! – Fattie Apr 11 '14 at 17:18

1 Answers1

2

Having each as a UIViewController (or a subclass thereof) is the way to go about what you are trying to do. The UITabBarController does this already: https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html

mr_greb
  • 208
  • 2
  • 8
  • 1
    Great answer. Indeed Lucy the cat, I would encourage you to not use UITabBarController *at first* .. it can be a bit of a crutch. Go for it! – Fattie Apr 11 '14 at 17:16
  • @JoeBlow - I don't want to use the UITabBarController. So now, I'm trying to figure out how to shove the User SettingsView into the UtilityView and use the separate controllers. Not as straight forward as I would have preferred. It's looking like I need to use the ContainerView. What do you think? – Patricia Apr 11 '14 at 23:18
  • 1
    Hmm, I guess I don't understand what you're up to. Is it possible you are looking for **addChildViewController** and related functions? Note that as I said above: *"I went down a path of creating two separate UIViewControllers"* **that is correct and what you should do**. But again I may not understand your plan here. it's not clear if you're saying you want the table view to run "INSIDE THE OTHER WINDOW". So you have some view ("show all dogs" or whatever) and then you want a "small table" to appear (say, at the top right .. whatever). Perhaps you should just post an image of the app. – Fattie Apr 12 '14 at 09:02
  • @JoeBlow - I added this to the original question. But for you, I'll include it here as well. I want the blue view to be put where the pink view is when the first button on the bar is clicked and I want the yellow view to be put where the pink view is when the second button is clicked. Essentially the pink view will never be displayed and may not even need to be on the UtilityViewController. – Patricia Apr 12 '14 at 13:20
  • @JoeBlow - I think you are correct about me wanting to use addChildViewController. From what I have seen, there are two ways to do this: The Container View in the storyboard or addChildViewController programmatically. Is this correct? I guess now I'm just looking for verification that I am understanding this correctly. Thank you for your help. – Patricia Apr 12 '14 at 23:55
  • I'm pretty sure that's correct, as I understand you. However that's an excellent, clear question. I would urge you to go ahead and ask that as a clear separate question. there's nothing wrong with asking more and more questions on a similar topic, indeed I encourage you to do that. Your current question has "too much in it" so it's good to ask another (or more) questions that are one-shot and have one simple clear question! Cheers! – Fattie Apr 13 '14 at 08:00
  • Does it help, http://stackoverflow.com/questions/21255392/from-within-a-view-controller-in-a-container-view-how-do-you-access-the-view-co – Fattie Apr 13 '14 at 08:02
  • @JoeBlow - YOU'RE AWESOME!!! I'm going to follow up on the link you provided and one provided in it before I submit another question, so I have a better understanding of both. I'll let you know when I post it. I so appreciate your positive feedback. You're the best!!! :-) – Patricia Apr 13 '14 at 23:28
  • I'm not awesome, just a drunk! My kids think your cat photo is awesome! When you post a new question, post a comment on this question linking to the new question. Interestingly I just asked a related question! http://stackoverflow.com/questions/23042908/for-uicollectionview-set-the-controller-in-storyboard – Fattie Apr 14 '14 at 06:32
  • 1
    @JoeBlow - I FINALLY asked a new question here: http://stackoverflow.com/questions/23399061/objective-c-how-to-add-a-subview-that-has-its-own-uiviewcontroller. I'm not being lazy, I really have not been able to figure out how to do this. Seems like it shouldn't be that hard. I'm beginning to feel really dumb. – Patricia Apr 30 '14 at 22:26
  • Lol far from dumb - you're "starting at the top"! :) You know, I firmly believe that **using Container View in the storyboard** is basically the answer to your question! I will try to make an effort to be more specific in your other question! Cheers – Fattie May 01 '14 at 07:32