iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – Swift UIAlertController – > ActionSheet iPad iOS8崩溃大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
目前我遇到了大麻烦与我的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",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.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

因为我伤心在iphone它的工作,但如果我点击iPad上的按钮应用程序崩溃

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

解决方法

错误消息告诉您,您需要给警报控制器的popoverPresentationController一个位置,以便它可以正确定位本身。这很容易做 – 只需检查是否有一个popover控制器,并添加发件人作为源。

如果你的按钮是一个UIBarButtonItem:

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

除此以外:

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

大佬总结

以上是大佬教程为你收集整理的iphone – Swift UIAlertController – > ActionSheet iPad iOS8崩溃全部内容,希望文章能够帮你解决iphone – Swift UIAlertController – > ActionSheet iPad iOS8崩溃所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。