朋友们,
我需要在两者之间切换不同标题的四到五个视图之间切换
有四个视图示例设置连接打开交易关闭交易
这些是我要在我单击的四个页面之间导航的标题
例如,当我单击它时,我想切换到设置视图,所有其他视图类似,但这些按钮必须在所有视图中
但我只需要一个视图中的这些按钮。当我选择它应该切换到其他视图
朋友们,
我需要在两者之间切换不同标题的四到五个视图之间切换
有四个视图示例设置连接打开交易关闭交易
这些是我要在我单击的四个页面之间导航的标题
例如,当我单击它时,我想切换到设置视图,所有其他视图类似,但这些按钮必须在所有视图中
但我只需要一个视图中的这些按钮。当我选择它应该切换到其他视图
根据四个视图对内容的要求,我建议为分段控件制作一个主视图,并在主视图中设置四个容器视图。他们三个应该被隐藏,然后您可以在四个视图之间切换(显示/隐藏)。
如果视图的代码非常“软”,或者同时运行 4-5 个视图会很慢,这才是一个很好的解决方案。如果是四个核心视图,我更愿意使用标准导航标签栏控件。
//////// 例子 ////////
设置将使用一个 UIViewController 作为背景。在这个视图上,我们将放置一个 UISegmentedControl + 四个容器视图。四个容器视图应放在彼此的顶部。三个容器视图是隐藏的,因此您只能看到一个。
背景视图控制器.h:
#import <UIKit/UIKit.h>
@interface BackgroundViewController : UIViewController {
IBOutlet UISegmentedControl *segmentedControl;
UIView actualView;
}
@property (nonatomic, weak) IBOutlet UIView *containerOne;
@property (nonatomic, weak) IBOutlet UIView *containerTwo;
@property (nonatomic, weak) IBOutlet UIView *containerThree;
@property (nonatomic, weak) IBOutlet UIView *containerFour;
@end
这是分段控件的 IBAction 示例。
- (void) viewDidLoad {
actualView = self.containerOne;
UIView *fromView = nil;
UIView *toView = nil;
self.containerOne.hidden = NO;
self.containerTwo.hidden = YES;
self.containerThree.hidden = YES;
self.containerFour.hidden = YES;
}
- (IBAction)segmentSwitchClick {
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
UIView *fromView = actualView;
UIView *toView = nil;
switch (selectedSegment) {
case 0: {
toView = [self containerOne];
break;
}
case 1: {
toView = [self containerTwo];
break;
}
case 2: {
toView = [self containerThree];
break;
}
case 3: {
toView = [self containerFour];
break;
}
default:
break;
}
}
[UIView transitionFromView:fromView toView:toView duration:1.9 options:UIViewAnimationOptionShowHideTransitionViews | UIViewAnimationOptionCurveLinear
completion:^(BOOL finished) {
if (finished) {
actualView = toView;
}
}];
}
PS我没有尝试过,但它应该可以工作。
在主要内容视图中添加分段控件。然后将其他视图添加为分段控件下的子视图。(设置子视图的框架以使视图不与分段控件重叠)然后为每个子视图设置 IBOutlet。基于分段控件选择索引的分段控件显示和隐藏子视图的操作方法。当您需要显示一个视图时,隐藏其他子视图。
这是一个简单直接的解决方案
下面是在 viewcontroller 的超级视图上添加 3 个视图的示例代码(未测试)
CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view1 = [[UIView alloc] initWithFrame:frame];
UIView *view2 = [[UIView alloc] initWithFrame:frame];
UIView *view3 = [[UIView alloc] initWithFrame:frame];
Then, you want to actually add it to the superview (assuming the view is self.view)
[self.view addSubview:view1];
[self.view addSubview:view2];
[self.view addSubview:view3];