I have a tableview with detailview which loads data from a plist. On the detail view I have added a UISegmentedControl into the navigationItem.rightBarButtonItem with up and down arrow images. I want to advance to the next detail/row (without going back to the RootViewController table view) when the the "down" arrow is clicked and go to the previous detail/row when the "up" arrow is clicked. How would I go about doing that?
Thank you.
RootViewController.m
Code:
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0){
InfoViewController *controller = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:[NSBundle mainBundle]];
controller.title =@"My Title";
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
if (indexPath.section==1){
DetailViewController *controller = [[DetailViewController alloc]
initWithDvdData:[dao libraryItemAtIndex:indexPath.row]
nibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
NSLog(@"%d", indexPath.row);
//controller.title = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"title"];
controller.title = [NSString stringWithFormat:@"Movement #%@", [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"movementnumber"]];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
}
DetailViewController.m
Code:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Segmented Control - Custom right bar button with a view
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:@"up.png"],
[UIImage imageNamed:@"down.png"],
nil]];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 70, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
titleLabel.text = [dvdData valueForKey:@"title"];
// etc....
Do something when the segmented control is clicked (go to the next or previous record)
Code:
- (void)segmentAction:(id)sender
{
UISegmentedControl* segCtl = sender;
// the segmented control was clicked, handle it here
if([segCtl selectedSegmentIndex]==0){
NSLog(@"You clicked the up arrow - the segment clicked was %d", [segCtl selectedSegmentIndex]);
}else {
NSLog(@"You clicked the down arrow - the segment clicked was %d", [segCtl selectedSegmentIndex]);
}
}