I have a custom view controller I load from a Storyboard. When I try to set one of its subviews nothing happens (remains generic white view). What I don't understand is if I try to set VC.view it works fine. Why is this? Everything seems to be initialized after I load from the Storyboard. Where would I set the VS's subview?
Yes, this slightly confusing behaviour is how it is 'supposed' to work. When a view controller is loaded, its view is not - at least not until it is actually needed. See this doc for further info. Only when the view controller is presented, will it then load the view. As you have found, this is tiresome, because you often want to set some of the the subviews' properties before it is presented (say in a prepareForSegue or prior to pushViewController: or presentViewController:).
There is a work around - based on what you have observed. If you directly access the view property, the view controller will immediately load the view and all its subviews. So, if you want to set subview properties, just "touch" the view itself:
NSLog(#"View tag is %i", viewController.view.tag);
and you should then be able to access the subviews.
Alternatively, you could pass the relevant data in (non-UI) properties of your view controller, and then set up the subviews using that data during viewDidLoad or viewWillAppear.
Related
I have 2 ViewControllers.
VC1 has a 2 views inside it.
I want to get MenuUIVC to appear in one of my views belonging(child) to VC1.
I tried this code inside of VC1 but it didn't work.
MenuUIVC * menuViewVC = [[MenuUIVC alloc] init];
menuUIView = menuViewVC.view;
I expected to be able to see the MenuUIVC inside the view (menuUIView) which is a child of VC1. I have the IBOutlets all hooked up on the storyboard.
You can use Container views to get View Controller inside a View Controller. And, you can get the reference to it through prepareForSegue() method.
Links for description:
iOS Container View
The Easy Way to Switch Container Views in iOS
If you are using UIView, then why don't you use
[menuUIView addSubview:menuViewVC.view];
Another option is to use childviewcontrollers.
See these links for example:-
adding view controller as child view in ios
Add child view controller to current view controller
Using container or child view controller, gives you full fledge option of using View Controller properties, which you will not get in UIView.
Say for example, you can you orientation delegate methods, which you can't using in UIView class directly.
Hope this can help you.
Cheers
Sanjay
you can go for container view if you want to do it using IBOutlets & storyborad.
but if you want to do it using code then follow the below steps :
initialise & create parent view controller.
then add required child view as a subview with negative frame.
now change the frame with UIView animation whenever required. & do add tap gesture recogniser on part of screen other than the child view to remove it back to the initial position.
I'm creating a ViewController that will contain as a portion of it a scrollView. In that scrollView I would like to include the view of another ViewController. When I set up this ViewController inside of the ScrollView, all of that ViewController's data is pulled from the web and even it's "ViewDidLoad" method is called. However, nothing appears except for the tableViewLines and a spinner I've created to show the page is loading. Here is what it looks like (the ScrollView in question is under Commitments and Awards):
What should be loaded inside the scrollView is a tableView that looks like this:
It may be that the tableview's delegate/datasource are not set correctly. Could you check whether tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath: are called or not?
It is not a good idea to show view of one view controller in another view controller view. Apple does not recommend it. what ever you want to do, do it in the same view controller.
I have a viewcontroller with default view associated with it.
If I want to discard the view only not viewcontroller, is it ok to set self.view = nil? So that view gets discarded and new view gets allocated with new values.
I want to fill the view with new UI, based on a condition I did that in viewdidLoad. So If I set self.view = nil, and try to access the view from anywhere, it will again create the view. So the new properties will be set.
Is it a right approach to do it. Or I have to write a separate method to refresh view with new properties.
Please provide me the correct approach to do it.
I don't think there is a correct approach, but here is some advice.
Don't interrupt the view lifecycle (make sure -viewWillAppear:, -viewDidAppear:, -viewWillDisappear:, -viewDidDisappear: all get called).
Consider multiple view controllers contained within a custom container view controller instead of swapping views. It will keep each view controller focused on a specific task.
Consider having an empty self.view and use -addSubview:/-removeFromSuperview to swap the content. That way you will not need to reset self.view.
Don't remove the default view which comes with the uiviewcontroller. If you want to show the custom uiview based on conditions then I would suggest, create your custom uiview and then initialize it and add it on your default view for respective conditions.
I am testing out something I would like to have in my app. I have a UiViewController in a storyboard that has a UIScrollView - I now want to add other viewControllers to this scrollView and swipe between them.
I would like to add a view that I made in the storyboard into this UIScrollView. Is it possible?
I tried something along the lines of:
MYViewController *viewOne = [self.storyboard instantiateViewControllerWithIdentifier:#"myView"];
[self.scrollView addSubview:viewOne.view];
I've set the the scrollView size to be bigger than the screen and when the main view loads, I can see there is a scroll view (the scroll bars show) but my viewController is not inside it.
Anyone have any ideas?
The code you posted is still not right, on a couple of levels.
First, you should not use alloc/init for view controllers. You either need to use initWithNibName:bundle: (to create a view controller from a nib file) or instantiateViewControllerWithIdentifier: to load a view controller from a storyboard.
Second, you should not add a view controller's view as a subview of another view controller unless you use the parent/child view controller support that was added in iOS 5 and greatly improved in iOS 6. If you do what you are doing then all sorts of things won't work correctly: Auto-rotation, low memory warnings, background notifications etc. The list of things that can go wrong is unbounded.
The easiest way to do this is to add a container view as a frame to hold your child view controller, and then control-drag from your container view onto the scene that you want to set up as a child. This causes IB to set up and "Embed" segue. Embed segues do all the housekeeping you need to host one view controller's content inside another, with no code needed.
You could create a container view inside your scroll view's content view, and then it would just work fine.
I found the problem:
I was not allocating and initialising my viewController. Ooops.
This is the correct code:
BaseViewController *viewOne = [[BaseViewController alloc]init];
viewOne = [self.storyboard instantiateViewControllerWithIdentifier:#"myView"];
[self.scrollView addSubview:viewOne.view];
I think I am having a case of disappearing up my own arse.
I am creating a small view on a ipad thats for settings (so not full scree), within this view. I need a navigation controller to show another view.
At the moment I have one class / xib
The xib contains the main view (graphic / boarder). This view is linked to the files owner and appear.
On the same xib, I also have a navigation controller that contains the inner view.
OnViewDidLoad I add the navigationcontroller.view to the subview and it appears. However I cant push anything off it. I wired up the delegate and etc but I am sure I am missing something stupid
Can I do this all within one controller / xib?
The only code I have done is
[self.view addSubview:mainNavigationController.view];
Is there some code I need to do for the navigationController
Just adding the navigation controller as a subview doesn't hook up the navigation controller to the view controller hierarchy properly. That's probably why it doesn't work.
Also the properties that need to be set are readonly properties, so I don't think there's anything you can really do about it.