2012-07-27 70 views
1

我有一個奇怪的問題,在Mac上的一個簡單的NSScrollView。我剛開始在Cocoa編程,但我以前在iOS上使用過UIKit。我在我的xib文件中添加了一個NSScrollView,並將我在窗口中放置滾動視圖時由Xcode自動生成的NSView劃分爲子類。子類只是將內容視圖添加到滾動視圖並繪製背景。那就是:NSScrollView沒有響應手勢

#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 

當我運行這個程序滾動視圖將只顯示正常,但我不能用我的觸摸板滾動它。滾動條也不出現。當我調整窗口的大小時,滾動條會像通常在Cocoa應用程序中那樣出現一秒鐘,如果我很快,我可以通過拖動滾動條來滾動。所以我認爲滾動視圖確實存在,但它不響應我的鼠標和手勢。

我在這裏做錯了什麼?我正在運行Mountain Lion和Xcode 4.4,並且在滾動視圖前沒有任何視圖。 在此先感謝!

+0

您的自定義視圖是否足夠高,需要滾動才能看到整個事物? – rdelmar 2012-07-27 15:46:22

+0

這是,我可以滾動滾動條,當我調整窗口大小後可見,但我不能使用默認手勢滾動。 – icant 2012-07-27 16:36:29

回答

0

顯然,我的滾動視圖的文檔視圖是NSScrollView的一個子類,顯然是錯誤的。我將其更改爲NSView的子類,現在問題已解決。