Need Help Making A button Open A window

Discussion in 'Mac Programming' started by goodrunb, Aug 30, 2009.

  1. goodrunb macrumors newbie

    Joined:
    Apr 13, 2009
    #1
    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

    [​IMG]
    [​IMG]
    [​IMG]
    [​IMG]
    [​IMG]
    [​IMG]

    Thanx
     
  2. SRossi macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #2
    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
     
  3. goodrunb thread starter macrumors newbie

    Joined:
    Apr 13, 2009
    #3
    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
     
  4. SRossi macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #4
    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
     
  5. goodrunb thread starter macrumors newbie

    Joined:
    Apr 13, 2009
    #5
    Thanks soo much. Do you have any clue as to why my app would crash when I connected it to delegate?
     
  6. SRossi macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #6
    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.
     
  7. goodrunb thread starter macrumors newbie

    Joined:
    Apr 13, 2009
    #7
    Thanks Very Very Much my Friend. :D
     
  8. goodrunb thread starter macrumors newbie

    Joined:
    Apr 13, 2009
    #8
    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?
     
  9. Cinder6 macrumors 6502

    Cinder6

    Joined:
    Jul 9, 2009
    #9
    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.
     
  10. SRossi macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #10
    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
     
  11. goodrunb thread starter macrumors newbie

    Joined:
    Apr 13, 2009
    #11
    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
     
  12. goodrunb thread starter macrumors newbie

    Joined:
    Apr 13, 2009
    #12
    Perhaps a screenie on where to put the code you gave me and what to connect?
     

Share This Page