ios - 从 uiscrollview 中的 uibutton 父 uiviewcontroller 中的触发方法

标签 ios xcode ipad uiviewcontroller uiscrollview

我在 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/

相关文章:

ios - 如何强制我的父 ViewController 在从 subview 中展开后重新加载?

iphone - iOS XCode native vs PhoneGap 的开发速度

iphone - 核心数据 - 保存电子邮件地址

ios - iPhone 上的 Safari 是否支持为 &lt;input type =“file” multiple =“multiple” > 元素选择多个文件?

iphone - iOS 自定义 TabBar 高度

ios - 将字典保存到核心数据

ios - 修改 IBM MobileFirst 7.1 为混合 iOS 应用程序创建的权利文件

ios - 在另一个项目中使用一个项目的应用程序委托(delegate)/ View Controller

ios - Swift 3. 尝试从 viewController 更新原型(prototype)单元类中声明的一些 IBOutlet

ios - 如何在 iOS 中更改启动图像的属性?