- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想要一个父 ViewController
来处理由它的一个子 ViewController
生成的 Target-Actions。
根据 Apple 文档,这应该可以通过将 target
设置为 nil
并遵循响应链来实现。
If you specify nil for the target object, the control searches the responder chain for an object that defines the specified action method. https://developer.apple.com/documentation/uikit/uicontrol
但是当target
为nil
时,我该如何编写action 方法签名呢?
通常,您将函数名称包含在 #selector()
中,这让您可以在编译时检查是否存在具有该签名的方法。像这样:
class MyViewController: UIViewController {
override func viewDidLoad() {
self.button.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchDown)
}
func buttonPressed(sender: UIButton) { }
}
但是因为 target
是 nil
编译器提示因为它找不到它:
// ! Use of unresolved identifier "buttonPressed(sender:)"
button.addTarget(nil, action: #selector(buttonPressed(sender:)), for: .touchDown)
我尝试使用字符串初始化选择器,但编译器也报错。
// ! String literal is not a valid Objective-C selector
button.addTarget(nil, action: Selector("buttonPressed(sender:)"), for: .touchDown)
最佳答案
解决方案是在定义类的名称前加上前缀。
button.addTarget(nil, action: #selector(MyViewController.buttonPressed(sender:)), for: .touchDown)
仅供引用,我通过 this nice blog post from Jan 2016 得到了答案,下载源文件,并让 XCode 将其转换为 Swift 3 语法:-)
关于swift - 添加目标 :action:forControlEvents: selector syntax when target is nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45243095/
我已向按钮添加了一些目标。他们工作得很好。但在我的 imageMoved... 方法中,如何确定两个事件中哪一个触发了它?这是我添加目标的位置: [button addTarget:self
代码只不过是按钮的目标操作: [subButton addTarget:self action:@selector(pickerviewSubButtonClicked:)
我知道我写的代码是错误的。但我想要这样的东西。怎么做? UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; int tot
我正在像这样使用 addTarget:action:forControlEvents: [newsButton addTarget:self action:@selector(switchToNews
当按下按钮时,我想推送一个新 View ,并且需要将 NSDictionary 对象传递给新的 DetailViewController ,因此该方法如下所示: -(void)displayCours
@selector(updateStuff:) 这是自动发送参数:(id)sender。是否可以将其覆盖为其他内容?在我的特定用法中,实际的发件人是没有意义的。 我正在使用 UITableView 并
尝试了很多方法,还是没有结果。 因此,我在 UIViewController 的子类中以编程方式创建了以下按钮: rightButton = [UIButton buttonWithType:
我环顾四周,但在互联网上和苹果文档中都找不到这个,所以我猜它不存在。 但是 iOS4 是否有一个等效的 API 可以: [button addTarget:self action:@selector(
我已经将UIButton子类化以绘制图形,但是“addTarget”将无法工作。 - (void)addTarget:(id)target action:(SEL)action forControlE
我知道这个问题已被问过很多次,但我想在单击按钮时将自定义对象作为参数传递。 UIButton addTarget:action:forControlEvents: 不允许我们这样做,但这对我来说很重要
我想要一个父 ViewController 来处理由它的一个子 ViewController 生成的 Target-Actions。 根据 Apple 文档,这应该可以通过将 target 设置为 n
我正在尝试将我的 indexPath.row 移动到不同的 View Controller - pageView 标签。这是我尝试过的: [cell.nav addTarget:self action
我打算在我的页面上添加一个“空白”星形图标,当用户点击它时该图标将变为“实心”星形。 我尝试设置 UIImage 如下所示: UIImage *hotIcon = [UIImage imageName
我的按钮如下 创建标签 1 创建标签 2 创建自定义 View (UIView) 在自定义 View 上添加了 label1 和 label2 创建了 myCustomButton(UIButton)
我正在尝试在 UIPicker 的数据更改(实时更新)后立即更新表格单元格中显示的日期和时间。我实现了以下代码。尽管更改了选择器中的值,但我的“更新”方法没有被调用。谁能建议?谢谢! 郑 - (voi
这是我的问题: 我想在 UIButton 上分配一个事件。我用这个方法: - (void)addTarget:(id)target action:(SEL)action forControlEvent
* 编辑:抱歉,刚刚意识到 - 这段代码工作正常,直到我将表格分为三个部分。当它是带有节标题的单个节时,滚动时一切正常。现在我有三个部分,它在滚动时崩溃。* 我正在向 uitablecell 添加一个
我有一个包含几个 subview 的 View ,每个 subview 上都有一个透明按钮。我将这些 View 放在 for 循环中的主 UIView 上,在此循环中,我还为 subview 按钮属性
利用这个论坛中的精彩帖子,我在 tableView 中创建了一个开关作为 accessoryView。当触摸开关时,我的 Action (switchChanged) 被调用。只有发送方有有效值,事件
如果我们使用 Interface Builder 在 .xib 文件中创建一个 UIButton,什么是更好的方法,是使用 IBAction 方法来定义它的 Action 方法和所需的控制事件更好,还
我是一名优秀的程序员,十分优秀!