所以我通过按分段工具将UIVcrollView中的UIView添加到另一个视图中:
UIView.h
@interface gettingELORolesViewController : UIViewController{
UIViewController *currentController;
NSMutableArray *viewControllers;
}
- (IBAction)SegmentToggle:(UISegmentedControl *)sender;
@property (strong, nonatomic) IBOutlet UIView *containerView;
UIView.m
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Storyboard_iPhone"
bundle:nil];
firstView *FirstView = [sb instantiateViewControllerWithIdentifier:@"firstOne"];
secondView *SecondView = [sb instantiateViewControllerWithIdentifier:@"secondOne"];
viewControllers = [NSMutableArray arrayWithObjects:FirstView,SecondView,nil];
currentController = FirstView;
[containerView addSubview:FirstView.view];//containerView = self.view
然后取决于选择的段,它显示不同的视图:
- (IBAction)SegmentToggle:(UISegmentedControl *)sender {
UIViewController *selectedView=nil;
if (sender.selectedSegmentIndex==0) {
selectedView= [viewControllers objectAtIndex:0]; // retrieve object at index 0 from viewControllers array
}
else if(sender.selectedSegmentIndex==1){
selectedView= [viewControllers objectAtIndex:1]; // retrieve object at index 1 from viewControllers array
}
[currentController.view removeFromSuperview]; // remove Current displaying view from superView
currentController=selectedView; // make selected View to be the current View
[containerView addSubview:selectedView.view]; // Add newly selected view on container View
}
secondView是tableViewController,当我选择第二段时,一切都很好。当我选择第一个它出现但滚动不起作用,我不知道为什么。
firstView.m viewDidLoad方法如下所示:
-(void)viewDidLoad{
[super viewDidLoad];
mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[mainScrollView setContentSize:CGSizeMake(320, 600)];
[mainScrollView setScrollEnabled:YES];
}
在storyBoard中启用了分页。它与firstView的.h文件连接起来。 请告诉我我做错了什么。
答案 0 :(得分:0)
致电-SegmentToggle:
时,
selectedView
selectedView
selectedView
超出范围简而言之,你什么都没做。您至少应该使用所选控制器的视图替换当前视图层次结构中的视图(并且可能在实例变量中存储对所选控制器的引用以供以后查找?)
但退后一步,看起来你正在尝试创建一个管理其他视图控制器的视图控制器。从iOS 5开始,现在有非常具体的规则。请查看名为“WWDC 2011 video”的Implementing UIViewController Containment以获取更多信息和示例。