2

I have a Nav controller that starts at a table view. Each row pushes to a detail UIView. I would like to have a next and previous button on the Detail UIView that would show next and previous views without going back to parent UITableView. All details are saved in an array. how can i access that array and its current index in DetailViewController.m .Thanx in advance.

Piscean
  • 3,069
  • 12
  • 47
  • 96
  • I just posted an answer (with code) in the similar question [here][1] [1]: http://stackoverflow.com/questions/6244776/iphone-navigate-to-previous-next-viewcontroller/11617283#11617283 – software evolved Jul 23 '12 at 17:17

2 Answers2

3

Another clean (and elegant) way is to build some sort of connection between the table view datasource, which is normally the UIViewController that contains the table, and the detail view: this can be done using the Delegate pattern, typical of Cocoa framework. In such case you can define a DetailViewDataDelegate protocol with two methods only:

-(id)nextTableObjectFrom:(id)referenceObject;
-(id)prevTableObject;(id)referenceObject;

where referenceObject is the calling object, that is the object detailed in the DetailView. So DetailView will be defined in this way:


@interface DetailViewController:UIViewController {
}
@property (nonatomic,assign) id currentDetailedObject;
@property (nonatomic,assign) id dataDelegate;

and of course when you call the controller, typical from the tableView:didSelectElementWithIndexPath:animated: method, you will do something like this:


DetailViewController *dv = [[[DetailViewController alloc] initWithNibName:nil bundle:nil] autorelease];
dv.dataDelegate=self;
dv.currentDetailedObject=[mySourceArray objectAtIndexPath:indexPath.row];
[self.navigationController pushViewController:dv animated:YES];

Finally in the DetailViewController when you need the next (or prev) element from the table you will simply call:

-(IBAction)nextButtonPressed {
  self.currentDetailedObject = [self.dataDelegate nextTableObjectFrom:self.currentDetailedObject];
}

Of course the implementation details may change, and in particular the delegate methods, to be implemented in the table's UIViewController, will depend on the data structure. The advantage of this approach, which can be at first sight complicated, is quite elegant and avoids to pass objects along controllers, which is often a source of memory issues. Besides with the delegate approach you can implement any complicated feature (e.g.: you can even manipulate table view objects directly from the DetailViewController, at your own risk of course!)

viggio24
  • 12,316
  • 5
  • 41
  • 34
  • 1
    I am sorry viggio24. I didnt understand the whole code. I am new in Objective-C and iPhone development. I dont have any file DetailViewDataDelegate. Can u explain that delegate file? Thanx – Piscean Feb 09 '11 at 10:43
  • Of course you must define the delegate protocol. So you have to write DetailViewDataDelegate as a protocol: @protocol DetailViewDataDelegate and define (just define, not code) the two delegate functions. The code will be written in the controller that declares itself as DetailViewDataDelegate protocol implementer. There is not enough space here to write this code, but I suggest you to read carefully the great Apple documentation on the subject (you can find very good explanations in Apple's Objective-C programming guide). I recommend this reading as delegate pattern is fundamental in cocoa. – viggio24 Feb 09 '11 at 13:02
2

The cleanest way is to create a custom initializer for DetailViewController:

@interface DetailViewController : UIViewController
{
    NSArray* allObjects;
    NSUInteger displayedObjectIndex;
}

- (id) initWithObjectAtIndex: (NSUInteger) index inArray: (NSArray *) objects;

@end


@implementation DetailViewController

- (id) initWithObjectAtIndex: (NSUInteger) index inArray: (NSArray *) objects
{
    self = [super initWithNibName:nil bundle:nil];
    if ( self ) {
        allObjects = [objects copy];
        displayedObjectIndex = index;
    }
    return self;
}

@end

That way, a DetailViewController always knows both what object it is displaying details for and the previous/next objects, if any.

Costique
  • 23,712
  • 4
  • 76
  • 79
  • It works only for displaying one next page.When i click again on next button i see blank page. I did log on displayedObjectIndex value. and it seems displayedObjectIndex has again the same value which it has before clicking the next button. – Piscean Feb 09 '11 at 10:55
  • My code in next function looks like this: - (IBAction)nextPage:(id)sender { displayedObjectIndex++; DetailViewController *nextController = [[DetailViewController alloc] init]; NSString *storyyTitle = [[allObjects objectAtIndex:displayedObjectIndex] objectForKey:@"title"]; nextController.title = @"FindersFee"; nextController.sTitle = storyyTitle; [self.navigationController pushViewController:nextController animated:YES]; [nextController release]; } – Piscean Feb 09 '11 at 10:57