Reputation: 231
My app header is currently a full width, 65pt height UIView which I then use as a generic header for all pages.
class AppHeader: UIView {
...
}
Then, in my Main.storyboard I have a UIViewController with a View from the object library which has its class specified as AppHeader.
My AppHeader (UIView) has multiple buttons which should, if clicked, take you to from the page/controller you're currently on to another.
From the AppHeader class I do not have access to use the present method to show another controller as its not within scope.
Here is my AppHeader.xib:
How can I resolve this?
Upvotes: 0
Views: 650
Reputation: 5523
You can create a protocol - call it AppHeaderDelegate or something - and set up your other viewControllers to adopt that protocol. You could define functions to let your delegate know that a certain button was pressed, and your delegate viewController can react to that by presenting the correct view controller.
Apple's docs on protocols here.
Alternatively, you can use NotificationCenter to broadcast notifications to let subscribers know that a certain button was pressed, and have your viewControllers listening for these notifications and reacting to them accordingly. You have to manage when classes start/stop listening, though, as you may have several objects trying to react to a single notification.
Upvotes: 0
Reputation: 131408
Make your AppHeader
view a custom subclass of UIView
if it isn't already. Wire the actions on the button to IBAction
methods in the view.
Create a protocol AppHeaderDelegateProtocol
. Give your AppHeader
class a weak delegate property. Define methods in that protocol that let the AppHeader
notify it's owning view controller about button presses.
Implement your AppHeaderDelegateProtocol
in view controllers that will contain instances of AppHeader
.
Connect the delegate property to each instance of AppHeader
's owning view controller.
That should do it.
Upvotes: 1
Reputation: 3691
This is very bad behaviour, you should not use a UIView as a header. You should add a Navigation View Controller as the first view controller of your app. Navigation view controller has the navigation bar where you can put the buttons you want there. From that ones, you will be able to push or present other view controllers.
Check the official documentation: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html
Upvotes: 1