[英]How to add an UIView from a XIB file as a subview to another Xib file
[英]How to load a xib file from another controller without segue and not as a subview
在我的項目中,我有一個:
我想做的是從MapViewController的ViewMapViewController調用xib文件,但不能作為子視圖,順便說一句嗎?
我試圖做這樣的事情:
let viewMapViewController:ViewMapViewController = ViewMapViewController()
self.presentViewController(viewMapViewController, animated: true, completion: nil)
它叫它,但是背景很暗,我不知道為什么。 起初我以為是背景色,但不是,我檢查了一下。
所以有我的文件:
class MapViewController: UIViewController, GIDSignInUIDelegate {
@IBOutlet weak var gmapView: GMSMapView!
var viewMapViewController: ViewMapViewController!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 15)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.setMinZoom(10, maxZoom: 15)
mapView.myLocationEnabled = true
self.view = mapView
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.map = mapView
}
override func viewDidAppear(animated: Bool) {
let viewMapViewController:ViewMapViewController = ViewMapViewController()
self.presentViewController(viewMapViewController, animated: true, completion: nil)
}
這是我的其他文件:
class ViewMapViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
您應該從xib文件中加載控制器,因為它們的出口都已設置在xib文件中,如果您在運行中創建這些控制器的實例,則會看到空的vew,因此正確的快速方法是:
let vc = ViewMapViewController (nibName: "nibName", bundle: nil)
您還可以將視圖控制器放在主情節提要中,並為其分別指定一個情節提要標識符,然后從情節提要中加載它們:
let storyBoard = NSStoryboard(name: "Main", bundle: nil) as NSStoryboard
let vc= storyBoard.instantiateControllerWithIdentifier("YourVCIdentifier") as! NSViewController
您可以將其作為情節提要中的childviewcontroller添加到mapviewcontroller,並將viewmapcontroller類分配給childviewcontroller。 在這種情況下,您甚至不必修改任何代碼即可調用它,它會自動通過mapviewcontroller加載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.