views:

217

answers:

1

Hi,

I have a UITableView with a list of items such as item1, item2, item3, item4 and so on.

If I select on item1, it will go to the detail view of item1 (which is controlled by DetailsViewController).

What I am trying to do is to add a button called "Next" on DetailsView. When clicked, the DetailsView will refresh and show item2. When clicked again, it will show item3 and so on.

I have been searching high and low for that to implement. But to no avail.

Many thanks in advance!!!

A: 

I would make the table view controller a delegate of the details view controller. Then do something like this in the details view controller:

-(IBAction) nextButtonClicked:(id)inSender {
  NSDictionary *data = [delegate detailView:self nextDetailsForCurrent:myDetails];
  if ( data ) [self setDetails:data];
}

You could make a much more robust delegate protocol than this oversimplified method, but hopefully this will get you started.

Edit:

There is nothing inherently special about a delegate. It is just one object that makes decisions on behalf of another object. In Objective-C a delegate is usually defined as a protocol, although it used to just be a category of NSObject.

Create a delegate protocol like this:

@protocol DetailDelegate <NSObject>
  -(NSDictionary *) detailController:(DetailController *)sender nextDetailsForCurrent:(NSDictionary *)current;
@end

Give the detail view controller a property like this:

@property (nonatomic,assign) id<DetailDelegate> delegate;

Notice the assign instead of retain. By convention a delegate is not retained, even though it must remain valid for the life of the other object. The id form just means the object conforms to the protocol.

When the table view controller creates the details view controller, it sets itself as the delegate.

drawnonward
Hi, thanks for suggestion.How to make table view controller a delegate of the details view controller? Sorry, I'm a newbie with programming.Thanks!
leo