Home > Back-end >  Show WebView ViewController through the xib View
Show WebView ViewController through the xib View

Time:05-11

I have a xib view and a button in it but when i try to configure it to present a ViewController with a WebView it shows some errors.

the xib file:

class popUpView: UIView, UITextFieldDelegate {

@IBOutlet weak var payNowPopUp: UIButton!
@IBOutlet weak var priceTextFieldPopUp: UITextField!

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

override init(frame: CGRect) {
    super.init(frame: frame)
    xibSetup(frame:CGRect(x: 0, y: 0, width: frame.width, height: frame.height))
}

func xibSetup(frame: CGRect){
    let view = loadXib()
    view.frame = frame
    addSubview(view)
    popUpViewInterface.layer.cornerRadius = 10
}

func loadXib() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "popUpView", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil).first as? UIView
    return view!
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    priceTextFieldPopUp.resignFirstResponder()
}


func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    
    priceTextFieldPopUp.resignFirstResponder()
    
    return true
}


@IBAction func payNowToWebView(_ sender: Any) {
    
    guard let mainTabBarController = self.storyboard?.instantiateViewController(withIdentifier: "paymentWebView")
    else {
        return
    }
    
    mainTabBarController.modalPresentationStyle = .custom
    self.present(mainTabBarController, animated: true, completion: nil)
    }
}

Inside the @IBAction func payNowToWebView(_ sender: Any) { } I want to present a viewController when the button is clicked but it's showing some errors like: Value of type 'popUpView' has no member 'storyboard' and Value of type 'popUpView' has no member 'present'

CodePudding user response:

Do it like this (Remember to replace 'Main' with your storyboard name):

@IBAction func payNowToWebView()  {

        let storyboard: UIStoryboard = UIStoryboard (name: "Main", bundle: nil)
        let vc: paymentWebView = storyboard.instantiateViewControllerWithIdentifier("paymentWebView") as! PaymentWebView
        let currentController = self.getRootVC()
        currentController?.presentViewController(vc, animated: false, completion: nil)

    }

func getRootVC() -> UIViewController? {

    if let rootController = UIApplication.shared.keyWindow?.rootViewController {
        var currentController: UIViewController! = rootController
        while( currentController.presentedViewController != nil ) {
            currentController = currentController.presentedViewController
        }
        return currentController
    }
    return nil

}

CodePudding user response:

present is a method and storyboard is a property both of them inside a UIViewController instance not a UIView instance

You can replace self.storyboard with UIStoryboard(name:"YourName",nil)but for present you need a vc to show the needed one from it most commonly to have a delegate weak variable inside that view that references the vc where the view is inside or you may get the app's window root vc then vc.present(.....

  • Related