I have created an NSView that is to serve as a template for several more NSViews that will be created. The code looks like this:
Code:
- (void)addBookmarksBarItem:(NSString *)title withAddress:(NSString *)address atLocation:(int)location
{
NSData *templateBookmarksBarItem = [NSArchiver archivedDataWithRootObject:bookmarksBarItemTemplate];
id copiedBookmarksBarItemTemplate = [NSUnarchiver unarchiveObjectWithData:templateBookmarksBarItem];
NSView* newBookmarksBarItem = [copiedBookmarksBarItemTemplate initWithFrame:NSMakeRect(location,0,60,16)];
[bookmarksBar addSubview:newBookmarksBarItem];
[bookmarksBarItem setTitle:title];
[bookmarksBarItem setAlternateTitle:address];
[bookmarksBarItem setAction:@selector(loadBookmarkItem:)];
}
- (IBAction)loadBookmarkItem:(id)sender
{
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[sender alternateTitle]]];
}
bookmarksBarItemTemplate is the template NSView, and bookmarksBarItem is a button that is contained inside bookmarksBarItemTemplate. I have an outlet to both of them set up in IB.
When I run my application, it duplicates the NSView the number of times I tell it to, but there are a few problems. Firstly, the action I set to each button doesn't seem to be sticking. Nothing happens when I click any of the duplicated buttons. Secondly, the first duplicated item I create is an EXACT copy of the template (the title, etc didnt get changed, even though I told it to change.) I've spent quite a while trying to fix these two errors to no avail. I'd appreciate any help.