当前的子视图控制器

时间:2013-01-18 18:41:31

标签: iphone ios objective-c xcode

我正在尝试在我的viewcontroller上实现SwipeGesture。

我目前面临的问题是我无法确定当前显示的子视图控制器是什么。

swipeGesture被添加到容器视图控制器中。然后必须确定父子关系中当前显示的VC是什么,然后向右或向左移动。

2 个答案:

答案 0 :(得分:5)

如果您正在谈论自定义容器视图控制器,那么容器控制器的工作就是跟踪它。因此,您可能拥有自己的@property,可以跟踪您所在的transitionFromController,并在滑动后按transitionFromViewController进行调整。所以你可能有一个数字属性,当你向右移动时你会递增,而当你向左移动时,你会递减。

一般情况下(如果您只是想跟踪哪个“来自”控制器,您传递给viewDidLoad),有两种方法。清单14-3在 View Controller Programming Guide的Adding a child controller中列出了一种方法。

在这种情况下,在addChildViewController中,您为第一个视图控制器执行addChildViewController。在这种情况下,您不会在此时通过[self.childViewControllers lastObject]加载其他子视图控制器,而是让您执行转换的方法处理该问题(如清单14-3所示)。 / p>

如果你这样做,你可以抓住@interface ViewController () @property (nonatomic) NSInteger currentIndex; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIViewController *initialController = ... // set your initial controller [self addChildViewController:initialController]; initialController.view.frame = self.containerView.bounds; [self.containerView addSubview:initialController.view]; [initialController didMoveToParentViewController:self]; UISwipeGestureRecognizer *gesture; gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; gesture.direction = UISwipeGestureRecognizerDirectionRight; [self.containerView addGestureRecognizer:gesture]; gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; gesture.direction = UISwipeGestureRecognizerDirectionLeft; [self.containerView addGestureRecognizer:gesture]; } - (void) handleSwipe:(UISwipeGestureRecognizer *)gesture { if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1)) { self.currentIndex++; UIViewController *newController = ... // set your new controller UIViewController *oldController = [self.childViewControllers lastObject]; [self cycleFromViewController:oldController toViewController:newController direction:gesture.direction]; } else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0) { self.currentIndex--; UIViewController *newController = ... // set your new controller UIViewController *oldController = [self.childViewControllers lastObject]; [self cycleFromViewController:oldController toViewController:newController direction:gesture.direction]; } } - (void) cycleFromViewController:(UIViewController*) oldController toViewController:(UIViewController*) newController direction:(UISwipeGestureRecognizerDirection)direction { [oldController willMoveToParentViewController:nil]; [self addChildViewController:newController]; newController.view.frame = oldController.view.frame; UIViewAnimationOptions options; if (direction == UISwipeGestureRecognizerDirectionRight) options = UIViewAnimationOptionTransitionFlipFromLeft; else if (direction == UISwipeGestureRecognizerDirectionLeft) options = UIViewAnimationOptionTransitionFlipFromRight; [self transitionFromViewController:oldController toViewController:newController duration:0.33 options:options animations:^{ [oldController removeFromParentViewController]; } completion:^(BOOL finished) { [newController didMoveToParentViewController:self]; }]; } ,这将是你的“当前”子控制器。所以,这可能看起来像:

addChildViewController

我见过的其他模型是通过viewDidLoad中的childViewControllers加载所有潜在的子控制器。我个人并不喜欢这种方法,但我知道人们这样做,而且效果很好。

但如果你这样做,你就不能再依赖currentChildController来知道哪个控制器是当前控制器。在这种情况下,您必须为@interface ViewController () @property (nonatomic, strong) UIViewController *currentChildController; @property (nonatomic) NSInteger currentIndex; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildOne"]]; [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildTwo"]]; [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildThree"]]; self.currentChildController = self.childViewControllers[0]; self.currentChildController.view.frame = self.containerView.bounds; [self.containerView addSubview:self.currentChildController.view]; for (UIViewController *controller in self.childViewControllers) [controller didMoveToParentViewController:self]; UISwipeGestureRecognizer *gesture; gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; gesture.direction = UISwipeGestureRecognizerDirectionRight; [self.containerView addGestureRecognizer:gesture]; gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; gesture.direction = UISwipeGestureRecognizerDirectionLeft; [self.containerView addGestureRecognizer:gesture]; } - (void) cycleFromViewController:(UIViewController*) oldController toViewController:(UIViewController*) newController direction:(UISwipeGestureRecognizerDirection) direction { self.currentChildController = newController; newController.view.frame = oldController.view.frame; UIViewAnimationOptions options; if (direction == UISwipeGestureRecognizerDirectionRight) options = UIViewAnimationOptionTransitionFlipFromLeft; else if (direction == UISwipeGestureRecognizerDirectionLeft) options = UIViewAnimationOptionTransitionFlipFromRight; [self transitionFromViewController:oldController toViewController:newController duration:0.33 options:options animations:^{ } completion:nil]; } - (void) handleSwipe:(UISwipeGestureRecognizer *)gesture { if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1)) { self.currentIndex++; UIViewController *oldController = self.currentChildController; UIViewController *newController = self.childViewControllers[self.currentIndex]; [self cycleFromViewController:oldController toViewController:newController direction:gesture.direction]; } else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0) { self.currentIndex--; UIViewController *oldController = self.currentChildController; UIViewController *newController = self.childViewControllers[self.currentIndex]; [self cycleFromViewController:oldController toViewController:newController direction:gesture.direction]; } } 定义自己的类属性。所以它可能看起来像:

[self.childViewControllers lastObject]

这是两种合乎逻辑的方法。

顺便说一下,第一种方法并不排除拥有自己的类属性来知道你所使用的控制器,有时这很有用(例如,如果你使用的动画类型取决于你是否会去“正确”或“左”,但除非是这种情况,否则只需看{{1}}即可逃脱。

答案 1 :(得分:0)

在我正在进行的项目中,我有许多View Controller,每个都有自己的导航控制器添加到父视图控制器。我知道最初的入学孩子,但我会做以下事情来找到孩子当前显示的孩子:

[myKnownViewController.childViewControllers objectAtIndex:0].navigationController.topViewController