want to go to next view from rightbarbutton item
I am using this code to get the three button on the right side of the navigationbar ,button are visible but next to this I want to go to the next view from this three button image, text ,vedio.
NSArray *segmentTextContent = [NSArray arrayWithObjects:@"Image",@"Text",@"Video",nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.frame = CGRectMake(13, 20, 150, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
if(segmentedControl.selectedSegmentIndex=0)
{
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
}
else if(segmentedControl.selectedSegmentIndex==1)
{
[segmentedControl addTarget:self action:@selector(segmentAction1:) forContr开发者_StackOverflowolEvents:UIControlEventValueChanged];
}
else if(segmentedControl.selectedSegmentIndex==2)
{
[segmentedControl addTarget:self action:@selector(segmentAction2:) forControlEvents:UIControlEventValueChanged];
}
[segmentBarItem release];
//[modalBarButtonItem release];
}
return self;
}
I think you have a fundamental misunderstanding of how actions and targets work. You can't check on the selectedSegmentIndex at the time of creation. It will always be 0. You have to add the target exactly once and inside the responder method, check the selectedSegmentIndex and act from there:
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvent:UIControlEventTouchUpInside];
//...
- (void) segmentAction:(UISegmentedControl *)source {
switch (source.selectedSegmentIndex) {
case 0:
[self segmentAction0:source];
break;
case 1:
[self segmentAction1:source];
break;
case 2:
[self segmentAction2:source];
break;
}
}
精彩评论