Swift UIActionSheet在iPad上崩溃了吗? [英] Swift UIActionSheet crashes on iPad?
问题描述
我目前遇到完全挫折,因为我找不到任何错误,但我的ActionSheet在iPad上崩溃但在iPhone上运行良好这里是行动代码
I m currently running into complete frustrations as i can't find any error, but my ActionSheet crashes on iPad but works well on iPhone here is the code of the action
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一款正常,在iPhone和iPad上,showShop在iPhone上运行良好,但在iPad上运行不正确
The Youtube one works fine, on iPhone and iPad, the "showShop" does work fine on iPhone but not on iPad
showShop向前推进ViewControllerShopView看起来像
The "showShop" Segue forward to my ViewControllerShopView that looks like
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()}
}
但是Segu从来没有在iPad上完成,它只是在Segue上崩溃。
but the Segue never been done on iPad, it simply crashes on the Segue.
任何人都知道可能出错了什么?
Anyone a idea what could be wrong?
推荐答案
如果项目支持 iOS7和iOS8,则需要在运行时检查系统版本;您可以将此代码段插入到任何方法中:
you need to check the system version in runtime if your project supports both iOS7 and iOS8; you can insert this snippet into any of your methods:
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)
}
您的班级需要符合 UIActionSheetDelegate
对于 UIActionSheet
类:
and your class needs to conform the UIActionSheetDelegate
for the UIActionSheet
class:
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"
}
}
}
}
这篇关于Swift UIActionSheet在iPad上崩溃了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!