我正在使用Xcode 6.0和swift。 UIAlertView和UIAlertController(例如:以下两个函数)在从UIViewController继承的myViewController上工作正常,但是它们在继承自UIInputViewController的KeyboardViewController上崩溃。 Apple不允许在自定义键盘上使用alertview,或者我的编码是否有任何错误?任何回复都是受欢迎和赞赏的。
func viewAlert() {
var alertView = UIAlertView() <———
alertView.addButtonWithTitle("Ok")
alertView.title = "title"
alertView.message = "message"
alertView.show()
}
func viewAlert0() {
var alert = UIAlertController() <———
alert.title = "title"
alert.message = "are disabled in your device"
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
<——— debugger error point:
0x325ca19: calll 0x327e620 ; symbol stub for: pthread_kill
0x325ca1e: movl $0x2710, (%esp)
0x325ca25: calll 0x327ec50 ; symbol stub for: usleep$NOCANCEL
0x325ca2a: movl $0xffffffe7, -0xc(%ebp)
0x325ca31: movl %esi, 0x4(%esp)
0x325ca35: movl $0x0, 0x8(%esp)
0x325ca3d: movl $0x3, (%esp)
0x325ca44: calll 0x327e476 ; symbol stub for: sigprocmask
0x325ca49: ud2 <====== Thread 1 :EXC_BAD_INSTRUCTION(code=EXC_i386_INVOP,subcode=0x0)
答案 0 :(得分:0)
从iOS 8开始,我们有了新的宏:
NS_EXTENSION_UNAVAILABLE_IOS
iOS不允许在EXTENSION
如果您要构建AlertView
AlertController
或CUSTOM KEYBOARD EXTENSION
查看更多:
答案 1 :(得分:0)
希望它能帮助斯威夫特的某个人:
假设您有一个必须与iOS和扩展一起使用的lib(在我的情况下,我可以使用UIApplication.sharedApplication()。对扩展不可用的keyWindow)
#if NS_EXTENSION_UNAVAILABLE_IOS
func captureScreen() -> UIImage {
let window = UIApplication.sharedApplication().keyWindow
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(window!.bounds.size, false, scale)
window?.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image : UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
#else
#endif
使用#ifdef允许我们为iOS和扩展程序编译此文件。