2 exit door eXit_door 于 2013.02.25 15:16 提问

`UISegmentController`和`UIViews`

我想知道通过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。

1个回答

g989_1314125
g989_1314125   2013.02.25 16:02
已采纳

用下面的代码创建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; 

    }
}
Csdn user default icon
上传中...
上传图片
插入图片