1


I have 2 classes: AppDelegate and ViewAddFriendsWindowObject.
In AppDelegate.m i have these lines of codes:

#import "ViewAddFriendsWindowObject.h"

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    ViewAddFriendsWindowObject *viewAddFriends = [[ViewAddFriendsWindowObject alloc] init];
    [viewAddFriends isFirstRun:YES];
}

In ViewAddFriendsWindowObject.h i have:

#import <Foundation/Foundation.h>

@interface ViewAddFriendsWindowObject : NSObject 

@property IBOutlet NSButton *cancelSkipBtn;
@property IBOutlet NSButton *doneBtn;

- (void)isFirstRun:(BOOL)firstRun;

@end

In ViewAddFriendsWindowObject.m i have:

    #import "ViewAddFriendsWindowObject.h"

    @implementation ViewAddFriendsWindowObject
    @synthesize cancelSkipBtn=_cancelSkipBtn;
    @synthesize doneBtn=_doneBtn;

    - (void)isFirstRun:(BOOL)firstRun{

        NSLog(firstRun ? @"Yes" : @"No");

        if(firstRun == YES){
            NSLog(@"YES");
            [_cancelSkipBtn setTitle:@"Skip"];
            [_cancelSkipBtn setEnabled:NO];
        }else{
            NSLog(@"NO");
            [_cancelSkipBtn setTitle:@"Cancel"];
        }

    }
@end

Here's the problem. The NSLog(@"YES") is executed but [_cancelSkipBtn setTitle:@"Skip"]; and [_cancelSkipBtn setEnabled:NO]; are not. Any ideas?

4

2 回答 2

3

When you alloc and init an object in code, you are bypassing any connections you made in IB. Therefore, your IBOutlet properties are not being set to anything before you call isFirstRun on it

于 2012-07-10T17:33:03.510 回答
0

In your xib, make sure that you have an object with the class set to ViewAddFriendsWindowObject in the identity inspector. Make your IBOutlet connections from the ViewAddFriendsWindowObject, then add an awakeFromNib method to ViewAddFriendsWindowObject.m. Finally, send the appropriate isFirstRun message to self in awakeFromNib based on a setting in the shared user defaults. Something like this:

- (void)awakeFromNib
{
   if (![[NSUserDefaults standardUserDefaults] boolForKey:@"PreviouslyRun"]) {
      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"PreviouslyRun"];
      [[NSUserDefaults standardUserDefaults] synchronize];
      [self isFirstRun:YES];
   } 
   else
      [self isFirstRun:NO];
}
于 2012-07-10T18:12:16.757 回答