Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-15-2009, 09:34 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 4
Default Prev/Next Button in the DetailView

Hi;
I need to put 2 buttons to my application next and previous. I don't want to tap back and select the new view from the tableview. i want to control the application in detailview page with next and prev buttons. I need some help. I try to do but i failed.
Thanks
tarituor is offline   Reply With Quote
Old 08-17-2009, 12:22 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

I think you're going to need to explain what you are looking for a bit better.

If there is a navigation controller involved, have the 'next' button pushViewController and have the 'previous' button popViewController. If not, you'll need to swap views in and out yourself.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 01-14-2010, 07:58 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 3
Default

I think Brian got you wrong. You want to navigate to the next / prev item while you are in the detail view, similar to the mail app where you can display the next/prev message by tapping the little arrows in the top bar, right?
That is exactly what I'm trying to do right now but I'm stuck. I'm not sure how to do some best practice.

I could hand over the complete items array to the detail view together with the row indicator. The prev/next buttons could navigate through that array.

Another solution might be a kind of callback into the listViewController to refresh the Text Fields with the prev/next item.

Do you have a solution on this?
c.schwerdtner is offline   Reply With Quote
Old 01-16-2010, 03:25 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 3
Default

ok, here's my solution on this

I have a root view that displays a list of items. Items is a NSMutableArray. When an item is clicked, the detail view is shown. Normally you hand over the object to display in a way like this:

Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	DetailViewController *detailVC = [[DetailViewController alloc] 
		initWithNibName:@"DetailViewController" bundle:nil];
	detailVC.item = [self.items objectAtIndex:indexPath.row];
	[self.navigationController pushViewController:detailVC animated:YES];
	[detailVC release];
}
The problem here is that the detail view knows about the item but nothing about the list and the current position. So the detail view is unable to navigate itself through the list of items later. We should hand over the items array and the position instead of only the item.
Simply replace
Code:
	detailVC.item = [self.items objectAtIndex:indexPath.row];
with
Code:
	detailVC.currentRow = indexPath.row;
	detailVC.items = self.items;
In the detail view we define
Code:
	NSMutableArray *items;
	int currentRow;
...
	@property (nonatomic, retain) NSMutableArray *items;
	@property (nonatomic, assign) int currentRow;
(be careful not to use retain here because currentRow is a simple datatype, not an object.)

In the detail view I use a segmented control in the navbar to navigate through the items (as you can see in the mail app). To setup the navbar I define
Code:
@property (nonatomic, retain) UISegmentedControl *segControl;
Then I extended the viewDidLoad:
Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// "Segmented" control to the right
	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, 90, 30.0);
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
	segmentedControl.momentary = YES;
		
	UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
	[segmentedControl release];
    
	self.navigationItem.rightBarButtonItem = segmentBarItem;
	self.segControl = segmentedControl;
	[self redrawButtonState];

	[segmentBarItem release];
	
}
The method segmentAction is a callback that says which button was pressed, prev or next. To make this work you also need two icon files showing an up and down arrow ("up.png" and "down.png")
Code:
- (IBAction)segmentAction:(id)sender
{
	// The segmented control was clicked, handle it here 
	
	UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
	switch (segmentedControl.selectedSegmentIndex) {
		case 0: // Up clicked
			if (self.currentRow>0) {
				self.currentRow = self.currentRow-1;
				[self showItem:currentRow];
			}
			break;

		case 1: // Down clicked
			if (self.currentRow < [items count]-1) {
				self.currentRow = self.currentRow+1;
				[self showItem:currentRow];
			}
			break;

		default:
			break;
	}
	[self redrawButtonState];
}
To display the item I use this method
Code:
-(void) showItem:(int)index {
	self.item = [self.items objectAtIndex:index];	
	
	itemTextView.text = [item objectForKey:@"something"];
	...
}
So we can use this any time when we need to display another item. First it is used in viewWillAppear
Code:
-(void) viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self showItem:currentRow];
}
Remember that currentRow is set already in the root view.
Another thing to mention here is that we want to disable the prev button when we show the first item in the list (and disable the next button when we show the last item, too). So I use this method whenever a button is pressed and also when we initially open the view, right after creating the segControl.
Code:
-(void)redrawButtonState {
	[self.segControl setEnabled:self.currentRow>0 forSegmentAtIndex:0];
	[self.segControl setEnabled:self.currentRow<[self.items count]-1 forSegmentAtIndex:1];
}
By the way, don't forget to release your objects in the dealloc.

For me it looks pretty cool now. I hope this is helpful.
c.schwerdtner is offline   Reply With Quote
Old 08-15-2010, 09:34 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 1
Default

Excellent post and explination. Thank you very much for your help I've been looking for this for so long. I had to open an account in the forum to thank you for the code
f315al is offline   Reply With Quote
Old 10-15-2010, 05:28 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 22
Default

I want to do the same, but I have a Sectioned TableView. How can I retrieve the correct IndexPath.row without section info or how can I set Next / Prev IndexPath.
SolidSnake is offline   Reply With Quote
Old 01-11-2011, 07:00 PM   #7 (permalink)
Registered Member
 
betunguemez's Avatar
 
Join Date: Jan 2011
Posts: 1
Default NSDictionary

Quote:
Originally Posted by c.schwerdtner View Post
ok, here's my solution on this

For me it looks pretty cool now. I hope this is helpful.
Thanks a lot, But I still have a little problem with the following: (By the way I am a Noob at programming and Iphone development, been learning on my own so my question might be stupid!)

I am trying to do the same thing as you did but with UIbuttons (one for next and one for previous) instead of a segmented control. Also, the book which I am learning from makes me use a NSDictionary or NSMutableDictionary in the detailView. So when I implement your code i get the following warning:

"NSDictionary may not respond to objectAtIndex"

I understand why, I just have no idea how to do it with a NSDictionary.
(thanks for your time man, and remember I am a Noob at programming too, doing my best to learn!)
Thanks a lot!
betunguemez is offline   Reply With Quote
Reply

Bookmarks

Tags
button, detailview, previous

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 236
16 members and 220 guests
@sandris, ADY, Alsahir, dacapo, Dani77, djohnson, HemiMG, jansan, JasonR, MarkC, mer10, prchn4christ, ryandb2, smethorst, tomtom100
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:59 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0