Create Container view for Two UIViewController on iPad - ios

i have two UIViewController class with xib from interface builder, and i want create another UIViewController with xib that contains this two view, it's possible and how i can achieve this feature?

You can't add View controller A and B's views to view controller C in an XIB because there is a fair amount of housekeeping code you need to do to set up the parent/child view controller relationship.
However, if you were to use storyboards, and you are running in iOS 6 or later, this would be trivial. There is a new "container" view in iOS 6. You just add a container view to your scene, and then control-drag from the container view onto another view controller's scene, and IB sets up an embed segue that does all the housekeeping for you. In my view this feature alone is enough to justify moving to storyboards.

You add two UIView in your third UIViewController xib and you link these UIView with IBOulet.
#property (nonatomic, weak) IBOutlet UIView *firstView;
#property (nonatomic, weak) IBOutlet UIView *secondView;
After that you add your UIViewController in theses subviews in the viewDidLoad for example :
- (void)viewDidLoad
{
[super viewDidLoad];
MyFirstViewController *myFirstViewController = [[MyFirstViewController alloc] initWitNibName:#"MyFirstViewController"];
MySecondViewController *mySecondViewController = [[MySecondViewController alloc] initWitNibName:#"MySecondViewController"];
[self.firstView addSubview:myFirstViewController.view];
[self.secondView addSubview:mySecondViewController.view];
}
Improve it with #property of First and Second ViewController and Lazy loading.

Related

Resizing container view programmatically

I have auto layout and auto size classes disabled. I'm currently presenting a child view controller using the container view.
I can't access the container view as a property in my parent view controller, so I don't know how to programmatically set it's size?
I want to do something like this:
#interface ViewController ()
#property (nonatomic) NSContainerView *container;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.container setFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height];
}
But it doesn't seem like there's a NSContainerView class or any public class to access it...
You need to add the container view as an IBOutlet, then you can be able to resize it.

Use of undeclared type ‘UIContainerView’

I’m using xcode 7 , I’ve a storyboard controller with an UIContainerView
When I’m trying to create an outlet to the controller there is this error "Use of undeclared type UIContainerView"
it’s not a bug of xcode 7 because there is the same error on xcode 6
i need to create an outlet because when i switch the segmented control i have to programmatically change the embed of the container
It's an error or i mustn't create an outlet for a container? It's seems that there is not something called UIContainerView in the library, it's strange
There is no such class called UIContainerView. You need to create an outlet of UIView and connect that to your container view.
You can switch the content of container view like:
// Property
#property (nonatomic, weak) IBOutlet UIView *container;
#property (nonatomic, strong) UIViewController *first;
#property (nonatomic, strong) UIViewController *second;
// Method that removes first vc from view and shows second vc
// Assumes first and second properties already initialized
- (void)showSecondVC
{
// Removes first view controller
[self.first.view removeFromSuperview];
[self.first willMoveToParentViewController:nil];
[self.first removeFromParentViewController];
// Shows second view controller
[self addChildViewController:self.second];
[self.second didMoveToParentViewController:self];
self.second.view.frame = self.container.bounds;
[self.container addSubview:self.second.view];
}
It is confusing because IB lablels it as UIContainerView, but it's type is really just a UIView.
UIContainerView is not a class, so you are getting error. Instead use UIView. Container view is actually a concept in storyboard, that allow you to do similar programming stuff:
Initialise a second view controller
Add it as child view controller
Add its view at location of container view with same frame.
When you add a container view then all above stuff is done automatically.
If you want to switch to different view controller then you will create multiple container view. Show and hide container views based on UISegmentedController's selectedIndex

Display UIview controller as a subview using storyboard IOS7

in my application I have a UIView object in second view controller. Now I want to display the UIView as a popup or sub view when I click the button from first view controller. Please tell me how to do this? I have seen many solution for the nib files but I didn't find any for storyboard.
I have connected the IBOulet of view in my second view controller.
#property (strong, nonatomic) IBOutlet UIView *popview;
And i have imported the second view controller in my firstview please tell me how to make this one i have been stuck here for long time please help me out on this one.
Thanks.
In your FirstViewController which has UIView *popview:
- (void)viewDidLoad
{
[super viewDidLoad];
// you don't want to show PopView
self.popview.alpha = 0;
}
- (void) showPopView
{
//you want to show PopView
self.popview.alpha = 1;
}
Try Adding this in your code
-(void)viewDidLoad
{
popView.hidden=true;
}
and when ever user clicks an action change the hidden property to false just like
-(IBAction)AnyActionPerferformed:(id)sender
{
popView.hidden=false;
}
You can Learn about this from
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html
You don't need an extra UIViewController
In your storyboard, drag a new UIView to your view controller. (It will be your popup view)
Link to your view controller. (#property (strong, nonatomic) IBOutlet UIView *popview;)
When you want to hide popview set it's alpha to 0.

UIPageViewController in XIB Error

I have a subclassed view controller and in Interface Builder I want a subview of the VC's view to be a UIPageViewController's view. I set the UIPageViewController's datasource to File's Owner and the view property to the subview of my VC. Can this functionality be achieved? or I can't use the UIPageVC in that way? Most of the examples are for storyboards and not the old VC pushed by NavigationController way. I get this exception '[UIView invalidatePageViewController]'.
I had the same thing happen to me when I had a UIViewController as my root view - and my UIViewController had an attribute defined as:
#property (strong, nonatomic) IBOutlet UIPageViewController *pageViewController;
My IBoutlet was also connected to a UIPageViewController in my xib.
The solution: Remove the IBoutlet and disconnect the UIPageViewController from my xib
#property (strong, nonatomic) UIPageViewController *pageViewController;
Maybe the crash is related to 'there should only be one view controller per view'
Also, in the 'viewdidload' if you do
_pageViewController.view = _apageView;
where _apageView is in your UIViewController as:
#property (strong, nonatomic) IBOutlet UIView *apageView;
This will also cause the same crash to happen - therefore you would need to do:
[_apageView addSubview:_pageController.view];
Then the crash shouldn't happen.

How to create generic view with viewcontroller for iOS5?

I'd like to develop for iOS5 and have a storyboard...
I've just created UIView with UIViewController on the storyboard.
I have added a lot of other UIButtons and labels and creates outlets to VC.
I would like to use this view with it's viewcontroller 3 times on a single parent view.
How is it possible? I dont want to copy "a lot of other UIButtons and labels" ...
Maybe i should create this view out of storyboard in separate XIB? How will i use XIB in storyboard?
UPDATE:
Thanx you, Juzzz - your solution works perfect:
You have to create 2 custom viewControllers (one for each view in your story board.
example:
#interface GraphCollectionViewController : UIViewController
#interface GraphViewController : UIViewController
To connect them you can use something called: UIViewController containment
For your GraphCollectionViewController create 3 outlets for your UIViews. Then create 3 properties of GraphViewController's (or an array, what you want) and initialize them in the view did load.
#property (strong, nonatomic) IBOutlet UIView *topView;
#property (strong, nonatomic) IBOutlet UIView *middleView;
#property (strong, nonatomic) IBOutlet UIView *bottomView;
#property (strong, nonatomic) GraphViewController *topGraphViewController;
#property (strong, nonatomic) GraphViewController *middleGraphViewController;
#property (strong, nonatomic) GraphViewController *bottomGraphViewController;
...
//top graph
self.topGraphViewController = [storyboard instantiateViewControllerWithIdentifier:#"GraphViewController"]; //init with view from storyboard
self.topGraphViewController.view.frame = self.topView.bounds; //set frame the same
[self.view addSubview:self.topGraphViewController.view];
[self addChildViewController:self.topGraphViewController];
[self.topGraphViewController didMoveToParentViewController:self];
//middle graph
self.middleGraphViewController = [storyboard instantiateViewControllerWithIdentifier:#"GraphViewController"]; //init with view from storyboard
self.middleGraphViewController.view.frame = self.middleView.bounds; //set frame the same
[self.view addSubview:self.middleGraphViewController.view];
[self addChildViewController:self.middleGraphViewController];
[self.middleGraphViewController didMoveToParentViewController:self];
//bottom graph
self.bottomGraphViewController = [storyboard instantiateViewControllerWithIdentifier:#"GraphViewController"]; //init with view from storyboard
self.bottomGraphViewController.view.frame = self.bottomView.bounds; //set frame the same
[self.view addSubview:self.bottomGraphViewController.view];
[self addChildViewController:self.bottomGraphViewController];
[self.bottomGraphViewController didMoveToParentViewController:self];
I think you get the point. For more understanding this example .
You can create a new view class (with it's own .h, .m files) and base this on the ViewController you created yourself.
So as for code:
#interface ViewController : UIViewController
Let's say the above is your original ViewController that you want to use in other places.
This has buttons, labels, etc. in it.
When you create a new view class base it on your own instead of UIViewController class like this:
#interface MyNewController : ViewController
Now that MyNewController is based on ViewController you created earlier, it should have it's buttons, labels, etc. you created as well.
Edit: You might also have to change your view's base class in the storyboard.
Click on the view you want to change, look at the right hand side attributes window, under Custom Class choose your own controller, in this case ViewController.

Resources