简体   繁体   中英

How to switch properly between UIViews or UIViewController inside NSObject Class when clicked the item in tableView inside of NSObject in Swift?

What i want is, I have NSObject MenuView which has tableView in it, when i click one the of cells it should remove the current UIViewController or UIView on another controller. I have used shared instance of my MainPageController in NSObject didSelectItem method to replace the current view on MainPageController with the new one by using view.willRemoveSubviews(myCurrentView) and addSubView(newView) but it does nothing even not a crash or error. is there anyone could help me with that please? I am stuck on it for a long time...

here is the info of how i did it.

inside of NSObject class's didSelectItem method

    let view = UIView()
    view.backgroundColor = .red
    MainPageController.shared.addCustomSubView(View: view)

and here how i defined it in MainPageController

 func addCustomSubView(View: UIView) {
    view.willRemoveSubview(inOutView)
    view.addSubview(View)
    View.anchor(top: headerLine.bottomAnchor, leading: view.leadingAnchor, bottom: footerLine.topAnchor, trailing: view.trailingAnchor, padding: .init(), size: CGSize(width: 0, height: 0))
}

inOutView also the view of my another controller, which i added as a subView of current MainPageController

So i want to replace the inOutView with my new View coming from the NSObject class inside didSelectItem

in MainPageController :

@objc fileprivate func handleMenuButton() {
    menuLauncher.showSettings()
    menuLauncher.mainPageController = self
}

func showController() {
    let dummyController = UIViewController()
    dummyController.view.backgroundColor = .red
    addChild(dummyController)
    view.addSubview(dummyController.view)
    dummyController.view.anchor(top: headerLine.bottomAnchor, leading: view.leadingAnchor, bottom: footerLine.topAnchor, trailing: view.trailingAnchor, padding: .init(), size: CGSize(width: 0, height: 0))
    dummyController.didMove(toParent: self)

}

and declare var mainPageController: MainPageController in NSObject class and inside of didSelectItemAtIndexPath this code:

 self.mainPageController?.showController()

by doing this I achieved switching between uiviews that is inside of viewController by using NSObject.

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