0

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:

enter image description here

How can I resolve this?

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I just looked at the picture. He is duplicating the behavior of a navigation bar, isn't he?
1

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.

1 Comment

My answer provides a way to do what you ask, but as Joan says in his answer, the better answer is "Don't do that."
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.