xCode and multiple windows

Discussion in 'Mac Programming' started by DennisBlah, Dec 5, 2013.

  1. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hello all, I'm new into xcode and I'm working on an application for my company.

    I am building up an array with an key name and key value.
    For exampe ( PHP like, cus its easier to explain)

    $myArray = array (
    'Person1' => 'Operator',
    'Person2' => 'Customer',
    'Person3' => 'Owner'
    );

    Now I found some snippet and after working around I got an window (up from code)
    Now is my question. This 1 window. I would like to duplicate for amount of items in my array.
    So for each 'person' I want to make exact same window.

    I hope someone can help me out with this.
     
  2. DennisBlah, Dec 5, 2013
    Last edited: Dec 6, 2013

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #2
    Nevermind I found an really formiliar topic on the forum

    Sorry I didnt looked good before posting.
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    Ok so I did my very best and I cant get this to work.
    I reverted back to the original code I had before.
    It actually does open multiple windows, but they are all named as same object.
    I must be able to close these programmaticly.

    This is the function I temporary made under an button for testing.
    I would be calling it later from my main code

    Code:
    -(void)newWindow:(int)winX:(int)winY:(int)winWidth:(int)winHeight {
        
        NSRect frame = NSMakeRect(winX, winY, winWidth, winHeight);
        myWindow  = [[NSWindow alloc] initWithContentRect: frame
                                                styleMask:(NSTitledWindowMask|NSClosableWindowMask)
                                                  backing:NSBackingStoreBuffered
                                                    defer:true];
        myWindow.title = @"Testing window!";
        
        NSButton *myButton = [[[NSButton alloc] initWithFrame:NSMakeRect(20, 20, 80, 32)] autorelease];
            [[myWindow contentView] addSubview: myButton];
            [myButton setTitle: @"Click me again!"];
            [myButton setButtonType:NSMomentaryLightButton];
            [myButton setBezelStyle:NSRoundedBezelStyle];
            [myButton setAction:@selector(buttonPressed:)];
        
        
        
        NSTextField *myTextField = [[[NSTextField alloc] initWithFrame:NSMakeRect(20, 50, 80, 20)] autorelease];
            [[myWindow contentView] addSubview: myTextField];
            [myTextField setStringValue: @"Testing"];
            [myTextField setEditable: false];
            [myTextField setSelectable: true];
            [myTextField setDrawsBackground: false];
            
        [myWindow makeKeyAndOrderFront:self];
        
    
    }
    

    I would like to know how I can get this into an array instead of calling them 'myWindow'
    So I can do somethin like
    [[myWindowsArray objectAtIndex:1] release];


    is this possible?
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Yes it's possible. Look into making a single NSMutableArray and storing your windows in it.
     
  5. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #5
    Indeed I tried.
    NSMutableArray *testWindows;

    Code:
    -(void)newWindow:(int)winX:(int)winY:(int)winWidth:(int)winHeight {
        
        NSRect frame = NSMakeRect(winX, winY, winWidth, winHeight);
        [testWindows addObject: [[NSWindow alloc] initWithContentRect: frame
                                                styleMask:(NSTitledWindowMask|NSClosableWindowMask)
                                                  backing:NSBackingStoreBuffered
                                                    defer:true]];
    
        int newWindowID = [testWindows.count] - 1;
    
    
    }
    
    But how do I go further on this?
    Code:
    [testWindows objectAtIndex:newWindowID].title = @"Testing Window!";
    
    I'm really down to a breakdown :p

    I also tried to get the object finished, and after put it in the array, and clear the 'myWindow' object. But had no use
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    I'm lost. What's the point of getting that variable with the number of objects in the array? What are you trying to do that you can't do?
     
  7. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #7
    Well Im looking for an way to get multiple windows. But they must be able to execute their own functions and must be able to be closed programaticly.

    With the first function I make the window and also multiple ones but as Im overwriting the NSWindow myWindow. I can only use [myWindow release] once. In second window itll make an error because myWindow already got released.

    I have no idea how many windows there will be opened by the application when its done. Maybe 1 but can go up to 40+
    Thats why I try to make an array populated with the windows. So that they all get their own unique 'identifier'.

    And need to be able to do something like

    [[myWindowArray objectAtIndex:12] release] to get that window to close.
    However the array keys wont really sum up like that cus I need to use unique userid to make this project succesfull

    So like I said. I need to make multiple windows with all same buttons labels and textfields but they all will contain different values and must be able to get closed programmaticly.

    So I can close them from an other function

    I hope someone can help me out
     
  8. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    How will you be determining when and which windows to close?
     
  9. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #9
    Basicly Im scanning for connected phones.
    When found Im making an array with
    iPhone => unique device id
    iPad => unique device id
    ...
    ...

    Then on the hand of this array I want to open the windows with the info i grab from the device.

    But I need to store the windows in an array like..
    Unique device id => window id?

    So when my timer kicks in again and found devices
    I want to compare the new array of devices with the array of created windows. If already found, then I shouldnt open the window. For all devices that are not found in new array the created windows must close.

    Becausr amount of devices can differ from the department where they using it. I have no clue how much windows it will open and its no use to make
    Window1,2,3,4,5 ... 10?, 20?

    Thats why I really need to find out how to make the windows in array form. And how I can call these nswindow functions like closing from the array

    I hope you understand what Im trying to explain
     
  10. chown33, Dec 7, 2013
    Last edited: Dec 7, 2013

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    An "array" in PHP isn't called an array in many other languages:
    http://www.php.net/manual/en/language.types.array.php
    An array in PHP is actually an ordered map. A map is a type that associates values to keys. ...
    As a result, one reason you're having trouble is because you're trying to use an NSArray for something it's completely incapable of doing.

    In order to understand which collection class to use, you need to understand all the fundamental Cocoa collection classes.
    https://developer.apple.com/library/mac/documentation/cocoa/conceptual/collections/Collections.html

    If you use an NSDictionary (actually, its mutable subclass NSMutableDictionary), you can use the unique device id as the key object, and store the window as the value object. Refer to the class description for details.


    Instead of trying to translate what you know about PHP, I strongly suggest that you learn the fundamentals of Objective-C and Cocoa, using either an online tutorial or a book. You will sometimes recognize similarities to things in PHP, which is to be expected. But the most important thing is you'll learn what Objective-C and the Cocoa classes actually are, instead of trying to translate PHP, which has some very different building-blocks.

    You can test your knowledge of Objective-C and Cocoa classes by looking through Cocoa Core Competencies. I suggest looking through all the articles listed there, not just the one on Collections. If you find a lot of articles on things you don't know about, it suggests you should study the basics using a tutorial or book that teaches things in a structured and sensible order. If there are only a few things you don't understand, then use the specific Core Competency article to link to a focused explanation.

    You will also need to understand the difference between an instance variable and a local variable. That's one of the fundamentals of Objective-C, and again, not something directly translatable from PHP. Though PHP has object-oriented features, your posted code shows no signs of using them, and since it's entirely possible to write PHP without using objects, I have no way to know whether you're familiar with those features or not. So again, you should consider a structured tutorial or book that covers Objective-C.
     
  11. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #11
    That relationship would probably better be served by an NSDictionary, where stored objects are accessed by key, rather than an NSArray, where stored objects are accessed by index.

    If you'll know the list of all of your windows right when the first one is created, use an NSDictionary. If it's possible for the user to plug in and access devices that weren't present in the beginning, an NSMutableDictionary would probably be better.
     
  12. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #12
    Thank you ArtOfWar, tomorrow Im going on with my project. But still I got the question how do I correctly call my release function in the NSDirectory. Lets say

    [[myDict objectAtId:2356355] release];

    Wont that just release the object from the dict? And keep the window open?
     
  13. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #13
    Is there still an way to give an variable to an button inside an programaticly added window?
    I'd like to do something like this while building teh window:
    Code:
        NSButton *closeButton = [[[NSButton alloc] initWithFrame:NSMakeRect(230, 20, 100, 32)] autorelease];
        [[myWindow contentView] addSubview: closeButton];
        [closeButton setTitle: @"Close!"];
        [closeButton setButtonType:NSMomentaryLightButton];
        [closeButton setBezelStyle:NSRoundedBezelStyle];
        [closeButton setAction:@selector(closeWindow:myIDfromMutableDictionary)];
    
    Where I put the id of the current window as an paramater to the closeWindow function.

    However the function MUST be like
    Code:
     -(void)closeWindow:(id)sender {
    }
    
    And I cannot read out the ID from the window which it should be closing.

    Maybe there is another way to 'know' from what window the button got pressed, so I could maybe grab an value from this programmaticly added window?
     
  14. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #14
    How familiar are you with MVC? I haven't made any apps that rely on multiple identical windows that can be opened concurrently, but my understanding is that each window should have a window controller. The buttons within the window should target their window controller, and the window controller knows about it's own window and that it should be closed.
     
  15. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #15
    I cheap tricked around by adding alternate title with the value I needed and take it out by calling the alternate title from the nsbutton by sender's id.

    At the moment Im just coding it procedural with few functions. Its my very first project on xcode, cus I dont own an mac and just started in xcode.

    MVC you mean a framework based on controllers that 'control' models t gather information and passing it into an view? Like in PHP? To be honest its a bit pain in the *** :p I do understand the flow and I can work with it but its not the best I can do.

    Also whole c and c++ are kinda new to me. I used c++ for building dll injection and wrappers for sum games a while ago ;)

    But also pure OOP coding with php is hard for when I dont know when to stop making getters and setters while loads can easily done by public vars from outside the class.. Its just how you think its easier, but like I said when Im on it, Im overreacting sometimes and my idea gets messed up and I delete it while I code 80% back after few days..

    But yet for my procedural code in X. Am I correct when calling functions for the NSWindow directly from the dictionary? Or do I first define nswindow var, link it to the id in dictionary and call it then? Tomorrow Im up with the comparing of new array with old and closing the missing ones and update the windows that ate already open.

    I do get the point what you are hinting me to tho. But I must go quick and dirty now for an working demo
    So I can finish it up later when I got time to get more into making classes and all.
     
  16. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #16
    That's exactly what I mean and I hear you when you think it sounds like a PITA. It took me a few years to warm up to MVC and let me tell you, things get a lot easier when you stop fighting it and start using it.

    Apple's frameworks and the Obj-C language were built around MVC. I think you'll find it's easier to learn MVC and use the frameworks as they were intended than to try bending the frameworks to use other design patterns.
     
  17. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #17
    Do you know how to use Interface Builder? How to link up Actions and Outlets to a window controller class? That's how the tools are designed to be used.

    I have a project where I wanted the user to be able to generate new "timer" windows on-demand. Essentially separate stop watches that operate independently. In order to do that, I added a new XIB to my project named "SubtimerWindow.xib". Inside of that XIB, I created the Window for the timer and a Controller to go along with it. You then use IB to hook up the Actions and Outlets from that window to the controller.

    In the application's main controller when the "new timer" action is requested, the code uses a NSNib object to create a new instance of the timer window and controller.

    Code:
    - (IBAction)makeNewTimer:(id)sender {
    
        NSNib *nnib = [[NSNib alloc] initWithNibNamed:@"SubtimerWindow" bundle:nil];
        NSArray *topLevelObjects;
        BOOL result;
        
        result = [nnib instantiateNibWithOwner:self topLevelObjects:&topLevelObjects];
        if (result)
        {
            [subTimerList addObject:topLevelObjects];
        }
        
        [nnib release];
    }
    
    The topLevelObjects array returned should be pointers to the window and controller that are created in the XIB file. You could use the pointer to the controller to send it a message telling it to close the window and go away.
     
  18. DennisBlah, Dec 9, 2013
    Last edited: Dec 10, 2013

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #18
    @ArtOfWar, ofcourse I agree with you. But for now it must be Quick and Dirty for a have not much time to go into it. And the main reason is that I'm the only one from the IT over here that could do Java and after looking into xCode I dropped the applescript part cause the users could accidentally ***** it all up when they're experimenting. And I'm the only one here that actually can do something in xCode :p

    @mfram, you mean, I don't need to put up a window programmatically to have multiple instances of them ? That should be great, I also think it'll perform loads faster if I could do it that way. I'm looking to the snippet you provided, thanks!


    Is there an way to add the object 'myWindow' into an dictionary?
    When I do like this:
    Code:
     [curWindows setObject:myWindow forKey:iTheKey];
    
    it wont work :(
    Also if I can do this, how could I be calling the NSWindow object to close from the curWindows dictionary?
     

Share This Page