使用 navigation?.push(mapView) 显示地图视图,但看不到任何弹出窗口
Displaying map view with navigation?.push(mapView) and not seeing anything pop up
我试图在用户单击 MapScreen 属于 class UIViewController 和 MKMapKitdelegate 的按钮时显示 MapKit 视图。
当我在 UIButton 上执行此发送器时,我得到的只是我的 TabBarController 中其他屏幕的背景颜色。
let mapController = MapScreen()
navigationController?.pushViewController(mapController, animated: true)
我希望看到在 MapScreen() 中定义的地图。我正在使用相同的方法来显示许多其他视图控制器并且效果很好。
如果您的视图控制器在情节提要中定义,则不应使用普通 init
来初始化对象。正如 official documentation 所述:
When using a storyboard to define your view controller and its associated views, you never initialize your view controller class directly. Instead, view controllers are instantiated by the storyboard either automatically when a segue is triggered or programmatically when your app calls the instantiateViewController(withIdentifier:)
method of a storyboard object. When instantiating a view controller from a storyboard, iOS initializes the new view controller by calling its init(coder:)
method instead of this method and sets the nibName
property to a nib file stored inside the storyboard.
因此,为了从故事板创建视图控制器,您应该调用 UIStoryboard
对象的 instantiateViewController(withIdentifier:)
函数:
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: .main)
let viewController = storyboard.instantiateViewController(withIdentifier: "Your view controller identifier")
希望对您有所帮助。
我试图在用户单击 MapScreen 属于 class UIViewController 和 MKMapKitdelegate 的按钮时显示 MapKit 视图。
当我在 UIButton 上执行此发送器时,我得到的只是我的 TabBarController 中其他屏幕的背景颜色。
let mapController = MapScreen()
navigationController?.pushViewController(mapController, animated: true)
我希望看到在 MapScreen() 中定义的地图。我正在使用相同的方法来显示许多其他视图控制器并且效果很好。
如果您的视图控制器在情节提要中定义,则不应使用普通 init
来初始化对象。正如 official documentation 所述:
When using a storyboard to define your view controller and its associated views, you never initialize your view controller class directly. Instead, view controllers are instantiated by the storyboard either automatically when a segue is triggered or programmatically when your app calls the
instantiateViewController(withIdentifier:)
method of a storyboard object. When instantiating a view controller from a storyboard, iOS initializes the new view controller by calling itsinit(coder:)
method instead of this method and sets thenibName
property to a nib file stored inside the storyboard.
因此,为了从故事板创建视图控制器,您应该调用 UIStoryboard
对象的 instantiateViewController(withIdentifier:)
函数:
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: .main)
let viewController = storyboard.instantiateViewController(withIdentifier: "Your view controller identifier")
希望对您有所帮助。