我有一个分段控制器,现在并在分段1上点击一个视图,然后在第二个分段上点击另一个视图,就在这个分段控制器的下方。现在我希望当出现这个分段控制器的视图出现时,第一个分段控制器视图随之出现,并且第一个分段控制器按钮似乎预先被分接。
我能够通过这种方式展示我的观点来完成这件事。但是我做了不必要的事情......请以其他方式告诉我。
-(void)loadTest
{
if(!cabinInfoDetailsViewObj)
{
cabinInfoDetailsViewObj = [[CabinInfoDetailsView alloc]initWithFrame:CGRectMake(232, 188, 617,412)];
}
[self addSubview:cabinInfoDetailsViewObj];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:@"16.4_WhiteButton.png"] forSegmentAtIndex:0];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:@"16.4_GreyButton.png"] forSegmentAtIndex:1];
}
这是段控制器部分
- (IBAction)CabinInfoSegmentButtonTapped:(id)sender
{
if(CabinInfoPopupSegmentControl.selectedSegmentIndex == 0)
{
if(!cabinInfoDetailsViewObj)
{
cabinInfoDetailsViewObj = [[CabinInfoDetailsView alloc]initWithFrame:CGRectMake(232, 188, 617,412)];
}
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:@"16.4_WhiteButton.png"] forSegmentAtIndex:0];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:@"16.4_GreyButton.png"] forSegmentAtIndex:1];
cabinInfoDetails.textColor = [UIColor blueColor];
cabinInfoLocate.textColor =[UIColor blackColor];
[cabinInfoDeckPlansViewObj removeFromSuperview];
[self addSubview:cabinInfoDetailsViewObj];
}
if(CabinInfoPopupSegmentControl.selectedSegmentIndex == 1)
{
if(!cabinInfoDeckPlansViewObj)
{
cabinInfoDeckPlansViewObj = [[CabinInfoDeckPlansView alloc]initWithFrame:CGRectMake(232, 188, 617,412)];
}
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:@"16.4_WhiteButton.png"] forSegmentAtIndex:1];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:@"16.4_GreyButton.png"] forSegmentAtIndex:0];
cabinInfoDetails.textColor = [UIColor blackColor];
cabinInfoLocate.textColor =[UIColor blueColor];
[cabinInfoDetailsViewObj removeFromSuperview];
[self addSubview:cabinInfoDeckPlansViewObj];
}
答案 0 :(得分:2)
在我的应用程序中,使用了以下代码:
//Give .h file UISegmentedControl *seg;
- void)viewDidLoad
{
seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Notification", @"Messages", nil]];
seg.tag = 1;
seg.segmentedControlStyle = UISegmentedControlStyleBar;
seg.frame = CGRectMake(1, 0, 318, 45);
[seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
seg.tintColor = [UIColor whiteColor];
seg.selectedSegmentIndex = 0;
seg.backgroundColor=[UIColor clearColor];
[seg setDividerImage:[UIImage imageNamed:@""] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[seg setImage:[UIImage imageNamed:@"notificationselected.png"] forSegmentAtIndex:0];
[seg setImage:[UIImage imageNamed:@"message.png"] forSegmentAtIndex:1];
[self.view addSubview:seg];
}
pragma mark - UISegmentedControl
- (IBAction)segmentAction:(id)sender
{
UISegmentedControl *segControll = (UISegmentedControl *)sender;
if (segControll.selectedSegmentIndex==0)
{
[segControll setImage:[UIImage imageNamed:@"notificationselected.png"] forSegmentAtIndex:0];
[segControll setImage:[UIImage imageNamed:@"message.png"] forSegmentAtIndex:1];
//right code for select the segmentcontroll.index=0
}
else if (segControll.selectedSegmentIndex==1)
{
[segControll setImage:[UIImage imageNamed:@"notification.png"] forSegmentAtIndex:0];
[segControll setImage:[UIImage imageNamed:@"messageselected.png"] forSegmentAtIndex:1];
//right code for select the segmentcontroll.index=1
}
}
我希望它会对你有所帮助。