简体   繁体   中英

How to change a parent view by clicking a button inside of subview in swift (Xcode)

I am working on an app which needs a menu bar, it should be used in many different views, so here is what i did.

I make a subview which is the menu, it is loaded by other view in Main.storyboard.

  1. Main.storyboard ----- this contains parent view

  2. SimpleCustomView.xib ----- this is the subview

Here is the Problem:

  • How can I make a button inside of SimpleCustomView which can change the parent view in Main.storyboard? So the users can click on the menu to switch pages.

I have tried:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyboard.instantiateViewController(withIdentifier: "PC") as UIViewController

// self.presentViewController(vc, animated: true, completion: nil)

//superview.presentViewController(vc, animated: true, completion: nil)

Neither 3rd line nor 4th line works.

So i got quite confused, how can i do it.

The general approach in a situation such as yours, where you want multiple views to show a certain view from a parent view, would be to send a notification from the child view when the button is clicked. The parent view would be listening for that particular message and then carry out an action - show the menu in this case.

Since I don't know enough about your particular set up, I don't want to hazard providing code which might not really work for you :) If you are able to share your project with us, then I can take a look and give you specific code which should work.

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