简体   繁体   English

获取UIView的容器/ UIViewController

[英]getting the container/UIViewController of UIView

So I have a UIViewController A which adds a UIView B as a subclass. 所以我有一个UIViewController A,它添加了一个UIView B作为子类。 The UIView B has a UITableView. UIView B有一个UITableView。 I want the UITableView scrollView delegate to be in the UIViewController A. How do I do this? 我希望UITableView scrollView委托在UIViewController A中。我该怎么做? As of now the scrollViewDidScroll delegate is inside this UIView class. 截至目前,scrollViewDidScroll委托在此UIView类中。 Is there a way so that the scrollViewDidScroll is inside viewController A and is called whenever the UITableView in the UIView is scrolled? 有没有办法使scrollViewDidScroll在viewController A中,并且只要滚动UIView中的UITableView就会调用它?

Make the tableView as a property accessible from the outside. 将tableView作为可从外部访问的属性。 Than you could set it up in your ViewController A like that: 你可以在ViewController A中设置它,如下所示:

// ClassB.h
@property (nonatomic, readonly) UITableView* tableView;


// In your UIViewControllerA.m
// in loadView: or anywhere else
self.viewB.tableView.delegate = self;

So result is: your ViewController is the delegate. 结果是:您的ViewController是委托。

The scroll view and the table view are the same object -- notice that UITableView is a subclass of UIScrollView. 滚动视图和表视图是相同的对象 - 请注意UITableView是UIScrollView的子类。 That object has only one delegate, not separate delegates for the table stuff and for the scroll stuff. 该对象只有一个委托,而不是表格内容和滚动内容的单独委托。 Also notice that UITableViewDelegate adopts UIScrollViewDelegate. 另请注意,UITableViewDelegate采用UIScrollViewDelegate。 So, the object you set as the table's delegate will also get UIScrollViewDelegate messages. 因此,您设置为表的委托的对象也将获得UIScrollViewDelegate消息。

Now, there's no reason that your table's delegate can't forward messages about scrolling to some other object. 现在,您的表的委托没有理由不能将有关滚动的消息转发给其他对象。 You'd have to set that up yourself, of course. 当然,你必须自己设置它。 So, when B gets a -scrollViewDidScroll: message, it might send an equivalent message to A, or whatever. 因此,当B获取-scrollViewDidScroll:消息时,它可能会向A发送等效消息,或者其他任何消息。 I'd think twice before actually doing that, though... I'd try to have just one object (the view controller) be responsible for everything related to the table. 在实际执行此操作之前,我会三思而行,但是......我会尝试只有一个对象(视图控制器)负责与表相关的所有事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM