0

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

Majkeee
  • 960
  • 1
  • 8
  • 28
JaSHin
  • 211
  • 2
  • 16
  • 43

1 Answers1

0

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • How can I connect itemPrototype outlet to NSCollectionViewItem? When I try to drag & drop, so it is not because NSCollectionViewItem has its own view see. shot1.png. – JaSHin Mar 06 '15 at 14:07
  • Oh, right, storyboards. Apparently, there are bugs in Xcode/IB regarding collection views and storyboards. For example, see https://stackoverflow.com/questions/26542955/cocoa-where-is-the-link-between-a-nscollectionview-and-a-nscollectionviewitem – Ken Thomases Mar 07 '15 at 03:21
  • I tried to get an instance CollectionViewItem over NSCollectionViewItem *collectionViewItem = [self.storyboard instantiateControllerWithIdentifier: @ "collection_item"]; and collectionViewItem is always nil. I tried to set "collection_item" as StoryBoard ID or ID for Restoration NSCollectionViewItem or Identity for NSView in NSCollectionViewItem and always collectionViewItem nil :( What is wrong ? – JaSHin Mar 07 '15 at 15:01
  • I solved the problem. I was just in the wrong breakpoint position :-/ – JaSHin Mar 08 '15 at 09:07