Swift Popover presentation get ViewController - ios

If I have ViewController1 and do a Popover Presentation to ViewController2. How I can call methods of ViewController1 in ViewController2?

If a child view controller needs to call methods on its parent you might want to reconsider the design and investigate whether you can refactor- normally a view controller should be essentially self-contained and a child shouldn't need to know about its parent.
However, you can accomplish this by implementing prepareForSegue(segue, sender) in the parent view controller and setting appropriate properties on the child (accessed via segue.destinationViewController).

Related

iOS : Why do we need to add child view controller when adding view as subview does the work?

There is a ViewController1 which has a stackView.
I created an instance of ViewController2 and added it's view as a subview to the stackView of ViewController1, I wanted to see if only by doing this does viewDidLoad of ViewController2 is invoked and it did so, ViewController2 viewDidLoad was invoked when I added view of ViewController2 to stackView of ViewController1. For ex: In ViewController1self.stackView.addArrangedSubView(viewControler2.view)
Then why do we need to do addChild(viewController2) then add view as subview, those typical lines which adds childController and it's view in parent view controller hierarchy
Certainly viewDidLoad was called. That happened instantly as soon as you referred to ViewController2's view in your code.
But let's say your ViewController2 does other things besides load a view. Suppose its view contains a button that is hooked through an action to a function in ViewController2. If you now tap that button nothing happens.
That's because the ViewController2 itself is dead: it has vanished in a puff of smoke.
You can see that by implementing deinit in ViewController2. You will see that, just as viewDidLoad is called, so is deinit. You are left with a view controller's view that has no view controller. That is bad.
There is a view controller hierarchy that is responsible for maintaining relations between view controllers. When you add ViewController2 as a child view controller of ViewController1, you maintain that hierarchy, and you maintain it correctly according to the rules, which say:
If VC2's view is somewhere inside VC1's view, then VC2 needs to be a child (at some depth) of VC1.
In other words, the view hierarchy and the view controller hierarchy must run together. Otherwise, the responder chain will break and life will become chaos.
(There are other requirements when you make one view controller the child of another, like sending didMoveToParent to the child as part of the opening dance, along with other message forwarding responsibilities later, so as to ensure that the child view controller gets other messages like viewDidAppear at the right time. It's a complex business. However, I've focussed my answer on the very basic part of what you asked.)
I should add: if your goal was merely to fetch a view out of a nib and stuff it into your own view, you can certainly do that, no problem. What you must not do is use a view controller as a kind of magnet or vacuum cleaner to fetch a view for you if your intention is then to let go of the view controller itself.

Unable to get the concept of popping navigation control in current class

self.navigationController!.popViewControllerAnimated(true)
So in above line i am having confusion that why I am getting navigation controller instance in the current class. According to the storyboard, navigation controller is present outside this class, but how come are we accessing the navigation controller in the current class?
The current viewController is being managed by the navigationController.
You could think of it in a similar way to a parent / child relationship. The navigationController is somewhat functioning like the parent of the viewController.
That property provides a reference to the navigationController provided it exists. In the same way you can say view.superView to access the parent view that the current view is inside.

Pass data between View controllers without using prepareforsegue

I have a table view which is embedded in a container view on my main view controller. In my main VC I have a Navigation Bar which has a bar button item that goes to another View controller, which for demonstration purposes I'll call View controller 2. When I hit save on view controller 2 I have an unwind segue to my main vc (the one with the container view). My question is how do I then pass that data to my container view's child.
NOTE: I cannot use NSUserDefaults as I have a custom class which I want to transfer.
I think you should add a delegate to handle this.When 'unwind segue ' was called,just call the delegate for your child view controller. This is the best way.
If you need the data to be shared by all views, you could consider a singleton class - effectively global data.
Here's a tutorial which explains how to use it

What do delegates catch with a parent and embedded view controller?

I have a view controller (VC1) which has a container view that holds a table view/VC2.
How I can catch things like scrolling in table view (VC2) but from VC1? Can I use delegates?
Also I am implementing a search feature. Do I have to update the embedded VC2 from VC1, is that also done using delegates?
Now I wonder how I can catch things like scrolling in VC2/table view
but from VC1?
You can catch scrolling in the same VC (in your case VC2), then relay that information to VC1 via custom protocols and delegates.
General principle: You should treat embedded view controllers like any view controller.
For example, when you want to pass data to the parent view controller from the embedded view controller you could relay that via delegates. You would use the prepeForSegue method to set the parent view controller as the delegate.
Similarly, when you want to pass data from the parent view controller to the embedded view controller, you can again pass it via the prepareForSegue method. This time you set data from the parent view controller to an embedded view controller variable.
These principles are true for passing data and delegating responsibility to other view controllers, and embedded view controllers should be treated the same way.

How to pass data between PageViewController and ViewController?

In my app i am using PageViewController which has two different View Controllers. I implemented that it works fine. Now what i am trying is to pass some information from MainView Controller to ChildView Controllers when bar button clicked. I tried using protocol,but it doesn't seems to be working.
MainViewController
import ViewControllerA
import ViewControllerB
#protocol MainControllerDelegate<NSObject>
-(void) passInformation :(NSString*)someInfo;
end
MainController<PageViewControllerDelegate>
In
ViewControllerA
What i would like to see is accessing the main controller delegate in child view controller and pass information when some action taken placed in main view controller navigation bar button.
ViewControllerA<MainControllerDelegate> // Can't find the delegate saying undefined
I am sure there will be a way to pass information between UIPageViewController and its Child View controllers. I tried a lot but couldn't find the answer.
Did you import the MainViewController.h file into your ViewControllerA file?
You haven't provided enough information. Post the whole header for both classes, indicating which one is the parent and which is the child. Post the code in the parent view controller that creates the child view controllers. The trick is when you instantiate a view controller that fills one of the pages in your page view controller, you need to set a property in the child that points the child view controller.
I've been working in Mac OS lately, so I'm a bit rusty on page view controllers. If I remember correctly, you pass them an array of the view controllers they manage.
You'd instantiate each of the child view controllers, set up pointers both ways, and then install them in the page view controller.

Resources