Swift UIActionSheet在iPad上崩溃?
我现在遇到了完全的挫折,因为我找不到任何错误,但我的操作表在iPad上崩溃了,但在iPhone上运行良好。下面是操作代码Swift UIActionSheet在iPad上崩溃?,ipad,swift,uiactionsheet,Ipad,Swift,Uiactionsheet,我现在遇到了完全的挫折,因为我找不到任何错误,但我的操作表在iPad上崩溃了,但在iPhone上运行良好。下面是操作代码 if (view.annotation.title as String!) == "San Francisco" { currentLat = 37.615223 currentLong = -122.389977 url = "www.google.de"
if (view.annotation.title as String!) == "San Francisco" {
currentLat = 37.615223
currentLong = -122.389977
url = "www.google.de"
let action:UIActionSheet = UIActionSheet(title: "Change Map Type", delegate: self, cancelButtonTitle: "Back", destructiveButtonTitle: nil, otherButtonTitles: "Product Page", "Video")
action.showInView(self.view)
action.tag = 0
VideoID = "XXXXXX"
}
因此,应该处理的行动是
if actionSheet.tag == 0{
if buttonIndex == 1{ performSegueWithIdentifier("showShop", sender: self) }
if buttonIndex == 2{ UIApplication.sharedApplication().openURL(NSURL(string: "http://www.youtube.com/watch?v=\(youtubeVideoID)")) }
//if buttonIndex == 2{ performSegueWithIdentifier("showYoutube", sender: self) }
}
Youtube one在iPhone和iPad上运行良好,“showShop”在iPhone上运行良好,但在iPad上运行不好
“showShop”继续前进到我的ViewControllersView,看起来像
import UIKit
class ViewControllerShopView: UIViewController {
/* ################################################## IBOutlets ################################################## */
@IBOutlet weak var activity3: UIActivityIndicatorView!
@IBOutlet weak var webView: UIWebView!
/* ################################################## viewDidLoad ################################################## */
override func viewDidLoad() {
super.viewDidLoad()
loadurl()
}
/* ################################################## didReceiveMemoryWarning ################################################## */
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
println("memory warning")
}
/* ################################################## viewWillAppear ################################################## */
override func viewWillAppear(animated: Bool) {
loadurl()
}
/* ################################################## loadurl func ################################################## */
func loadurl(){
var loadingurl = "google.com"
var homeurl = "google1.com"
loadingurl = url
let webviewURL = NSURL(string: loadingurl)
let request = NSURLRequest(URL: webviewURL)
webView.loadRequest(request)
}
/* ################################################## HomeButton ################################################## */
@IBAction func Reload(sender: AnyObject) {
var loadingurl = "google.com"
var homeurl = "google1.com"
loadingurl = url
let webviewURL = NSURL(string: loadingurl)
let request = NSURLRequest(URL: webviewURL)
webView.loadRequest(request)
}
/* ################################################## Activity Indicator ################################################## */
func webViewDidStartLoad(_ : UIWebView){activity3.startAnimating()}
func webViewDidFinishLoad(_ : UIWebView){activity3.stopAnimating()}
}
但是Segue从来没有在iPad上完成过,它只是在Segue上崩溃了。
有人知道会出什么问题吗 也试过这个
@IBAction func BTfindme(sender: AnyObject) {
let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Test", preferredStyle: .ActionSheet)
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
}
actionSheetController.addAction(cancelAction)
let ActionOne: UIAlertAction = UIAlertAction(title: "Action one", style: .Default) { action -> Void in
}
actionSheetController.addAction(ActionOne)
let ActionTwo: UIAlertAction = UIAlertAction(title: "Action two", style: .Default) { action -> Void in
}
ActionTwo.addAction(choosePictureAction)
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
这也会导致ipad崩溃,而不是iphone崩溃如果您的项目同时支持iOS7和iOS8,则需要在运行时检查系统版本;您可以将此代码段插入到任何方法中:
let systemVersion: NSInteger = (UIDevice.currentDevice().systemVersion as NSString).integerValue
if systemVersion < 8 {
// iOS7:
let action:UIActionSheet = UIActionSheet(title: "Change Map Type", delegate: self, cancelButtonTitle: "Back", destructiveButtonTitle: nil, otherButtonTitles: "Product Page", "Video")
action.tag = 0
action.showInView(self.view)
} else {
// iOS8:
let alertController: UIAlertController = UIAlertController(title: "Change Map Type", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let cancelAction: UIAlertAction = UIAlertAction(title: "Back", style: UIAlertActionStyle.Cancel, handler: nil)
let button1action: UIAlertAction = UIAlertAction(title: "Product Page", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
// doing something for "product page"
})
let button2action: UIAlertAction = UIAlertAction(title: "Video", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
// doing something for "video"
})
alertController.addAction(cancelAction)
alertController.addAction(button1action)
alertController.addAction(button2action)
// for iPAD support:
alertController.popoverPresentationController?.sourceView = self.view
alertController.popoverPresentationController?.sourceRect = CGRectMake(self.view.bounds.width / 2.0, self.view.bounds.height / 2.0, 1.0, 1.0) // this is the center of the screen currently but it can be any point in the view
self.presentViewController(alertController, animated: true, completion: nil)
}
在iOS8+上,您需要使用
UIActionController
,而UIActionSheet
和UIAlertView
是不推荐使用的,更多信息如下:,但是您没有提到哪些iOS版本受到影响…@holex我的操作表必须看起来像actioncontroller吗?它们看起来一样,因为UIAlertController
是一个类中UIActionSheet
和UIAlertView
的“合并”(或“组合”(如果您愿意)版本,因此最终用户不会发现差异,而是在引擎盖下(在您的代码中)如果您的项目同时支持iOS7和iO8,则需要处理此类弃用。@holex我的意思是代码中的外观:)对不起。目前它只支持ios8no,UIAlertController
的工作原理与旧的UIActionSheet
或UIAlertView
完全不同。(抱歉!我错误输入了上面类的名称!)由于未捕获的异常“NSGenericeException”,导致***终止应用程序时崩溃,原因是:'您的应用程序提供了UIAlertControllerStyleActionSheet样式的UIAlertController()。具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover。您必须通过警报控制器的popoverPresentationController提供此popover的位置信息。必须提供sourceView和sourceRect或barButtonItem。如果在显示警报控制器时不知道此信息,您可以在UIPopoverPresentationControllerDelegate方法-PrepareForPoverPresentation中提供它。“我刚刚在一个新项目上用Xcode6 6A313模拟器对它进行了测试,它适用于iphone,但在iPad上崩溃。这里有一个干净的示例项目适用于iphone,但在iPad上崩溃
extension ViewController : UIActionSheetDelegate {
func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
if actionSheet.tag == 0 {
if buttonIndex == 1 {
// doing something for "product page"
} else if (buttonIndex == 2) {
// doing something for "video"
}
}
}
}