以表单大小显示弹出视图

时间:2016-10-19 19:53:35

标签: ios swift swift3 uipopovercontroller

我正在尝试在触摸按钮时显示我在屏幕上的xib中创建的视图。我希望它以屏幕中间的表单样式显示。然而,在这一点上,我一直试图让它显示出来,然后再担心这个位置。

这是我在xib中创建的视图 enter image description here

我为它分配并创建了“CallNowView”类,现在看起来就是这样。

import Foundation
import UIKit

class CallNowView: UIViewController{

    @IBOutlet var btnCancel: UIButton!
    @IBOutlet var btnCall: UIButton!

    @IBAction func btnCall_pressed(_ sender: AnyObject) {
    }

    @IBAction func btnCancel_pressed(_ sender: AnyObject) {
    }

}

我想在xib显示的视图中尝试做的是以下

@IBAction func btnRSRCall_pressed(_ sender: AnyObject) {
        if (callNowView == nil)
        {
            callNowView = CallNowView(nibName: "CallNow", bundle: nil)
        }

        if(UIViewController.responds(to: #selector(getter: popoverPresentationController))){
            callNowView.modalPresentationStyle = .formSheet
            callNowView.popoverPresentationController?.sourceView = btnCall;
            callNowView.popoverPresentationController?.sourceRect = btnCall.bounds;
            callNowView.popoverPresentationController?.permittedArrowDirections = .any
        }

        else {
            //no support ios7 device, either ipad or iphone
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone)
            {
                //present standard controller for ios7, iphone,
                self.present(callNowView, animated: true, completion: nil)
            }
            else
            {
                //present s popupcontroller for ios7, ipad
                let popover = UIPopoverController.init(contentViewController: callNowView)
                popover.present(from: btnCall.bounds, in: btnCall, permittedArrowDirections: .any, animated: true)
            }
        }
    }

当我运行代码时,它会在此行self.present(callNowView, animated: true, completion: nil)上崩溃并显示错误terminating with uncaught exception of type NSException

我希望有人可以帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

在你的xib中,你应该将Class of File的Owner设置为CallNowView,因为CallNowView是UIViewController的子类。然后将Filer的所有者视图拖到IB视图。它应该如下面的截图: enter image description here