简体   繁体   English

从UIView导航到UIViewController

[英]Navigate to UIViewController from a UIView

My app header is currently a full width, 65pt height UIView which I then use as a generic header for all pages. 我的应用程序标头当前为全角,65pt高的UIView,然后将其用作所有页面的通用标头。

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 . 然后,在Main.storyboard中,我有一个UIViewController,带有一个来自对象库的View,其类指定为AppHeader

My AppHeader (UIView) has multiple buttons which should, if clicked, take you to from the page/controller you're currently on to another. 我的AppHeader(UIView)有多个按钮,如果单击这些按钮,应该会将您从当前所在的页面/控制器带到另一个页面。

From the AppHeader class I do not have access to use the present method to show another controller as its not within scope. 从AppHeader类中,我无权使用本方法将另一个控制器显示为不在范围内。

Here is my AppHeader.xib: 这是我的AppHeader.xib:

在此处输入图片说明

How can I resolve this? 我该如何解决?

This is very bad behaviour, you should not use a UIView as a header. 这是非常糟糕的行为,您不应将UIView用作标题。 You should add a Navigation View Controller as the first view controller of your app. 您应该将Navigation View Controller添加为应用程序的第一个视图控制器。 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 查看官方文档: https : //developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html

Make your AppHeader view a custom subclass of UIView if it isn't already. 使您的AppHeader视图成为UIView的自定义子类(如果尚未创建)。 Wire the actions on the button to IBAction methods in the view. 将按钮上的操作连接到视图中的IBAction方法。

Create a protocol AppHeaderDelegateProtocol . 创建一个协议AppHeaderDelegateProtocol Give your AppHeader class a weak delegate property. 给您的AppHeader类一个弱的委托属性。 Define methods in that protocol that let the AppHeader notify it's owning view controller about button presses. 定义该协议中的方法,这些方法可使AppHeader通知其拥有的视图控制器有关按钮按下的信息。

Implement your AppHeaderDelegateProtocol in view controllers that will contain instances of AppHeader . 实现你AppHeaderDelegateProtocol在视图控制器将包含的实例AppHeader

Connect the delegate property to each instance of AppHeader 's owning view controller. 将委托属性连接到AppHeader拥有的视图控制器的每个实例。

That should do it. 那应该做。

You can create a protocol - call it AppHeaderDelegate or something - and set up your other viewControllers to adopt that protocol. 您可以创建一个协议-将其称为AppHeaderDelegate或其他名称-并设置其他viewController来采用该协议。 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. 您可以定义函数以使您的委托人知道按下了某个按钮,并且委托人viewController可以通过显示正确的视图控制器来对此做出反应。

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. 或者,您可以使用NotificationCenter广播通知,以使订阅者知道某个按钮被按下,并使您的ViewController监听这些通知并做出相应的反应。 You have to manage when classes start/stop listening, though, as you may have several objects trying to react to a single notification. 但是,您必须管理类何时开始/停止侦听,因为您可能有多个对象试图对单个通知做出反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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