【问题标题】:UIBarButtonItem crashes my application in SwiftUIBarButtonItem 在 Swift 中使我的应用程序崩溃
【发布时间】:2015-07-10 05:31:17
【问题描述】:

我已经实现了我的按钮:

import UIKit

class ShareButton {

    var status_title: String!
    var status_content: String!

    var button: UIBarButtonItem!

    init(status_title: String ,status_content: String) {
        button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: Selector("btn"))        
        self.status_title = status_title
        self.status_content = status_content
    }

    func btn()
    {
        let application = UIApplication.sharedApplication()
        var can_open = application.canOpenURL(NSURL(string: "whatsapp://")!)
        if (can_open) {
            let url_string = "whatsapp://send?text=" + status_content
            let url: NSURL = NSURL(string: url_string)!
            application.openURL(url)
        }
        else {
            let alert = UIAlertView()
            alert.title = "You don't have WhatsApp"
            alert.message = "You need WhatsApp to share this status"
            alert.addButtonWithTitle("Got it")
            alert.show()
        }
    }
}

但是当我点击按钮时,应用程序崩溃了......

我很乐意为您提供帮助, 非常感谢您的帮助!

http://i.stack.imgur.com/tIVqn.png

【问题讨论】:

  • 永远不要说“应用程序崩溃”。说出崩溃发生在哪一行以及控制台消息是什么。
  • 谢谢!我会改的。

标签: ios swift uibutton uibarbuttonitem


【解决方案1】:

曾经有一个非常相似的问题,对我有用的是在选择器中添加一个冒号。尝试改变:

button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: Selector("btn"))

button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: Selector("btn:"))

【讨论】:

  • 我已经试过了,谢谢你的帮助,但它不起作用:(
【解决方案2】:

问题是您的 ShareButton 类不是从 NSObject 派生的。因此,它的内部对 Cocoa 是不可见的,它无法调用它 - 它找不到您的 btn 方法。你有两个选择:

  • 从 NSObject 派生 ShareButton:class ShareButton : NSObject {

  • 或者,将 btn 暴露给 Objective-C:@objc func btn() {

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多