标签云

微信群

扫码加入我们

WeChat QR Code

I am struggling with subviews that have their own UIViewControllers. I have a UIViewController with a view (light pink) and two buttons on a toolbar. I want blue view to display when the first button is pressed and the yellow view to display with the second button is pressed. Should be easy if I just wanted to display a view. But the blue view will contain a table, so it needs it's own controller. That was my first lesson. I started off with this SO question where I learned I needed a controller for the table.

So, I am going to back up and take some baby steps here. Below is a picture of a simple starting point with my Utility ViewController (the main view controller) and the other two controllers (blue and yellow). Imagine that when the Utility ViewController (the main view) is first displayed the blue (default) view will be displayed where the pink view is located. Users will be able to click the two buttons to go back and forth and the pink view will NEVER be displayed. I just want the blue view to go where the pink view is and the yellow view to go where the pink view is. I hope this makes sense.

Simple Storyboard image

I'm trying to use addChildViewController. From what I have seen, there are two ways to do this: The Container View in the storyboard or addChildViewController programmatically. I want to do it programmatically. I don't want to use a NavigationController or a Tab bar. I just want to add the controllers and shove the correct view into the pink view when the associated button is pressed.

Below is the code I have so far. All I want to do is display the blue view where the pink view is. From what I have seen I should be able to just addChildViewController and addSubView. This code is not doing that for me. My confusion is getting the better of me. Can somebody help me get the blue view displayed where the pink view is?

This code is not intended to do anything other than display the blue view in viewDidLoad.

IDUtilityViewController.h

#import <UIKit/UIKit.h>

@interface IDUtilityViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *utilityView;
@end

IDUtilityViewController.m

#import "IDUtilityViewController.h"
#import "IDAboutViewController.h"

@interface IDUtilityViewController ()
@property (nonatomic, strong) IDAboutViewController *aboutVC;
@end

@implementation IDUtilityViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.aboutVC = [[IDAboutViewController alloc]initWithNibName:@"AboutVC" bundle:nil];
    [self addChildViewController:self.aboutVC];
    [self.aboutVC didMoveToParentViewController:self];
    [self.utilityView addSubview:self.aboutVC.aboutView];
}

@end

--------------------------EDIT------------------------------

The self.aboutVC.aboutView is nil. But I wired it up in the storyboard. Do I still need to instantiate it?

enter image description here


What's the actual problem you're having?

2018年09月27日46分50秒

When the view loads, I see the pink view. I'm guessing I must not be coding it correctly.

2018年09月26日46分50秒

Right, OK. First of all, step through the code one line at a time and make sure none of the objects are nil. Specificalyl self.aboutVC, self.utilityView and self.aboutVC.aboutView.

2018年09月26日46分50秒

You are correct. The self.aboutVC.aboutView is nil. But I wired everything up in the storyboard. I'll update my question with the image.

2018年09月26日46分50秒

What's ur first button and what's your second button? And when u say the blueview where the pink is...do u mean once the blueview is on screen—still the 'X' and the '?' Are hanging there at the top of the view?

2018年09月27日46分50秒

I'll try this when I get into the office. Thank you. I still think you're AWESOME!!! :-)

1970年01月01日00分03秒

L O L let's work it out one step at a time!

2018年09月26日46分50秒

You're funny, Joe. This solves my damned problem using a Container View. :-) I wanted to do it in code simply because I did it before in code (once....and over a year ago). I started off, like everybody else, using the storyboard. My ex-coworkers were Sr. iOS developers and didn't want to use storyboards because we had too many windows. So my experience with storyboards and segues are extremely weak. Recently, I've been working with AVFoundation and camera controls and NOT UI stuff. When you get slung around it's easy to forget the basics.

2018年09月26日46分50秒

I might still need a little more help with the little "square in a square" Masonic symbol thingy!!! :-)

2018年09月26日46分50秒

iOS 9's Storyboard References make container views even more awesome. You can define your reusable view(controller) wherever you like and reference it from any container view in multiple, modular storyboards.

2018年09月26日46分50秒

Is setting frame necessary? or it will give the view a default frame according to the storyboard?

2018年09月26日46分50秒

Brian, it's probably not necessary if the view you're adding is full screen, but I usually add it any way to make sure. Also, in this case, I wasn't sure if self.utilityView was the full view or the part of the view below the tool bar, in which case, you probably do need to to set the frame.

2018年09月26日46分50秒

it's 100% not necessary, it's what the storyboard is for guys!

2018年09月26日46分50秒

rdelmar - Thank you for your information. I wondered if I should be using instantiateViewControllerWithIdentifier but was a bit confused. Also, yes, self.utilityView is not the full view. All 3 subviews are the same size, so I didn't think I needed to set the frame size. I'll try your suggestion when I get into the office.

2018年09月26日46分50秒

JoeBlow, The OP mentioned using a container view in the storyboard, but said they wanted to do it in code.

2018年09月26日46分50秒