1
votes

I am facing a strange behavior when loading a view controllers view with addSubview():

Hard- and Software: Cocoa Swift App on OSX 10.9.5 Mavericks, XCode 6 Beta 6

Goal: App, programmatically loading a ViewController from its own xib and map the VCs view on main window

MainMenu.xib: Window with just one view (NSView) as anchor for the controllers view

SpecialsVC.xib View ( NSView) with just one label „Specials View“, view linked to Files owner

Swift Code of AppDelegate and view controller SpecialsVC:

class SpecialsVC : NSViewController {
    required init(coder aDecoder: NSCoder!) { super.init(coder: aDecoder); }

    override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
}


class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var anchorView: NSView!

    var specialsVC  = SpecialsVC(nibName: "SpecialsVC", bundle: nil );

    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        anchorView.addSubview( specialsVC.view );    // <— 2nd window opens here
        specialsVC.view.frame = anchorView.bounds;
    }

    func applicationWillTerminate(aNotification: NSNotification?) {  }
}

Running the app: 1 ) Main window is opening as expected, view controller is loaded from nib, its view is placed on the anchor view of main window. —> OK 2 ) 2nd and empty window with title „Window“ opens. Can be closed, no effect to main window. —> ?????????

Debugging: The unexpected window comes up when stepping over anchorView.addSubview( specialsVC.view )

Also tried 1: Load view controller not programmatically, but via outlet from View-Controller-Object in MainMenu.xib —> same effect

Also tried 2: Put a button on the main window and loaded the view controller into a local variable in the buttons action. --> no 2nd window comes up, but the controller is lost when leaving the action. Copying the local var to an instance var —> 2nd window comes up

Hint: Size of 2nd window does not change when size of view controllers view is changed.

What am I doing wrong? Where comes the 2nd window from?

Kind regards Ulrich

1
specialsVC.xib probably contains a window, that is instantiated during specialsVC.view call. Check your xib again. - user3125367
Copying the local var to an instance var —> 2nd window comes up I.e. the window shows up even without -[view] call? Then I must be wrong on xib idea. - user3125367

1 Answers

0
votes

I think that you should be putting be specialsVC.view.frame = anchorView.bounds before your addSubview() method. I think that they will be ignored otherwise. It's like adding a declaration in a function after the return statement.