I'm currently trying to show a view I created in a xib on my screen when I touch a button. I want this to show up in a form style in the middle of the screen. However at this point I've just been trying to get it to show before worrying about the placement.
Here is the view I created in a xib
I've assigned and created the "CallNowView" class for it that just looks like this for now.
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) {
}
}
What I've tried doing in the view where I want the xib to show is the following
@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)
}
}
}
When I run the code it crashes on this line self.present(callNowView, animated: true, completion: nil)
with an error saying terminating with uncaught exception of type NSException
I'm hoping anyone can help me out with this
In your xib, you should set Class of File's Owner to CallNowView, because CallNowView is subclass of UIViewController. Then you drag Filer's Owner's view to IB view. It should look like screenshot below: