Xcode UIInputViewController上的UIAlertView崩溃
我正在使用Xcode 6.0和swift。UIAlertView和UIAlertController示例:以下两个函数在继承自UIViewController的myViewController上运行良好,但在继承自UIInputViewController的KeyboardViewController上崩溃。苹果不允许在自定义键盘上使用alertview吗?或者我的编码有错误吗?欢迎您的任何回复Xcode UIInputViewController上的UIAlertView崩溃,xcode,swift,uialertview,uiinputviewcontroller,Xcode,Swift,Uialertview,Uiinputviewcontroller,我正在使用Xcode 6.0和swift。UIAlertView和UIAlertController示例:以下两个函数在继承自UIViewController的myViewController上运行良好,但在继承自UIInputViewController的KeyboardViewController上崩溃。苹果不允许在自定义键盘上使用alertview吗?或者我的编码有错误吗?欢迎您的任何回复 func viewAlert() { var alertView = UIAlertView
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)
}
在iOS 8中,我们有了新的宏:
NS_EXTENSION_UNAVAILABLE_IOS
iOS不允许在扩展中使用某些类
如果您正在构建自定义键盘扩展,iOS不允许使用AlertView或AlertController
请参阅更多:
希望它能帮助Swift中的人: 假设您有一个必须与iOS和扩展一起工作的库,在可能的情况下,我使用的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和扩展名编译此文件。UIAlertViews在iOS 8中已被弃用,为什么要使用它们呢。Popyeye,感谢您的回复。更重要的是,iOS 8中推荐的UIAlertController与此相同。关键是,它们在UIViewController中都可以正常工作,但在UIInputviewController中却不行。老实说,我对swift的研究还不多,所以我不能完全确定。我只知道UIAlertViews在iOS8中被反对用于objective-c和swift。如果我看到任何可能有用的东西,我会回来把它传给你。
#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