简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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