Child view controller's view is not shown in a container - ios

I have a scene in the storyboard with a Container View. As you may suppose, I want its associated UIViewController to load there another UIViewController as a child view controller, and to show its view as a subview.
So, in the parent UIViewController I defined the properties:
#property (weak, nonatomic) IBOutlet UIView *containerView;
#property (strong, nonatomic) MyChildViewController *childController;
Then, in the viewDidLoad method of the parent, I do:
if (self.childController == nil) {
self.childController = [[MyChildViewController alloc] init];
}
[self addChildViewController:self.childController];
self.childController.view.frame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
[self.containerView addSubview:self.childController.view];
[self.childController didMoveToParentViewController:self];
Both self.childController.view and self.containerView are not nil, but self.childController.view is not displayed when I run the app... What can I be missing?
Thanks

I finally noticed what I was overlooking... since I also created a scene in the storyboard for MyChildViewController, I needed to instantiate it from there.

Related

iOS Container View and Parent ViewController communication?

How can I communicate with container view controller and parent view controller? I've tried to make a communication and pass the value between them. I've added a container view in the green view controller. And I had set segue name with "communicateSegue". I've searched some methods, I know we can set the prepareForSegue to pass the value at initial.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"communicateSegue"]) {
NSLog(#"call prepare for segue at parent view controller");
}
}
But now I want to pass value and change the label in the different viewcontroller . Not at the initial.
I don't know how to do?
Question:
Have anyone know how to implement ParentViewController pass value to Container View Controller and show the label in the ContainerViewController?
And reverse , Container View Controller click the button then pass the value and change the label show in the ParentViewController with objective-c?
Thanks.
My declare variable in parentViewController.h:
#import
#interface ParentViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *parentTF;
#property (weak, nonatomic) IBOutlet UIButton *passValToChildVCBtn;
#property (weak, nonatomic) IBOutlet UILabel *showContainerValLB;
#property (weak, nonatomic) IBOutlet IBAction *passValueToContainerEvent;
#end
In the ContainerChildViewController.h
#import
#interface ContainerChildViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *showParentValLB;
#property (weak, nonatomic) IBOutlet UITextField *childTF;
#property (weak, nonatomic) IBOutlet UIButton *passToParentBtn;
#property (weak, nonatomic) IBOutlet IBAction *passValueToParentBtnEvent;
#end
test project in the here.
thank you very much.
Regarding passing value to the containerView controller, you can make a property in the ChildViewController of the value you want it to be passed. Then in your ParentViewController do something like the following:
self.ChildViewController.yourProperty = yourValue
The opposite can be done in 4 ways:
You can make a delegate protocol to communicate the data between your controllers.
You can post a notification in your ChildViewController and add the parent controller as an observer.
You can use KVO.
And the easiest way out, you can make a property in your parentviewController and access it like the following
((YourParentViewControllerClassType *)self.parentViewController).yourParentProperty = TheValueYouWant;
I do it like this:
set identifier for segue from your containerView to viewController
make property/variable myChildViewController in your parent view controller and property/variable myParentViewController in your child view controller
in your prepareForSegue method set this properties to be the reference to right objects
Now you can access to properties of parent/child view controller through this references.
Example in Swift:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourIdentifier" {
// Set references
myChildViewController = segue.destinationViewController as! YourChildViewController
myChildViewController.myParentViewController = self
}
}
Example in Objective C
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"YourIdentifier"]) {
// Set references
myChildViewController = [segue destinationViewController];
myChildViewController.myParentViewController = self
}
}
I did't try the objective C code becouse I don't ever use it. It is just an example to get the idea.
After that you can set any property of parent from child view controller as:
myParentViewController.property = value
and in your parent view controller:
myChildViewController.property = value
Hope this helps anyone!
Is pretty simple, the UIStoryboardSegue contains a reference to your destination view controller using the property -destinationViewController.
In the method -prepareForSegue you can access it and pass the data that you need.
If you need to pass data from child to parent, I strongly suggest you to avoid hard coupling and use something such as delegation or notification pattern.
Remember that your parent view controller already contains a reference to you child view controllers and you can simply access it by using -childViewControllers property.
you can use NSNotificationCenter for updating all screens or you can do this way..
create a property in ContainerChildViewController.h
#property(nonatomic,copy)NSString * mytextString;
this property will be accessible in your ParentViewController.h if you import ContainerChildViewController.h
you have to create a NSString in ParentViewController.h
in this you can set button action to copy textfielddata.text
then you can assign this value to your ContainerChildViewcontroller's
as
// create an instance of 2nd view controller as second in ParenViewController
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ContainerChildViewController * second= [mystoryboard instantiateViewControllerWithIdentifier:#"ContainerChildViewController"];
second.mytextString = self.mytextString;
// either you directly pass this value or use the string it should work both ways
NSLog(#"%#",second.orderID);
[self.navigationController pushViewController:second animated:YES];
Reply if it works
try this
- (IBAction)showDetail:(UIButton *)sender {
DetailViewController *detailVc = [self.childViewControllers firstObject];
detailVc.lable.text = sender.titleLabel.text;
}

Create Container view for Two UIViewController on iPad

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.

How to get custom UIView from Storyboard

In FatEditViewController, has a custom UIView.
FatEditViewController.h
#import <UIKit/UIKit.h>
#interface FatEditViewController
#property (weak, nonatomic) IBOutlet UIView *myview;
#end
In another UIViewController, I want to get the myview object.
I do like this:
FatEditViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"FatEdit"];
UIView *view = [vc myview];
NSLog(#"%#, %#", vc,view);
And the myview is null.
How to get the myview?
The problem here is that your myview gets instantiated only when your FatEditViewController's method viewDidLoad is called, which of course is called only when the view controller is presented on screen. One possible way to achieve what you need is to build myView programmatically (it wouldn't be an IBOutlet anymore), possibly by lazy initialising it in the getter. You would have to add the view to your FatEditViewController in the viewDidLoad method programmatically as well.
-(UIView *)myview
{
if(!_myview){
_myview = [[UIView alloc] initWithFrame:whateverFrameYouNeed];
//build the view
}
return _myview;
}
The property would have to be strong as well.

Accessing UIScrolView form other controller

I have a UIScrollView which is added modally form a segue in UIStoryboard. I would like to access a UIScrollView in UIViewController ViewController from controller B, I tried this code:
ViewController *linkToA;
linkToA = [[ViewController alloc] init];
linkToA = (ViewController*)self.presentingViewController;
linkToA.groupPass.text = #"cool party ajdaj";
[linkToA.scroll1.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
The code doesn't give errors, but doesn't do anything.
I import the controller in this way:
#import "ViewController.h"
and in the ViewController.h I have:
#property (strong, nonatomic) IBOutlet UIScrollView *scroll1;
What is the problem??
linkToA IBOutlets will be not accesible while linkToA view controller will not be pushed to nav controller or presented modally

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