我在 uiviewcontroller 内的 uiScrollview 内有一个 uibutton。当我单击按钮时,我希望能够调用 uiviewcontroller 中的方法。
我在我的按钮上执行以下操作,调用我的 uiscrollview 中的方法:
[categoryButton addTarget:self action:@selector(categoryButtonWasClicked:) forControlEvents:UIControlEventTouchUpInside];
但这只是调用了我的 uiscrollview 中的一个方法(我仍然会保留它)。我该如何调用 View Controller 中的方法??
感谢您提供的任何帮助。
最佳答案
在此处输入代码
尝试做这样的事情
#import <UIKit/UIKit.h>
@protocol myUIScrollViewProtocol <NSObject>
-(void)buttonWasPressInScrollView:(id)sender;
@end
@interface MyUIScrollView : UIScrollView
@property (weak, nonatomic)id myDelegate;
@end
创建 UIScrollView 的子类并添加一个委托(delegate),然后将您的 UIViewController 指定为委托(delegate)并在其中实现“buttonWasPress..”方法。
然后从您的 uiScrollView 中的 categroryButtonWasClicked 方法:
-(void)categoryButtonWasClicked{
if ([self.myDelegate respondsToSelector:@selector(buttonWasPressInScrollView:)]){
[self.myDelegate buttonWasPressInScrollView:self];
}
...
}
在您的 viewController 的 .h 中添加以下内容
@interface MyViewController : UIViewController <myUIScrollViewProtocol>
关于ios - 从 uiscrollview 中的 uibutton 父 uiviewcontroller 中的触发方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11780005/