Set NSButton title on program launch

Discussion in 'Mac Programming' started by larswik, Sep 2, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I thought this would have been easy. When launching the program I had an init method that is supposed to change the Title of a button with SetTitle

    Code:
    @implementation AppController
    
    - (id)init
    {
        self = [super init];
        if (self) {
            [self setNameOfButton];
        }
        
        return self;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    
    - (IBAction)testButton:(id)sender {
        [testButtonNameField setTitle:@"Blah"];
    }
    -(void)setNameOfButton{
        [testButtonNameField setTitle:@"Blah"];
    
        
    }
    @end
    
    It works when I use the code in the method for the button to trigger the event but not on start up? The goal for the night was to have a 'name.txt' file on my website and try to launch the program and have it retrieve the name from the name.txt file.

    But I have not gotten that far yet?
     
  2. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    I solved the problem. It appears that you should not do it in the init method. Instead create the awakeFromNib Method because the outlets are created after the init method and before the awakeFromNib.

    Now it functions.
     
  3. LostSoul80 macrumors 68020

    LostSoul80

    Joined:
    Jan 25, 2009
    #3
    Correct. You can also manage your UI in -applicationDidFinishLaunching:.
     
  4. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #4
    WWhich brings up an interesting question. If an AppController object is nib-instantiated, can you even count on the -init method being called? A nib (compiled xib) is a keyed archive, so everything within it should receive -initWithCoder: in lieu of -init, I would think.
     

Share This Page