Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

goodrunb

macrumors newbie
Original poster
Apr 13, 2009
22
0
Ok So im making an app in xcode. I need to make it to whenever when I click a button it opens a window. Also the same with a menu item. I know how to do it, but if a press the button or menu item more than once, the application crashes.

Have a look at the pics

3870292687_7e7b4199e6_b.jpg

3870292113_061cb555e5_b.jpg

3870291597_a413a749ef_b.jpg

3871074446_bc0362f135_o.png

3871074308_02c10b9ee0_b.jpg

3871073894_2f7b90ea26_o.png


Thanx
 
Well what I have done to open a window with a button is to create a button in interface builder and connect it to a IBAction and IBOutlet in the .h file.

Then I used this code to open the other window:

Code:
if (!otherWindow) {
		[NSBundle loadNibNamed:@"OtherWindow" owner:self];  // Other window being the name of the other nib or xib window.
	}
	
	[otherWindow makeKeyAndOrderFront:sender];
	[aWindow orderOut:sender];  // aWindow being the name of original window

You must also create an IBOutlet for both windows, as in the OtherWindow and aWindow names.

Hope this helps.

Stephen
 
Well what I have done to open a window with a button is to create a button in interface builder and connect it to a IBAction and IBOutlet in the .h file.

Then I used this code to open the other window:

Code:
if (!otherWindow) {
		[NSBundle loadNibNamed:@"OtherWindow" owner:self];  // Other window being the name of the other nib or xib window.
	}
	
	[otherWindow makeKeyAndOrderFront:sender];
	[aWindow orderOut:sender];  // aWindow being the name of original window

You must also create an IBOutlet for both windows, as in the OtherWindow and aWindow names.

Hope this helps.

Stephen

I'll try this. Thanks for the response. Also will this work for the menu item as well? And btw the screenies are not the actual app that I'm trying to do this for. I just threw that up in Xcode real quick to show my issue :p
 
For the menu item what I would do is create another function in the .m file, lets say:

Code:
- (void)toggleFullScreen:(NSMenuItem *)menuItem

In the now bring up the main menu in IB and add a new First Responder Action called "toggleFullScreen:" with "id" as the type. And connect your menu item to that action in the First Responder.

And using the code posted earlier it should open the new window.

Just change the function name to whatever you would like but remember to also make sure it is the same in IB.

Stephen
 
For the menu item what I would do is create another function in the .m file, lets say:

Code:
- (void)toggleFullScreen:(NSMenuItem *)menuItem

In the now bring up the main menu in IB and add a new First Responder Action called "toggleFullScreen:" with "id" as the type. And connect your menu item to that action in the First Responder.

And using the code posted earlier it should open the new window.

Just change the function name to whatever you would like but remember to also make sure it is the same in IB.

Stephen

Thanks soo much. Do you have any clue as to why my app would crash when I connected it to delegate?
 
Ah that I believe is any easy one.

Open the Windows nib that does not start at startup.
Open the window attributes.
De-check "Hides on deactivate".

That should do it.

Stephen

EDIT: You should only need to do this if you do not check to see wether it is open already as seen in the if statement I posted earlier. I cannot get my code to crash so I may be wrong, although I am sure it has to do with the Hide on deactivate check box in IB.
 
Ah that I believe is any easy one.

Open the Windows nib that does not start at startup.
Open the window attributes.
De-check "Hides on deactivate".

That should do it.

Stephen

EDIT: You should only need to do this if you do not check to see wether it is open already as seen in the if statement I posted earlier. I cannot get my code to crash so I may be wrong, although I am sure it has to do with the Hide on deactivate check box in IB.
Thanks Very Very Much my Friend. :D
 
I forgot because I haven't been working with Xcode today. How do you create an IBAction/ Outlet? Any informational pictures/videos would be great!


EDIT: I know how to add actions and outlets but where do I put the code. I created the actions and outlets then connected it. Nothing. Any pictures / videos would be great
EDIT #2: I got it to work by connecting to deminaturize and unchecking release when close
EDIT #3: Its weird I got it to work but when i have two different window and i press the button it goes to the second window even though that its not connected but when i close and re press it goes to the window i want it to. Help?
 
You declare the outlets in the @interface declaration for your class. The actions go with other method declarations.
Code:
@interface MyClass : NSObject {
     IBOutlet id myObject;
}
-(IBAction)doStuff:(id)sender;
@end

EDIT: That's a lot of dock items you got there.
 
EDIT #2: I got it to work by connecting to deminaturize and unchecking release when close
EDIT #3: Its weird I got it to work but when i have two different window and i press the button it goes to the second window even though that its not connected but when i close and re press it goes to the window i want it to. Help?

Ah ha thats what I meant earlier I was trying all last night to get it to crash but glad you got it working.

And is all your connections properly defined in IB? Perhaps they are connected to the same IBOutlet?

Stephen
 
You declare the outlets in the @interface declaration for your class. The actions go with other method declarations.
Code:
@interface MyClass : NSObject {
     IBOutlet id myObject;
}
-(IBAction)doStuff:(id)sender;
@end

EDIT: That's a lot of dock items you got there.

Lol I used to have way more docks items but my mac crashed D: but thanks for the help everyone I cant try it right now.. waiting for friday for my snow leopard to come in :D
 
Ah ha thats what I meant earlier I was trying all last night to get it to crash but glad you got it working.

And is all your connections properly defined in IB? Perhaps they are connected to the same IBOutlet?

Stephen

Perhaps a screenie on where to put the code you gave me and what to connect?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.