0

I am working through a chapter of COCOA PROGRAMMING FOR MAC OS X (3RD EDITION) on NSArrayController and it tells me to:

Control-Drag to make the array controller become the target of the Add New Employee button. Set the action to add:

However when I drag over the array controller it does not highlight so I get no target options.

How do I do this correctly in the new XCode

full size image

document.h:

//
//  Document.h
//  RaiseMan
//
//  Created by user on 11/12/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface Document : NSDocument
{
    NSMutableArray *employees;
}

@end

document.m:

//
//  Document.m
//  RaiseMan
//
//  Created by user on 11/12/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Document.h"

@implementation Document

- (id)init
{
    self = [super init];
    if (self) {
        employees = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc
{
    [self setEmployees:nil];
    [super dealloc];
}


-(void)setEmployees:(NSMutableArray *)a
{
    //this is an unusual setter method we are goign to ad a lot of smarts in the next chapter
    if (a == employees)
        return;
    [a retain];
    [employees release];
    employees = a;
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"Document";
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    /*
     Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
    You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
    */
    NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return nil;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    /*
    Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
    You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
    If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
    */
    NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return YES;
}

+ (BOOL)autosavesInPlace
{
    return YES;
}


    - (void)setEmployees:(NSMutableArray *)a;

    @end

person.h:

//
//  Person.h
//  RaiseMan
//
//  Created by user on 11/12/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    NSString *personName;
    float expectedRaise;
}

@property (readwrite, copy) NSString *personName;
@property (readwrite) float expectedRaise;

@end

person.m:

//
//  Person.m
//  RaiseMan
//
//  Created by user on 11/12/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Person.h"

@implementation Person

- (id) init
{
    self = [super init];
    expectedRaise = 5.0;
    personName = @"New Person";
    return self;    
}

- (void)dealloc
{
    [personName release];
    [super dealloc];
}

@synthesize personName;
@synthesize expectedRaise;

@end
ian
  • 11,605
  • 25
  • 69
  • 96
  • can you also upload your code at some place... so that we can check? – Devarshi Nov 13 '11 at 01:04
  • your code looks ok... if you have account on drop box or some other file sharing service, can you upload it so that we can reproduce your problem and solve it? – Devarshi Nov 13 '11 at 01:14
  • here is a zip of the project: http://sharesend.com/2w9uo – ian Nov 13 '11 at 01:16

1 Answers1

2

Dude.. you are doing it wrong in IB. This is wrong -

enter image description here

These entries should be made in attribute inspector for array controller -

enter image description here

Once you correct it, you will be able to set target correctly :)

Devarshi
  • 16,440
  • 13
  • 72
  • 125
  • you are still unable to set target ? Please check and verify other bindings also! – Devarshi Nov 13 '11 at 02:20
  • yea the array object just wont highlight when i try to target to it. – ian Nov 13 '11 at 02:48
  • 1
    @ian: The upshot of what Miraaj said in his answer is that you made it not an array controller. You turned it into a `person` object, the sort of thing that you mean to keep in the array controller's array. That's why you couldn't connect buttons to it: A `person` doesn't have any actions. If you've cleared the object's Custom Class and it still doesn't work, please edit your question to include an updated screenshot of both the Identity and Attributes inspectors, or an updated copy of the code. – Peter Hosey Nov 13 '11 at 03:25
  • Peter! That did it. If you want to make that into a answer put it on here. – ian Nov 13 '11 at 04:04