ios - 试图理解#selector 及其方法所发生的事情需要参数 Swift 3

标签 ios swift

据我所知,#selector(myFunc) 仅“指向”myFunc。假设我有

UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))

func handleLogout(shouldAnimate: Bool) {
    present(someController, animated: shouldAnimate, completion: nil)
}

按下条形按钮项时参数 shouldAnimate 会发生什么变化?

有时我想为 View 设置动画,有时我不想。这取决于是否从按钮调用 handleLogout。有没有更好的方法来实现这一点?

我看过许多其他线程,他们都说要使用实例变量,但我不明白这对我有什么帮助。

最佳答案

在这种情况下,参数将始终是发送者,它是 UIBarButtonItem 的实例。所以你应该像这样更改方法签名..

func handleLogout(sender: UIBarButtonItem) {
    //let shouldAnimate = decide what you want here... 
    present(someController, animated: shouldAnimate, completion: nil)
}

关于ios - 试图理解#selector 及其方法所发生的事情需要参数 Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44192833/

相关文章:

swift - 在 Swift (MacOS) 中画一个圆

ios - 从核心数据本质上调整大小(swift 3)

ios - Xamarin 无法在 iPhone 模拟器上运行 iOS 应用程序 - 不支持 386 架构

swift - 无法从 Xcode 6.1 中的 Storyboard连接自定义协议(protocol)委托(delegate)

ios - 在 firestore 中存储字典值时存在逻辑问题

ios - 节点的不同侧有不同的触点

swift - 使用圆角矩形创建进度指示器

ios - 为什么在进行本地搜索时会收到 MKErrorDomain 错误?

ios - 使用动态颜色创建 UI 的最佳方法

ios - Swift - Parse.com - 为什么这个闭包是错误的?