Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call presentViewController in UIView class?

There is a custom tab in in my project that is on every screen of the app, so i made custom UIView class and xib file and added buttons in them. Now i want my buttons for navigations on different screens having different story board ids. But i can't call presentViewController from UIView class. Previously i custom function in extension class to navigate between screens but UIView class also can't call that function.

import UIKit

@IBDesignable class tab: UIView {

var view: UIView!

@IBAction func btn1(sender: UIButton) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("6")
    self.presentViewController(vc, animated: true, completion: nil)
}
override init(frame: CGRect) {
    super.init(frame: frame)
    xibSetup()
}

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

func xibSetup() {
    view = loadViewFromNib() 
    view.frame = bounds 
    view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 
    addSubview(view)
} 
func loadViewFromNib() -> UIView {
    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "tab", bundle: bundle) 
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    return view
} 
}
like image 527
Nasir Khan Avatar asked Jan 07 '16 07:01

Nasir Khan


People also ask

What is presentViewController?

The presentViewController:animated:completion: method always displays the view controller modally. The view controller that calls this method might not ultimately handle the presentation but the presentation is always modal. This method adapts the presentation style for horizontally compact environments.

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.

Does SwiftUI have viewcontrollers?

SwiftUI works seamlessly with the existing UI frameworks on all Apple platforms. For example, you can place UIKit views and view controllers inside SwiftUI views, and vice versa.


1 Answers

Call this function on your button

func infoClick()  {

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

    }

This function will create root view controller

 func getCurrentViewController() -> UIViewController? {

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

}

This above code must work, it is working for me in Swift 4.0

like image 53
ak2g Avatar answered Sep 17 '22 04:09

ak2g