简体   繁体   中英

How can i present programmatically made ViewController on button click

I have this custom UIViewController. It is not connected to anything storyBoard and want to present on clicking a button

class SeeListsViewController: UIViewController {
   
    var tableView = UITableView()
    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.setUpTableView()
        view.addSubview(tableView)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        // Do any additional setup after loading the view.
    }
}

How can i do that. I know the storyBoard way but this one has no identifier and it is all programmatically made

The solution below if you have SeeListsViewController in the storyboard and make sure the storyboard identifier is also SeeListsViewController:

let storyboard = UIStoryboard(name: "<Your storyboard name>", bundle: nil)
let listVC = storyboard.instantiateViewController(withIdentifier: SeeListsViewController.self)
self.present(listVC, animated: true)

A programmatic way to present if you don't have a view controller in the storyboard and all the UI components should be designed programmatically in your viewcontroller

let navVC = UINavigationController(rootViewController: SeeListsViewController())
    navVC.modalPresentationStyle = .fullScreen
    self.present(navVC, animated: true, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM