我已经实现了我的按钮:
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()
}
}
}
但是当我点击按钮时,应用程序崩溃了......
我会很高兴得到一些帮助, 非常感谢你的帮助!
答案 0 :(得分:0)
问题是你的ShareButton类不是从NSObject派生的。因此,它的内部结构对Cocoa是不可见的,它不能调用它 - 它无法找到你的btn
方法。你有两个选择:
从NSObject派生ShareButton:class ShareButton : NSObject {
或者,将btn
公开给Objective-C:@objc func btn() {
答案 1 :(得分:0)
曾经有一个非常类似的问题,对我有用的是在选择器中添加一个冒号。尝试更改:
button = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Action,target:self,action:Selector(" btn"))
到
button = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Action,target:self,action:Selector(" btn:"))