以表单大小显示弹出视图

Showing a popupview in the form size

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

这是我在 xib 中创建的视图

我已经为其分配并创建了“CallNowView”class,目前看起来就像这样。

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

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

在你的xib中,你应该将File's Owner的Class设置为CallNowView,因为CallNowView是UIViewController的子类。然后将 Filer 的所有者视图拖到 IB 视图。它应该看起来像下面的截图: