我想知道通過UISegmentController怎麼操作兩個view。
現在有兩個UIView和UISegmentController。
- (void)changeView:(NSInteger)index {
switch (index) {
case 0:
self.recipeInfoView.alpha = 1;
self.recipeInfoView2.alpha = 0;
break;
case 1:
self.recipeInfoView.alpha = 0;
self.recipeInfoView2.alpha = 1;
break;
default:
break;
}
代碼可以運行,但是每個view都在同一個位置,尺寸也一樣。用起來不方便。
我用的storyboard。
用下面的代碼創建UISegmentedControl,並且給UIColor上色。
- (void)viewDidLoad
{
[super viewDidLoad];
//Make hide of your UIView
self.recipeInfoView.hide = YES;
self.recipeInfoView2.hide = YES;
//Give color of your UIView
self.recipeInfoView.backgroundColor = [UIColor redColor];
self.recipeInfoView.backgroundColor = [UIColor blackColor];
//Create UISegmentedControl Controller
NSArray *itemArray = [NSArray arrayWithObjects: @"FirstView", @"SecondView", nil];
self.segmentedControl= [[UISegmentedControl alloc] initWithItems:itemArray];
self.segmentedControl.frame = CGRectMake(35, 100, 250, 33);
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
self.segmentedControl.momentary=NO;
self.segmentedControl.tintColor=[UIColor darkGrayColor];
[self.segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.segmentedControl];
}
// segmentAction Methods
- (IBAction)segmentAction:(id)sender
{
if([sender selectedSegmentIndex] == 0)
{
if(self.recipeInfoView.isHidden == YES)
self.recipeInfoView.Hidden == NO;
else
self.recipeInfoView.Hidden == NO;
if(self.recipeInfoView2.isHidden == YES)
self.recipeInfoView2.Hidden == YES;
else
self.recipeInfoView2.Hidden == YES;
}
else if([sender selectedSegmentIndex] == 1)
{
if(self.recipeInfoView2.isHidden == YES)
self.recipeInfoView2.Hidden == NO;
else
self.recipeInfoView2.Hidden == NO;
if(self.recipeInfoView.isHidden == YES)
self.recipeInfoView.Hidden == YES;
else
self.recipeInfoView.Hidden == YES;
}
}