PDA

View Full Version : Open Duplicate Window (xcode)




Ipodize
Apr 21, 2011, 12:43 PM
I've made a simple web browser application in Xcode with this (http://www.youtube.com/watch?v=BfjMmeMR6Go&feature=related) 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?



jiminaus
Apr 21, 2011, 08:06 PM
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:

Create a new xib file in your project called window.xib.
Cut and paste the window from mainmenu.xib into window.xib.
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.
In your app delegate's applicationDidFinishLaunching:, send newWindow: to self.
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.

Ipodize
May 16, 2011, 12:51 PM
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:

Create a new xib file in your project called window.xib.
Cut and paste the window from mainmenu.xib into window.xib.
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.
In your app delegate's applicationDidFinishLaunching:, send newWindow: to self.
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.

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).

Sydde
May 16, 2011, 09:47 PM
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).

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.