[英]How Do I call a method from a view contained in a view controller ?
我有一个包含UIView Subclass
的UIViewController
,我想从该子类中调用包含它的UIViewController
中定义的方法。 我不想实例化视图控制器的新实例,因为它包含我尝试调用的方法中所需的信息。 这是试图进一步阐明的图表:
(UIViewController)MainView->具有方法updateView
(UIView)SubView --->具有播放动画并具有完成块的按钮
我想在完成块中调用UpdateView
我认为您可以在Subview中设置协议,该协议可以由ViewController实施
您的SubView.h
@class SubView;
@protocol SubViewDelegate <NSObject>
- (void)actionToPromoteToViewController: (NSString *)exampleString isSelected:(BOOL)exampleBool;
@end
然后,在您的ViewController.h中:
@interface MainViewController : UIViewController <SubViewDelegate>
和您的ViewController.m,实现该方法。
- (void)actionToPromoteToViewController: (NSString *)exampleString isSelected:(BOOL)exampleBool{
// Method Implementation
}
为了实现“正确”,您需要在UIView Subclass
引用视图控制器:
@interface UIViewSubclass : UIView
...
@property UIViewControllerSubclass *viewController;
@end
然后将此viewController
引用设置为您的视图控制器,并在完成块中使用它。
如果您需要本地解决方案(而不是使用属性扩展UIViewSubclass
),请查看以下答案: https : UIViewSubclass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.