子视图控制器如何使用Parentviewcontroller中定义的方法 [英] How child view controller can use the method defined in Parentviewcontroller

查看:49
本文介绍了子视图控制器如何使用Parentviewcontroller中定义的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,子视图控制器试图显示多个视图控制器,并且在执行该操作时,子视图控制器需要从父视图控制器访问播放暂停操作方法. childviewcontroller可以使用在父视图控制器中定义的用于暂停音频播放器,暂停计时器和暂停layer:self.view.layer的播放暂停操作方法来实现此目的.

I have a situation where child view controller is trying to display multiple view controller and while doing that child view controller needs to access play pause action method from the parent view controller. How this can be achieved that play pause action method which is to pause audio player, pause timer and pause layer:self.view.layer and is defined in parent view controller can be used by childviewcontroller.

我将非常感谢您提供的各种帮助来解决这个问题.

I will appreciate so much for all kinds of help to solve this.

谢谢

推荐答案

您可以使用parentViewController属性访问视图控制器的父级.

You can access a view controller's parent with the parentViewController property.

if([self.parentViewController isKindOfClass:[SomeViewController class]]) {
    SomeViewController* viewController = (SomeViewController*)self.parentViewController;

    [viewController foo];
}

但是,这取决于视图控制器之间的关系.从您的问题中,我推断您与多个孩子有亲子关系,但是如果我错了,请纠正我!这与模态视图控制器的演示非常不同,后者仅显示一个视图控制器,并且需要用户立即注意.

However, this depends on the relationship between your view controllers. From your question, I inferred that you had a parent-child relationship with multiple children, though please correct me if I am wrong! This is very different from a modal view controller presentation, in which only one view controller is presented and it demands the user's immediate attention.

说明:

关于UIViewController的parentViewControllerpresentingViewController属性之间的差异似乎有些困惑.有两种不同的视图控制器关系,每种关系都适用于这些属性之一.

There seems to be some confusion about the difference between the parentViewController and presentingViewController properties on UIViewController. There are two different view controller relationships, each of which applying to one of these properties.

如果您希望将多个视图控制器的视图添加为父视图控制器的子视图,请使用

If you wish to add multiple view controllers' views as subviews of a parent view controller, you use view controller containment. In this situation, any of the views added as subviews (children) of the parent view controller will return the parent view controller (which controls the superview of the children; the parent view) when the parentViewController property is accessed. In this situation, the presentingViewController property returns null.

例如,在父视图控制器中:

For example, in the parent view controller:

- (void)viewDidLoad {
    [super viewDidLoad];

    SomeViewController* someVC = [[SomeViewController alloc] init];

    [self addChildViewController:someVC];
    [self.view addSubview:someVC.view];
    [someVC.view setFrame:<SOME_FRAME>];
    [someVC didMoveToParentViewController:self];

    AnotherViewController* anotherVC = [[AnotherViewController alloc] init];

    [self addChildViewController:anotherVC];
    [self.view addSubview:anotherVC.view];
    [anotherVC.view setFrame:<ANOTHER_FRAME>];
    [anotherVC didMoveToParentViewController:self];

    /* this prints self */
    NSLog(@"%@", someVC.parentViewController);

    /* this prints null */
    NSLog(@"%@", someVC.presentingViewController);


    /* this prints self */
    NSLog(@"%@", anotherVC.parentViewController);

    /* this prints null */
    NSLog(@"%@", anotherVC.presentingViewController);
}

相反,如果您只希望呈现一个模态视图控制器(这种情况比上面的一对多父子关系更为常见),则使用presentingViewController属性.

Contrarily, if you simply wish to present a single, modal view controller (a situation which is more common than the one-to-many parent-child relationship above), then the presentingViewController property is used.

例如,在呈现视图控制器中:

For example, in the presenting view controller:

- (void)someActionTriggered {
    SomeViewController* viewController = [[SomeViewController alloc] init];
    
    [self presentViewController:viewController animated:YES completion:nil];

    /* this prints null */
    NSLog(@"%@", viewController.parentViewController);

    /* this prints self, or a tab bar controller if 'self' is contained in one */
    NSLog(@"%@", viewController.presentingViewController);
}

尽管presentingViewController可能由于iOS中模态视图控制器模式的普遍使用而更常见,但是视图控制器的视图控制器包含父子关系绝对合法,并且parentViewControllerchildViewController属性自iOS 5起,不再弃用UIViewController的 ,它们的用法刚刚发生了变化.您可以从文档中阅读以下摘录:

Although presentingViewController might be seen more commonly due to the prevalence of the modal view controller pattern in iOS, the view controller containment parent-child relationship of view controllers is absolutely legitimate, and the parentViewController and childViewController properties of a UIViewController have not been deprecated as of iOS 5, their use has just changed. You can read this excerpt from the documentation:

讨论

如果接收者是容器视图控制器的子代,则此属性保存包含在其中的视图控制器.如果接收者没有父代,则此属性中的值为nil.

If the recipient is a child of a container view controller, this property holds the view controller it is contained in. If the recipient has no parent, the value in this property is nil.

在iOS 5.0之前,如果某个视图没有父视图控制器并且正在显示,则将返回该显示视图控制器.在iOS 5上,此行为不再发生.而是使用presentingViewController属性访问展示视图控制器.

Prior to iOS 5.0, if a view did not have a parent view controller and was being presented, the presenting view controller would be returned. On iOS 5, this behavior no longer occurs. Instead, use the presentingViewController property to access the presenting view controller.

这篇关于子视图控制器如何使用Parentviewcontroller中定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆