2012-10-31 103 views
-1

我使用UIViewController與XIB的幾個子視圖(ViewController的視圖),它們是在使用UISegmentedControl之間選擇的。其中一種觀點包含UIScrollView。問題是UIScrollView不可滾動第一次這個子視圖被添加。如果我選擇另一個細分/視圖,然後再選擇UIScrollView,那麼現在它是可滾動的。帶有子視圖的ViewController,UIScrollView不可滾動的第一次

UIScrollView只有IB增加。這是一些對意見和UISegmentedControl代碼的MainViewController.m

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

nameSubViewController = [[NameSubViewController alloc] initWithNibName:@"NameSubViewController" bundle:nil]; 
nameSubView = (NameSubView *)[nameSubViewController view]; 

priceSubViewController = [[PriceSubViewController alloc] initWithNibName:@"PriceSubViewController" bundle:nil]; 
priceSubView = (PriceSubView *)[priceSubViewController view]; 

[self.view addSubview:nameSubView]; 
currentView = nameSubView; 
} 

- (void) segmentAction:(id)sender 
{ 
segmentedControl = sender; 
if([segmentedControl selectedSegmentIndex] == 0) { 
    [currentView removeFromSuperview]; 
    [self.view addSubview:nameSubView]; 
    currentView = nil; 
    currentView = nameSubView; 
} 
if([segmentedControl selectedSegmentIndex] == 1) { 
[currentView removeFromSuperview]; 
[self.view addSubview:priceSubView]; 
    currentView = nil; 
    currentView = priceSubView; 
} 

在這段代碼中,假設1段(priceSubView)包含的UIScrollView。如果我選擇此段,那麼UIScrollView不會對滾動作出反應,除非我再次選擇段0(nameSubView)並重新選擇段1.

什麼是造成這種情況以及如何解決這個問題的?

+1

嘗試[myScrollView setScrollEnabled:YES];在viewDidAppear – Kassem

+0

'PriceSubViewController'如何看起來像?我的想象力情景是'scrollView.contentSize'在設計正確的contentSize之前以某種方式設置。 – barley

+0

我想你只是在段被改變後才添加滾動視圖。在第一次加載時,你不添加scrollView,我是否正確? –

回答

0

如果您需要在第一次加載時滾動。 您需要在firstLoad中添加scrollView。 您正在更改段控件後添加滾動視圖,這就是爲什麼它不滾動。一探究竟。

變化viewDidLoad像:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    nameSubViewController = [[NameSubViewController alloc] initWithNibName:@"NameSubViewController" bundle:nil]; 
    nameSubView = (NameSubView *)[nameSubViewController view]; 

    priceSubViewController = [[PriceSubViewController alloc] initWithNibName:@"PriceSubViewController" bundle:nil]; 
    priceSubView = (PriceSubView *)[priceSubViewController view]; 

    [self.view addSubview:priceSubView]; 
    currentView = priceSubView; 
} 
相關問題