views:

121

answers:

1

I have one class that controls one window, and another class that controls a different window in the same xib, however, the second window never displays what it should.

In the first class I alloc and init the second class, then pass some information to it. In the second class it displays that data in the table view.
Yes, in the .xib I have all the connections set up correctly, I've quadruple checked. Also the code is correct, same with the connections, I've quadruple checked.

Edit: and yes, there's data in the arrays, and the classes are NSObjects.

Edit2: I kinda found the problem. For some reason, the array is filled with contents, but it's returning 0 as a count.

Edit 9000:

Here's the code:

Answer.h

#import <Cocoa/Cocoa.h>
@interface MSAnswerView : NSObject {
    IBOutlet NSWindow *window;
    NSArray *User;
    NSArray *Vote;
    NSArray *Text;
    IBOutlet NSTableView *view;
    IBOutlet NSTableColumn *voteCount;
    IBOutlet NSTableColumn *saidUser;
    IBOutlet NSTextView *body;
}
-(void)setUpWithVoteCount:(NSArray *)array User:(NSArray *)user Text:(NSArray *)text;

@property (nonatomic, retain) NSWindow *window;
@property (nonatomic, retain) NSTableView *view;
@property (nonatomic, retain) NSTableColumn *voteCount;
@property (nonatomic, retain) NSTableColumn *saidUser;
@property (nonatomic, retain) NSTextView *body;
@end

.m

#import "MSAnswerView.h"
@implementation MSAnswerView
@synthesize view;
@synthesize voteCount;
@synthesize saidUser;
@synthesize body;
@synthesize window;
-(void)awakeFromNib
{
    [view setTarget:self];
    [view setDoubleAction:@selector(bodydata)];
    [view reloadData];
}
-(void)setUpWithVoteCount:(NSArray *)array User:(NSArray *)user Text:(NSArray *)text
{
    Vote = array;
    User = user;
    Text = text;
    if (window.isVisible = YES) {
        [view reloadData];
        [view setNeedsDisplay];

    }
}
-(int)numberOfRowsInTableView:(NSTableView *)aTable
{
    return [User count];;
}

-(id)tableView:(NSTableView *)aTable objectValueForTableColumn:(NSTableColumn *)aCol row:(int)aRow
{
    if (aCol == voteCount)
    {
        return [Vote objectAtIndex:aRow];
    }
    else if (aCol == saidUser)
    {
        return [User objectAtIndex:aRow];
    }
    else 
    {
        return nil;
    }


}

-(void)bodydata
{
    int index = [view selectedRow];
    [body setString:[Text objectAtIndex:index]];
}

@end
+1  A: 

The problems in your code are numerous.

For one thing, this comparison in -setUpWithVoteCount:User:Text: is incorrect:

window.isVisible = YES

That should be the comparison operator, == not the assignment operator =.

Secondly, you are naming your ivars and methods incorrectly. Instance variables (in fact, variables of any type) should start with a lower-case letter. This is to distinguish them from class names. Check out the Apple coding guidelines.

I'd also suggest that a name like text is a bad name for a variable that stores a collection like an NSArray. Instead, you should name it something like textItems so it's clear that the variable represents a collection and not a single string.

Also, the class itself is poorly named. You have called it MSAnswerView but it's not a view, it's some type of window controller. At the very least call it MSAnswerWindowController. Better still would be to make it a subclass of NSWindowController and make it File's Owner in its own nib. This is the standard pattern for window controllers.

Your method -setUpWithVoteCount:User:Text: should really be an initializer:

- initWithVoteCount:user:text:

That way it's clear what it's for and that it should be called once at object creation time.

The main problem, however, is that you're not retaining the values that you pass in to your setup method. That means that if no other object retains a reference to them, they will go away at some indeterminate point in the future. If you access them at a later time, you will crash or at the very least receive bad data, which is what's occurring.

Of course, you must also add a -dealloc method in this case to ensure you release the objects when you're finished with them.

Putting all those suggestions together, your class should really look something like this:

MSAnswerWindowController.h:

#import <Cocoa/Cocoa.h>

//subclass of NSWindowController
@interface MSAnswerWindowController : NSWindowController <NSTableViewDataSource>
{
    //renamed ivars
    NSArray *users;
    NSArray *voteCounts;
    NSArray *textItems;
    IBOutlet NSTableView *view;
    IBOutlet NSTableColumn *voteCount;
    IBOutlet NSTableColumn *saidUser;
    IBOutlet NSTextView *body;
}

//this is now an init method
- (id)initWithVoteCounts:(NSArray *)someVoteCounts users:(NSArray *)someUsers textItems:(NSArray *)items;

//accessors for the ivars
@property (nonatomic, copy) NSArray* users;
@property (nonatomic, copy) NSArray* voteCounts;
@property (nonatomic, copy) NSArray* textItems;

@property (nonatomic, retain) NSWindow *window;
@property (nonatomic, retain) NSTableView *view;
@property (nonatomic, retain) NSTableColumn *voteCount;
@property (nonatomic, retain) NSTableColumn *saidUser;
@property (nonatomic, retain) NSTextView *body;
@end

MSAnswerWindowController.m:

#import "MSAnswerWindowController.h"

@implementation MSAnswerWindowController

//implement the init method
- (id)initWithVoteCounts:(NSArray*)someVoteCounts users:(NSArray*)someUsers textItems:(NSArray*)items
{
    //this is an NSWindowController, so tell super to load the nib
    self = [super initWithWindowNibName:@"MSAnswerWindow"];
    if(self)
    {
        //copy all the arrays that are passed in
        //this means we hold a strong reference to them
        users      = [someUsers copy];
        voteCounts = [someVoteCounts copy];
        textItems  = [items copy];
    }
    return self;
}

//make sure we deallocate the object when done
- (void)dealloc
{
    self.users      = nil;
    self.voteCounts = nil;
    self.textItems  = nil;
    [super dealloc];
}

//this is called when the window first loads
//we do initial window setup here
- (void)windowDidLoad
{
    [view setTarget:self];
    [view setDataSource:self];
    [view setDoubleAction:@selector(bodydata)];
}

//this is called when the view controller is asked to show its window
//we load the table here
- (IBAction)showWindow:(id)sender
{
    [super showWindow:sender];
    [view reloadData];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView*)aTable
{
    return [users count];
}

- (id)tableView:(NSTableView*)aTable objectValueForTableColumn:(NSTableColumn*)aCol row:(NSInteger)aRow
{
    if (aCol == voteCount)
    {
        return [voteCounts objectAtIndex:aRow];
    }
    else if (aCol == saidUser)
    {
        return [users objectAtIndex:aRow];
    }
    return nil;
}

- (void)bodydata
{
    NSInteger index = [view selectedRow];
    [body setString:[textItems objectAtIndex:index]];
}

@synthesize users;
@synthesize voteCounts;
@synthesize textItems;
@synthesize view;
@synthesize voteCount;
@synthesize saidUser;
@synthesize body;
@end
Rob Keniger
wow, thanks!! The only problem I'm having is the xib isn't loading...
Matt S.
nevermind, I was just being stupid and not telling the previous controller to show the window. Thanks a bunch!!!!!
Matt S.