I have got it working with a few small problems. Here is my current code. It's not very elegant but it's working.
segmentActionPressed is an NSInteger set in the .h file which I use to specify if the segment control was used to move to the next view.
- (IBAction)segmentAction:(id)sender{
NSIndexPath *selectedRow = CurrentIndexPath; //CurrentIndexPath is an NSIndexPath object that holds the current indexpath.
NSIndexPath *scrollIndexPath;
self.segmentActionPressed = 1;
self.tableView.delegate = self;
if([sender selectedSegmentIndex] == 0)
{
[self.navigationController popViewControllerAnimated:NO];
if(selectedRow.row == 0) //If on the first row when up button is pressed, send us to the last row.
{
scrollIndexPath = [NSIndexPath indexPathForRow:tableDataSource.count -1 inSection:selectedRow.section];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:tableDataSource.count -1 inSection:selectedRow.section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:tableDataSource.count -1 inSection:selectedRow.section]];
}
if(selectedRow.row > 0) //If on any other row when up button is pressed, go to the next row above.
{
scrollIndexPath = [NSIndexPath indexPathForRow:selectedRow.row -1 inSection:selectedRow.section];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:scrollIndexPath.row inSection:selectedRow.section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:scrollIndexPath.row inSection:selectedRow.section]];
}
}
else if([sender selectedSegmentIndex] == 1)
{
[self.navigationController popViewControllerAnimated:NO];
if(selectedRow.row == tableDataSource.count -1) //If on the last row when up down is pressed, go to the first row.
{
scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:selectedRow.section];
[self.tableView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:scrollIndexPath];
}
if(selectedRow.row < tableDataSource.count -1) //If on any other row when up down is pressed, go to the next row below.
{
scrollIndexPath = [NSIndexPath indexPathForRow:selectedRow.row +1 inSection:selectedRow.section];
[self.tableView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:scrollIndexPath];
}
}}
In my -didSelectRowAtIndexPath method, I used the following code to move to the next view without animation if the up/down arrows are used.
if (segmentActionPressed == 0)
{
[self.navigationController pushViewController:tvc animated:YES];
}
else if (segmentActionPressed == 1)
{
[self.navigationController pushViewController:tvc animated:NO];
}
Finally, I set the following code to set the segmentActionPressed to "0" when the parent view appears. I also deselect the row here so you can see which row was selected when you move back to the parent view.
- (void)viewWillAppear:(BOOL)animated {
self.segmentActionPressed = 0;
[self.tableView deselectRowAtIndexPath:CurrentIndexPath animated:YES];}
The main problem is that the segment buttons never appear pressed and the action is sent immediately when touched instead of when the button is released as with the "touch up inside" action.