如何从视图控制器中包含的视图中调用方法?

时间:2014-01-21 15:22:49

标签: ios objective-c uiview uiviewcontroller

我有一个UIViewController,其中包含一个UIView Subclass,来自我要调用UIViewController中定义的包含它的子类。我不想实例化视图控制器的新实例,因为它包含我尝试调用的方法中需要的信息。这是一个试图进一步澄清的图表:

(UIViewController)MainView - >有方法updateView

(UIView)SubView --->有按钮播放动画并有完成块

我想在完成块中调用UpdateView

2 个答案:

答案 0 :(得分:4)

我认为您可以在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

}

答案 1 :(得分:0)

对于“正确”实施,您需要在UIView Subclass中查看控制器的参考:

@interface UIViewSubclass : UIView
...
@property UIViewControllerSubclass *viewController;
@end

然后将此viewController引用设置为视图控制器并在完成块中使用它。

如果您需要本地解决方案(而不是使用属性扩展UIViewSubclass),请查看以下答案:https://stackoverflow.com/a/3732812/326017

相关问题