[英]How to correctly switch between two UIViews by using segment control ios?
我想這兩者之間進行切換UIViews
使用segmentControl
。現在這兩個的UIViews
嵌入了scrollView
的own.These的UIViews
都應該表現為一個,所以我把它們放在對方(在XIB)的頂部。而當segmentControl
單擊,我試圖相應地隱藏/顯示它們。 但是到目前為止,我無法在兩者之間進行切換。下面還嘗試了解決方案,但是它僅適用於隨機單元格,並且無法在所有單元格中切換。缺少什么?
這就是我將上層視圖設置為隱藏在didSelectRow
位置,在那里我正在擴展tableViewCell
。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.expandedCells containsObject:indexPath]) {
expCell.upperContainer.hidden = NO;
expCell.upperScroll.hidden = NO;
[self.expandedCells removeObject:indexPath];
}else{
isExpanded=YES;
[self.expandedCells addObject:indexPath];
//hide upper container
if (!expCell.upperContainer.hidden) {
expCell.upperContainer.hidden = YES;
}
if (!expCell.upperScroll.hidden) {
expCell.upperScroll.hidden =YES;
}
}
[self.bTableView beginUpdates];
[self.bTableView reloadData];
[self.bTableView endUpdates];
}
在segmentControl上的expandedCell中,單擊以執行以下操作。
-(void)selectDeckView:(UISegmentedControl*)sender{
if (sender.selectedSegmentIndex==0) {
NSLog(@"segment 0"); //executes
expCell.lowerDeckView.hidden=NO;
expCell.lowerScrollView.hidden=NO;
expCell.upperScroll.hidden=YES;
expCell.upperContainer.hidden=YES;
}else if (sender.selectedSegmentIndex==1){
NSLog(@"segment 1"); //executes
expCell.lowerDeckView.hidden=YES;
expCell.lowerScrollView.hidden=YES;
expCell.upperContainer.hidden=NO;
expCell.upperScroll.hidden=NO;
}
}
我建議將每個視圖都放在自己的視圖控制器中,然后每當按下一個段時添加適當的子視圖控制器。
包含分段控件的視圖控制器將是容器視圖控制器,並且它將具有類似的方法,該方法將添加兩個內容視圖控制器之一(每個都包含您提到的一個視圖),當一個段被調用時按下:
- (void) displayContentController: (UIViewController*) content;
{
[self addChildViewController:content]; // 1
content.view.frame = [self frameForContentController]; // 2
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self]; // 3
}
您可以緩存子視圖控制器實例,以使它們僅創建一次(如果合適的話),這意味着它會很快。 Apple關於內容/子視圖控制器的文檔:
您可能有一個涵蓋其他視圖的視圖,是否嘗試過:
if (sender.selectedSegmentIndex==0) {
NSLog(@"segment 0"); //executes
expCell.lowerDeckView.hidden=NO;
expCell.lowerScrollView.hidden=NO;
expCell.upperScroll.hidden=YES;
expCell.upperContainer.hidden=YES;
[self.view bringSubviewToFront:expCell.upperScroll]
[self.view bringSubviewToFront:expCell.upperContainer]
}else if (sender.selectedSegmentIndex==1){
NSLog(@"segment 1"); //executes
expCell.lowerDeckView.hidden=YES;
expCell.lowerScrollView.hidden=YES;
expCell.upperContainer.hidden=NO;
expCell.upperScroll.hidden=NO;
[self.view bringSubviewToFront:expCell.lowerScrollView]
[self.view bringSubviewToFront:expCell.lowerDeckView]
}
}
確保以正確的順序將它們放到最前面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.