我使用UIViewController
来自 XIB 的几个子视图(ViewController 的视图),它们是在使用UISegmentedControl
. 其中一个视图包含一个UIScrollView
. 问题是第一次添加此子视图时UIScrollView
不可滚动。如果我选择另一个片段/视图,然后选择另一个片段/视图,那么它现在是可滚动的。UIScrollView
UIScrollView
仅在 IB 中添加。这是 MainViewController.m 中视图和 UISegmentedControl 的一些代码
- (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。如果我选择此段,除非我再次选择段 0 ( ) 并重新选择段 1,UIScrollView
否则不会对滚动做出反应。nameSubView
是什么导致了这种情况以及如何解决?