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.
Related
I have three view controllers each under the same custom view controller class, and a page view controller. I want to be able to reuse these three view controllers but with different content on their subviews. However, when I try to instantiate one of these view controllers from the page view controller using [self.storyboard instantiateViewControllerWithIdentifier:identifier], with a method to find the subview by tag right after, the subview returned is null. Is there some way I can get the subview by tag right after instantiating the view controller programmatically?
A view controller's views don't get created until it is about to be displayed. They won't be created after the call to instantiateViewControllerWithIdentifier:
You should put code that accesses the view controllers views in viewDidLoad, viewWillAppear, or viewDidAppear.
You should not try to manipulate a view controller's views from an outside object. That violates the principle of encapsulation, an important principle of object-oriented design. (It also often doesn't work, as you found out.)
If you need do things to the views programmatically you should add one or more public methods to the view controller and call those methods to ask the view controller to make adjustments to its views.
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
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).
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.
I've two different views which have two different controllers(firstVC and secondVC). Initially these two views appear in different viewcontrollers. Now, for some reason they have to appear in one viewcontroller and make some changes on one view when some action is done in second view, I need to create another viewcontroller(thirdVC).
Can I just add firstVC.view and secondVC.view on thirdVC's view and create some delegate methods of firstVC and secondVC to be implemented in thirdVC so that I do not need to make changes to existing firstVC and secondVC?
A view controller whose view contains subviews managed by other view controllers is called a "container view controller". This is a supported pattern but you need to see Implementing a Container View Controller and add the child view controllers to their parent container view controller in addition to adding their views as subviews.
This ensures that 1. the parent controller retains the child controllers so that their views do not send messages to deallocated controllers and 2. correctly sends framework messages like rotation events are sent to the child controllers. If you do not use these view controller containment methods your child controllers will eventually break in surprising ways.