Linking Child View Controllers to a Parent View Controller Within Storyboard

Linking child view controllers to a parent view controller within storyboard

As something of a combo of Caleb and Matt's answers, I did:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"cpdc_check_embed"]) {
self.checkVC = segue.destinationViewController;
}
}

...where checkVC is a property on the container controller:

@property (weak,nonatomic) PXPCheckViewController * checkVC;

You just have to set your embed segue's Storyboard ID to whatever you want (in this case, cpdc_check_embed):

check embed screen in Xcode

...and then check the identifier in -prepareForSegue:sender:.

Still not an outlet, but cleaner than Matt's (IMHO) and more specific than Caleb's, and you still get a nice-looking storyboard:

Sample Image

Reuse parent ViewController storyboard file in Child ViewController

I founded the answer to reuse the story board file or inherite the story board file.

object_setClass(Sets the class of an object.) will override instance of AViewController with BViewController Class. So on top of AViewController you can add some more methods.

when you have similar viewcontroller with small changes. you have to create different viewcontrollers. using this method you can create on basic viewcontroller with storyboard and reuse that viewcontroller .

 class BViewController{
static func vcInstanceFromStoryboard() ->BViewController? {
let storyboard = UIStoryboard(name: "AViewController"), bundle: UIBundle.main)
let instance = storyboard.instantiateInitialViewController() as? AViewController
object_setClass(instance, BViewController.self) //
return (instance as? BViewController)!
}
.....
}

This is an example of how do we use it:
let vc = BViewController.vcInstanceFromStoryboard()
self.present(vc , animation : true)

how to manage which controller should show in parent view controller?

I don't know what is wrong with storyboard but my problem was :
because I adde 5 container view to my main view controller and connected all of them to their view controllers by segue it presents all of them and then close main view controller .
I clean all segue and container views from storyboard and I did it like this :

private lazy var firstViewController: AvailableView = {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
var viewController = storyboard.instantiateViewController(withIdentifier: "AvailableViewID") as! AvailableView
self.add(asChildViewController: viewController)

return viewController
}()

private lazy var secondViewController: NotificationView = {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
var viewController = storyboard.instantiateViewController(withIdentifier: "NotificationViewID") as! NotificationView
self.add(asChildViewController: viewController)
return viewController
}()

private func add(asChildViewController viewController: UIViewController) {
addChild(viewController)
view.addSubview(viewController.view)
viewController.view.frame = view.bounds
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
viewController.didMove(toParent: self)
}

private func remove(asChildViewController viewController: UIViewController) {
viewController.willMove(toParent: nil)
viewController.view.removeFromSuperview()
viewController.removeFromParent()
}

and the way you can use it :
in viewDidLoad():

        add(asChildViewController: firstViewController)

and when you wanted to present second view controller you should remove first View Controller and then add your second view controller like so:

remove(asChildViewController: firsttViewController)
add(asChildViewController: secondViewController)

you can see this link for more explanation : https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/

hope to help any one else :)

Position Child View Controller Inside Parent

You're getting this error because you are adding fact as a subview instead of header, so since header is not in the view hierarchy it cannot have constraints between it and other views that are in the view hierarchy:

view.addSubview(fact)
[...]
header.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

Change this to:

view.addSubview(header)
[...]
header.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

Also, every time you say QuickFact() you are creating a brand new instance of a QuickFact view controller. You need to call this initializer once and use the resulting instance to do whatever you want with it. If you don't then you'll get the same error as before, but on this line:

QuickFact().view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

You'll want to change your addQuickFactView function to something like this:

func addQuickFactView() {
// Make a single instance of QuickFact and use it later on:
let quickFactVC = QuickFact()

addChild(quickFactVC)
view.addSubview(quickFactVC.view)
quickFactVC.didMove(toParent: self)
quickFactVC.view.translatesAutoresizingMaskIntoConstraints = false
quickFactVC.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
quickFactVC.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
quickFactVC.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
quickFactVC.view.heightAnchor.constraint(equalToConstant: 200).isActive = true
}

Child View Controller within Parent View Controller

Try to change pageController's frame to the following.

CGRect frame = self.view.frame;

CGRect insetFrame = CGRectInset(frame, frame.size.width * 1/8, frame.size.height * 1/8);

How to navigate a child view controller from other child view controller in objective c

you can just drag a segue from child1's button to child2, and child2's button to child1, though the storyboard will look ugly, but it works. or you can just use

[self.navigationController pushViewController:child2 animated:YES];

Add Child View Controllers to parent View Controller with gestures programmatically

I'm assuming jobListController is a UIViewController? You need to add gesture recognizers to its view (jobListController.view). Not sure if that's the only issue you're having, but that'll fix the crash.

calling a parent UIViewController method from a child UIViewController

One easy way to achieve this you can use NSNotificationCenter for that.

In your ParentViewController add this into viewDidLoad method:

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: Selector(("refresh:")), name:NSNotification.Name(rawValue: "refresh"), object: nil)
}

After that add this function in ParentViewController which will get called when you dismiss your ChildViewController:

func refreshList(notification: NSNotification){

print("parent method is called")
}

and into your ChildViewController add this code where you dismissing your child view:

 @IBAction func btnPressed(sender: AnyObject) {

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refresh"), object: nil)
self.dismiss(animated: true, completion: nil)
}

Now when you dismiss child view refreshList method will be call.



Related Topics



Leave a reply



Submit