简体   繁体   English

在“视图”负载上启用细分控制器视图

[英]enable a segment controller view on View load

i have a segment controller, now and on its tap on segment 1 , a view appears and on tap of second segment another view is show, just below this segment controller. 我有一个段控制器,现在并在其点击段1时,出现一个视图,在第二段的点击中,显示另一个视图,就在此段控制器下方。 now i want when the view on which this segment controller is present appears, the 1st segment controller view appears with that and the first segment controller button appears to be tapped, in advance. 现在我想在出现此分段控制器的视图时,与之一起出现的第一个分段控制器视图和第一个分段控制器按钮似乎被预先敲击。

i am able to get this thing done by showing my view in this manner. 我可以通过以这种方式显示我的观点来完成此任务。 but i am doin unecessary thing.. please advise me some other way. 但是我在做不必要的事情..请以其他方式告诉我。

-(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];

}

this is segment controller part 这是段控制器的一部分

- (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];

}

in my application,used this code : 在我的应用程序中,使用以下代码:

//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
  }
}

i hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM