0

i have a main table view. and a DetailView. when cell is clicked, DetailView of that cell comes which shows details of that cell. DetailView has two buttons next and previous. I wanna know how to disable a detail view button from RootViewcontroller.m. code looks like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    DetailViewController *nextController = [[DetailViewController alloc] init];
    int storyIndex = [indexPath indexAtPosition:[indexPath length] -1];
    nextController = [nextController initWithObjectAtIndex:storyIndex inArray:stories];

    NSString *storyTitle = [[stories objectAtIndex:storyIndex] objectForKey:@"title"];
    nextController.title = @"Details";

    UIBarButtonItem *tempButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
    tempButtonItem.title = @"Back";

    self.navigationItem.backBarButtonItem = tempButtonItem ;

    nextController.sTitle = storyTitle;
    [self.navigationController pushViewController:nextController animated:YES];

    [nextController release];

}

i have already tried nextController.next.enabled=NO and [nextController.next setEnabled:NO] after this line: [self.navigationController pushViewController:nextController animated:YES];

where next is the UIBarButtonItem name which is in DetailViewController. can anybody tell me how to disable that button. thanx in advance

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Piscean
  • 3,069
  • 12
  • 47
  • 96

1 Answers1

1

Viewcontrollers and views are not loaded at the same time. What this means is that when you instantiate an object of DetailViewController in your case, the views are not drawn(and if you are using Nib's) loaded, this is part of the lazy loading concept.

So the first time you send the message setEnabled = NO, the object will be nil(sending messages to objects that are nil is allowed in Objective C).

Example:

[nextController setEnabled:NO] is equal to [nil setEnabled:NO] and this is surely not what you want.

The next time, unless a memory warning and the views are unloaded, the views will be in memory and the reference to the button will no longer be nil so the second time you invoke it, it will work.

And add the code line above the pushViewController:animate

If you want to initialize the button to be disable you may put this code in the viewDidLoad/viewWillAppear depending on the context of your application.

This is only one possible solution.


Edited answer to request in comment:

In your initializer method in the DetailviewController add this:

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRoot)] autorelease];

- (void)backToRoot {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

and also add the method-signature to your headerfile.

Reference to the UINavigationController: http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
  • cool. its working. awesome. can u tell me how can i set target or action of my back button which is on DetailView. infact when i click on 3rd item, i go to the detail view of 3rd item.and after that if i use next button. i go to 4th item's detailView. now i want that if i ll click back button it should go to RootView instead of 3rd item's detailview. i wrote that question on stackoverflow but unfortunately didnt get any answer. – Piscean Feb 11 '11 at 18:03
  • Great. Thank u so much amigo. I was working on that since last 3 days but didnt find any solution. well i just stepped in iphone development before 1 week. have to read alot of things. but thank u so much once again. take care. – Piscean Feb 11 '11 at 18:34
  • is there any way to use back animation when i click previous button on detail view. it takes right details but it has the same animation like its taking next item detail. i want it to work like back animation works. thanx – Piscean Feb 11 '11 at 18:39
  • No problem, but I do have some problem with understanding your last request? – LuckyLuke Feb 11 '11 at 18:59
  • on every DetailView there are two buttons, next and previous. when i click next button, details of next cell comes, and same when i click previous, details of previous cell comes. thats working fine. but animation for both buttons are same. i want that whenever i click previous button animation should be reverse to next button animation. it should show that its going back to previous detailpage. like when i click back button animation is reverse to next. i want that animation with previous button. reverse to next. i dont know i explained it well or ? – Piscean Feb 11 '11 at 19:56
  • If you are pushing/popping viewcontrollers then you can simply write [self.navigationController popViewControllerAnimated:YES], else you need to write some custom animation code. – LuckyLuke Feb 11 '11 at 20:19
  • i tried this: [self.navigationController popViewControllerAnimated:YES] before. only problem is that if i click at cell 3. and then i push previous button,it goes back to direct RootView. Thats the only problem. otherwise its working fine. is there any way to overcome that problem? bcz it ll be hard to write custom code for a beginner :). Tack(thanx) – Piscean Feb 11 '11 at 23:26