Open Duplicate Window (xcode)

Discussion in 'Mac Programming' started by Ipodize, Apr 21, 2011.

  1. Ipodize macrumors member


    I've made a simple web browser application in Xcode with this tutorial. It's all working perfectly, but I'd like to add the ability to have multiple browser windows open at the same time, using a "New Window" menu item. How would I do that?
  2. jiminaus, Apr 21, 2011
    Last edited: Apr 21, 2011

    jiminaus macrumors 65816


    Dec 16, 2010
    If this video tutorial (in which you wrote zero code) is you're only experience with XCode and Cocoa programming, then this is beyond you, because you can't just drag a connection between the New menu item and something.

    If you do know Cocoa programming then:
    1. Create a new xib file in your project called window.xib.
    2. Cut and paste the window from mainmenu.xib into window.xib.
    3. Create an newWindow: action in your app delegate that will load window.xib using NSBundle's loadNibFile:externalNameTable:wihtZone: and then send makeKeyAndOrderFront: to the NSWinow.
    4. In your app delegate's applicationDidFinishLaunching:, send newWindow: to self.
    5. Reconnect your menu items in MainMenu.xib to the First Responder object.

    If you've never loaded a nib file programmatically, see the Nib Files chapter of the Resource Programming Guide.

    Like your current version, this won't be perfect. For example, the web view must have focus for File|Print to print the web page. If focus is in the text field, for example, text field will print instead.

    And you might be leaking memory with unreleased window objects. I don't know about this. I've never used a window without a window controller.
  3. Ipodize thread starter macrumors member


    I do know a bit of cocoa programming, but for some reason xcode didn't create any application delegate files in my project. I had a look at my Cocoa Application project template folder (/Developer/Library/Xcode/Project Templates/Application/Cocoa Application/) and there aren't any application delegate files there either! Any idea why? I'm running Xcode 3.14 (on 10.5.8).
  4. Sydde macrumors 68020


    Aug 17, 2009
    XCode from that era did not normally create app delegates as part of a project. You have to create one yourself, which is really not difficult at all:

    • create a new class (.h + .m files) in your project
    • open mainmenu.xib
    • drag the generic object (blue box) instance into the xib list
    • set the class of the object to your new class (using the Identity Inspector)
    • connect the Application object to your class instance as delegate

    Now you can write methods in your class that respond to NSApplication delegate messages. It will work because the nib (xib) loader will instantiate an object of your class and set it to be the app delegate. Later versions of XCode automatcally added an app delegate to new projects on the premise that it would make coders' work a little quicker as most of them were probably going to do that anyway.

Share This Page