![](/img/trans.png)
[英]calling a parent UIViewController method from a child UIViewController
[英]Trigger method in parent uiviewcontroller from uibutton in uiscrollview
我在uiviewcontroller內的uiScrollview內有一個uibutton。 當我單擊按鈕時,我希望能夠在uiviewcontroller中調用方法。
我在按鈕上執行以下操作,該按鈕在uiscrollview中調用一個方法:
[categoryButton addTarget:self動作:@selector(categoryButtonWasClicked :) forControlEvents:UIControlEventTouchUpInside];
但這只是在uiscrollview中調用了一個方法(我將繼續保留)。 然后如何在ViewController中調用方法?
感謝您提供任何幫助。
enter code here
嘗試做這樣的事情
#import <UIKit/UIKit.h>
@protocol myUIScrollViewProtocol <NSObject>
-(void)buttonWasPressInScrollView:(id)sender;
@end
@interface MyUIScrollView : UIScrollView
@property (weak, nonatomic)id myDelegate;
@end
創建UIScrollView的子類並添加委托,然后將UIViewController分配為委托,並在其中實現“ buttonWasPress ..”方法。
然后從uiScrollView中的categroryButtonWasClicked方法中:
-(void)categoryButtonWasClicked{
if ([self.myDelegate respondsToSelector:@selector(buttonWasPressInScrollView:)]){
[self.myDelegate buttonWasPressInScrollView:self];
}
...
}
在您的viewController的.h中添加以下內容
@interface MyViewController : UIViewController <myUIScrollViewProtocol>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.