Search code examples
objective-ccocoacocoa-bindings

NSCollectionView won't work


I have a problem with NSCollectionView. According to this manual (https://www.youtube.com/watch?v=dw-sHMTsMVs), which seems to me is very simple.

I created NSCollectionView, NSArrayController which I bind NSMuttableArray * todoList and set Todo class. I joined ViewController with IBAOutlet NSArrayController. Everything seems OK, but when I call [arrayController addobject: todo], it throws an exception. see. pictures.

ViewController.h:

#import <Cocoa/Cocoa.h>

@interface ViewController : NSViewController{
    IBOutlet NSArrayController *todoArrayController;
}
@property NSMutableArray *todoList;
@property IBOutlet NSImageView *logo;

@end

ViewController.m:

#import "ViewController.h"
#import "Db.h"
#import "TodoManager.h"

@interface NSURL (NSObject)
+(id)URLWithLocalDatabase:(NSString* )database username:(NSString* )username params:(NSDictionary* )params;
+(id)URLWithHost:(NSString* )host ssl:(BOOL)ssl username:(NSString* )username database:(NSString* )database  params:(NSDictionary* )params;
+(id)URLWithHost:(NSString* )host port:(NSUInteger)port ssl:(BOOL)ssl username:(NSString* )username database:(NSString* )database params:(NSDictionary* )params;
@end

@implementation ViewController
@synthesize logo;
@synthesize todoList = _todoList;
const int WINDOW_WIDTH = 300;
TodoManager* todoManager;

- (void)awakeFromNib{
    Todo *todo = [[Todo alloc]init];
    todo.name = @"Shit";
    _todoList = [[NSMutableArray alloc]init];
    [todoArrayController addObject:todo];

}

- (void)viewDidLoad {
    todoManager = [[TodoManager alloc] init];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];
    // Update the view, if already loaded.
}

- (void)notifikace:(id)sender{
    NSUserNotification *notification = [NSUserNotification alloc];
    notification.title = @"Kurva to je dobry";
    notification.informativeText = @"TOTO je kurvesky dobrej text";
    notification.soundName = NSUserNotificationDefaultSoundName;
    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}

- (void)viewDidAppear{
    //Nastaveni rozmeru a pozici okna
    NSRect rozmery = NSMakeRect([[NSScreen mainScreen] frame].size.width - WINDOW_WIDTH, 0, WINDOW_WIDTH, [[NSScreen mainScreen] frame].size.height);
    [self.view.window setFrame:rozmery display:true];


    //Nastaveni pozadi okna
    [self.view.window setBackgroundColor:[NSColor whiteColor]];
    //Vždy top
    [self.view.window setLevel:NSFloatingWindowLevel];

    //Nastaveni loga
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:@"/Users/jasin/Documents/XcodeProjects/TestProjekt/TestProjekt/logo.png"];
    [self.logo setImage: image];

}
@end

Layout & connections: http://www.jumina.cz/shot1.png


Solution

  • Please don't use images for information, like the exception log, which is primarily text.

    Anyway, the exception is "NSCollectionView item prototype must not be nil". So, you have neglected to connect the itemPrototype outlet of your collection view to an instance of NSCollectionViewItem in the NIB. When you drag a collection view into the NIB, IB creates the collection view item prototype and connects the outlet for you, so maybe you deleted the item and/or disconnected the outlet yourself. If so, you shouldn't have.