我有三个文件:
我使用以下代码在TodayViewController中加载SummaryView.xib:
var summaryView: SummaryView!
summaryView = UINib(nibName: "SummaryView", bundle: NSBundle.mainBundle()).instantiateWithOwner(SummaryView.self, options: nil)[0] as! SummaryView
xib包含一个按钮,其出口在控制器SummaryView.swift中定义:
@IBOutlet weak var openAppButton: UIButton!
@IBAction func openAppButtonClicked(sender: AnyObject) {
let url = NSURL(string: "MyApp://")
sender.extensionContext!!.openURL(url!, completionHandler: nil)
}
不幸的是,点击按钮时从不调用此函数。之后,我在Today Widget的控制器中定义了一个额外的目标:
summaryView.openAppButton.userInteractionEnabled = true
summaryView.openAppButton.addTarget(self, action:#selector(TodayViewController.openApp(_:)), forControlEvents: UIControlEvents.TouchUpInside)
与方法一起
func openApp(sender: AnyObject) {
let url = NSURL(string: "MyApp://")
sender.extensionContext!!.openURL(url!, completionHandler: nil)
}
但也没有调用此方法。
点击确实会触发一些东西,因为我可以在控制台中看到以下条目:
2016-09-21 14:51:45.667049 My App Widget[7546:1001683] subsystem: com.apple.UIKit, category: GestureEnvironment, enable_level: 0, persist_level: 0, default_ttl: 1, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-21 14:51:45.667669 My App Widget[7546:1001683] subsystem: com.apple.UIKit, category: GestureExclusion, enable_level: 0, persist_level: 0, default_ttl: 1, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
我错过了什么?
干杯