I have a weird problem with a simple NSScrollView on the Mac. I just started programming in Cocoa, but I had previous experience with UIKit on iOS. I added an NSScrollView to my xib file and subclassed the NSView that was automatically generated by Xcode when I dropped the scroll view in my window. The subclass simply adds content views to the scrollview and draws a background. Here it is:
#import "TweetsView.h"
@implementation TweetsView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)awakeFromNib {
[self reload];
}
- (void)reload {
//Retrieve tweets
ACAccount *account = [[TwitterModel retrieveTwitterAccounts] objectAtIndex:1];
TwitterModel *model = [[TwitterModel alloc] init];
NSArray *timeline = [model retrieveMainTimelineWithAccount:account];
//Display the tweets
int stackHeight = 0;
for (NSDictionary *tweetDictionary in timeline) {
TweetView *tweetView = [[TweetView alloc] initWithFrame:NSMakeRect(0, stackHeight, self.frame.size.width, 60)];
[tweetView setMainText:[tweetDictionary objectForKey:@"text"]];
[self addSubview:tweetView];
stackHeight += (int)tweetView.frame.size.height;
}
self.frame = NSMakeRect(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, stackHeight);
}
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor colorWithCalibratedRed:0.96 green:0.96 blue:0.96 alpha:1.0] setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
- (void)setTweets:(NSArray *)tweets {
_tweets = tweets;
}
- (NSArray *)tweets {
return _tweets;
}
@end
When I run this program the scroll view will display just normal but I can’t scroll it with my trackpad. The scrollbars don’t appear either. When I resize the window the scrollbars appear for a second just like they normally do in a Cocoa application, and if I’m quick I can scroll by dragging the scroll bars then. So I think the Scroll View does exist, but it doesn’t respond to my mouse and gestures.
What am I doing wrong here? I’m running Mountain Lion and Xcode 4.4 and there are no views in front of the scroll view.
Thanks in advance!
Apparently the document view of my scroll view was a subclass of NSScrollView which is obviously wrong. I changed it to a subclass of NSView and now the problem is fixed.