这是主视图,我想切换两个分开的视图;用于ON状态和OFF状态。
代码的样子如何?
答案 0 :(得分:1)
你可以在xib中获取两个视图并在视图中添加两个视图在视图中加载并将属性隐藏为你不想显示的属性,然后为开关控制事件设置隐藏属性TRUE或FALSE关闭。
答案 1 :(得分:1)
执行以下操作
- (void)viewDidLoad //of mainViewController {
//do viewController1 alloc init // Gray Color
[self.view addSubview:viewController1.view];
//do viewController2 alloc init //Red Color
[self.view addSubview:viewController2.view];
[viewController2.view setHidden:YES];
}
//assign this method to segmented controls' valueChanged event
- (IBAction)segmentControlClicked:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender; // if segmented control is not declared as an IBOutlet
if (segmentedControl.selectedSegmentIndex == 0) {
[viewController1.view setHidden:NO];
[viewController2.view setHidden:YES];
}
else if (segmentedControl.selectedSegmentIndex == 1) {
[viewController2.view setHidden:NO];
[viewController1.view setHidden:YES];
}
}
确保'y'原点viewController1和viewController2位于分段控件下方,以便不隐藏分段控件。
答案 2 :(得分:0)
尝试以下
- (IBAction)segmentControlClicked:(id)sender {
if (!viewController1) {
//do alloc init
}
if (!viewController2) {
//do alloc init
}
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
if (segmentedControl.selectedSegmentIndex == 0) {
[viewController1.view setHidden:NO];
[viewController2.view setHidden:YES];
}
else if (segmentedControl.selectedSegmentIndex == 1) {
[viewController2.view setHidden:NO];
[viewController1.view setHidden:YES];
}
}