How Does View Controller Containment Work in iOS 5

How does View Controller Containment work in iOS 5?

The UIViewController docs are pretty clear on when and when not to call willMove/didMove methods. Check out the "Implementing a Container View Controller" documentation.

The docs say, that if you do not override addChildViewController, you do not have to call willMoveToParentViewController: method. However you do need to call the didMoveToParentViewController: method after the transition is complete. "Likewise, it is is the responsibility of the container view controller to call the willMoveToParentViewController: method before calling the removeFromParentViewController method. The removeFromParentViewController method calls the didMoveToParentViewController: method of the child view controller."

Also, there is an example worked out here and sample code here.

Good Luck

iOS view controller containment | Child View Controller passes touches to Parent View Controller

You've got it backwards. Controller containment is one thing, but this is just standard view hierarchy and responder chain stuff. So the touches are initially sent to the parent; specifically a call is made to its view's -(UIView *)hitTest:withEvent:. That returns a child view if one can be found. Otherwise it returns one of its own views. Possibly it returns itself.

If a touch is being captured by the parent controller that means it's never been sent to the child. Not that it's being sent to the child then ascending up to the parent.

If you want the parent to do something unusual with touch capture then just use a custom view and implement your own -hitTest:withEvent:. As a first step, do that and just log the inputs to see what happens.

Using iOS 5 Containment API and reflect sliding gestures down to the child view controller

I don't know if this is what you want, but I've implemented something like this using a scroll view with four container views in the scroll view's content view. Each of those container views has an embedded segue to a view controller (would be a tableViewController in your case).

So this is how I did it:

  1. I added a scroll view to my view controller
  2. I dragged in a UIView onto the scroll view and set its frame to {{0,0},{640,548}}
  3. Added 4 container views underneath the view from step 2 (by underneath, I mean underneath the view in the objects list on the left side of the screen. It was easier to do it this way since the view doesn't show its full width on the canvas). I set their frames to {{0,0},{160,548}} {{160,0},{160,548}} {{320,0},{160,548}} and {{480,0},{160,548}}. This gives you four view controllers, each half the width of the screen.
  4. Added another full size view under the scrollview (under in the list, but that means on top in the view hierarchy) and made its background color clear. This view sits on top and intercepts all the touches.
  5. Added 2 swipe gesture recognizers (one left on right) to the view from step 4.

In the view controller, I created IBOutlets to the scroll view (scroller) and to the large (the 640 x 548 one) view (content), and added this code in the .m file:

@implementation ViewController

-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.scroller.contentSize = self.content.frame.size;
}

-(IBAction)moveLeft:(id) sender {
if (self.scroller.contentOffset.x < 320)
[self.scroller setContentOffset:CGPointMake(self.scroller.contentOffset.x + 160, 0) animated:YES];
}

-(IBAction)moveRight:(id) sender {
if (self.scroller.contentOffset.x > 0)
[self.scroller setContentOffset:CGPointMake(self.scroller.contentOffset.x - 160, 0) animated:YES];
}

@end

The 2 gesture recognizers were connected in IB to the moveLeft: and moveRight: methods. So, this gives me the ability to show controllers 1&2, 2&3, or 3&4 on the screen at the same time.

Container View Controllers - notify parent of action

You can either use delegate or block;

Using delegate

Create a protocol :

@protocol SomeProtocol 
- (void)someAction;
@end

Just declare a delegate in HomeViewController.h like this:

id delegate; 

and then in MainViewController's viewDidLoad set it like this:

homeVC.delegate = self;
//some where in MainViewController implement the protocol method
-(void)someAction
{
//do something
}

then when you press the button in homeVC, just simply call:

if ([self.delegate respondsToSelector:@selector(someAction)]) {
[self.delegate someAction];
}

Using Block:

In HomeViewController.h declare a block property:

typedef void (^ActionBlock)();

@property (nonatomic, copy) ActionBlock block;

then in MainViewController ViewDidLoad:

homeVC.block = ^(){
//do something
};

then when you press the button in homeVC, just simply call:

self.block();


Related Topics



Leave a reply



Submit