在我的项目中,我有一个:
我试图做的是从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.
}
答案 0 :(得分:0)
你应该从xib文件中加载控制器,因为它们的出口都是在xib文件中设置的,如果你动态创建这些控制器的实例,你会看到一个空的vew,所以在swift中正确的方法是:
let vc = ViewMapViewController (nibName: "nibName", bundle: nil)
您还可以将视图控制器放在主故事板中,并为每个视图控制器提供一个故事板标识符,并从故事板中加载它们:
let storyBoard = NSStoryboard(name: "Main", bundle: nil) as NSStoryboard
let vc= storyBoard.instantiateControllerWithIdentifier("YourVCIdentifier") as! NSViewController
答案 1 :(得分:0)
您可以将它作为storyviewcontroller中的childviewcontroller添加到mapviewcontroller,并将viewmapcontroller类分配给childviewcontroller。在这种情况下,你甚至不需要任何代码来调用它,它将自动加载mapviewcontroller。