iphone - Swift UIAlertController -> ActionSheet iPad iOS8 崩溃

标签 iphone ipad swift uiactionsheet

目前我的 ActionSheet 遇到了大麻烦。在 iPhone 上运行良好,但在 iPad 上它只会崩溃

我创建一个只有一个按钮的新项目

import UIKit

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"
            }
        }
    }

}

class ViewController: UIViewController, UIActionSheetDelegate {
    @IBAction func test(sender: AnyObject) {

        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)

            self.presentViewController(alertController, animated: true, completion: nil)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

}

正如我在 iphone 上所说,它可以工作,但如果我在 iPad 上单击按钮,应用程序会崩溃

2014-09-25 14:54:52.784 test[9541:1970048] * Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.' * First throw call stack: ( 0 CoreFoundation 0x00613df6 exceptionPreprocess + 182 1 libobjc.A.dylib
0x01fdaa97 objc_exception_throw + 44 2 UIKit
0x0164da37 -[UIPopoverPresentationController presentationTransitionWillBegin] + 3086 3 UIKit
0x00f54f75 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke + 1666 4 UIKit 0x00f53554 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 226 5 UIKit
0x00f8721b __40+[UIViewController _scheduleTransition:]_block_invoke + 18 6 UIKit 0x00e4d62e ___afterCACommitHandler_block_invoke + 15 7 UIKit 0x00e4d5d9 _applyBlockToCFArrayCopiedToStack + 415 8 UIKit
0x00e4d3ee _afterCACommitHandler + 545 9 CoreFoundation
0x00536fbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 30 10 CoreFoundation 0x00536f00 __CFRunLoopDoObservers + 400 11 CoreFoundation 0x0052c93a __CFRunLoopRun + 1226 12 CoreFoundation 0x0052c1ab CFRunLoopRunSpecific + 443 13 CoreFoundation
0x0052bfdb CFRunLoopRunInMode + 123 14 GraphicsServices
0x0438424f GSEventRunModal + 192 15 GraphicsServices
0x0438408c GSEventRun + 104 16 UIKit
0x00e23e16 UIApplicationMain + 1526 17 test
0x00085e9e top_level_code + 78 18 test
0x00085edb main + 43 19 libdyld.dylib
0x0273eac9 start + 1 20 ???
0x00000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

项目可以在 https://www.dropbox.com/s/54jqd8nsc67ll5g/test.zip?dl=0 找到下载并试用。

最佳答案

错误消息告诉您需要为警报 Controller 的 popoverPresentationController 提供一个位置,以便它可以正确定位自己。这很容易做到——只需检查是否有弹出 Controller 并将发送者添加为源即可。

如果您的按钮是 UIBarButtonItem:

if let popoverController = alertController.popoverPresentationController {
    popoverController.barButtonItem = sender
}
self.presentViewController(alertController, animated: true, completion: nil)

否则:

if let popoverController = alertController.popoverPresentationController {
    popoverController.sourceView = sender
    popoverController.sourceRect = sender.bounds
}
self.presentViewController(alertController, animated: true, completion: nil)

关于iphone - Swift UIAlertController -> ActionSheet iPad iOS8 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26039229/

相关文章:

iphone - 将省略号添加到 NSString

iphone - 重新排列 UIScrollView 中的 subview ,就像主屏幕上的应用程序图标一样

iphone - 跨 iOS 平台的视频规范

swift - 无法将图像从 PARSE 加载到 PFTableViewCell 的 imageView 属性中

iOS 警告 : Attempt to dismiss from view controller while a presentation or dismiss is in progress

iphone - 通过 iPhone 访问通话记录

ios - ios 中 UITextField 的自动布局问题

iphone - iPad 平放在表面时的启动方向

ipad - 如何在滚动时隐藏(但不禁用)iPad 上的滚动条?

swift - 如何从 Vapor 3 单元测试中的响应访问原始内容?