views:

109

answers:

2

I am trying to place a "Dismiss" or "Done" button in the top right corner in a navigation control for a ModalView that closes the view. Using a normal UIView, I am able to do this. However, when I try to use a UITableView for my ModalView, I am no longer able to set the BarButton's target to a parent controller.

The "dismissModalController" method is in a separate controller than the TableView's, but the button wants to default to the TableViewController rather than using a target Controller where "dimissModalController" is.

How can I replicate the functionality of the button in UIView with that of the UITableView?

UITableView Initiation (Not Working):

   -initWithStyle:(UITableViewStyle) style {
        ToolbarController *myParent;
 if(self=[super initWithStyle:style]) {
  self.title=@"Widgets List";
  UIBarButtonItem *rightButton = 
  [[UIBarButtonItem alloc] initWithTitle:@"Dismiss" 
           style:UIBarButtonItemStyleDone
          target:myParent
          action:@selector(dismissModalController)
   ];
  self.navigationItem.rightBarButtonItem=rightButton;
  [rightButton release];
 }
 return self;}

UIView Initiation (Works):

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    parent:(ToolbarController*) myParent{
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  UIBarButtonItem *rightButton = 
  [[UIBarButtonItem alloc] initWithTitle:@"Dismiss" 
           style:UIBarButtonItemStyleDone
          target:myParent 
          action:@selector(dismissModalController)
   ];
  self.navigationItem.rightBarButtonItem=rightButton; 
  [rightButton release];

 }
 return self;
}
+1  A: 

You are not setting "myParent" in initWithStyle before you use it.

Also, you can dismiss the modal view controller from itself rather than setting up a delegate. I find this simpler. Here is the iPhone Dev Center discussion of dismissModalViewControllerAnimated:

The parent view controller is responsible for dismissing the modal view controller it presented using the presentModalViewController:animated: method. If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller.

gerry3
A: 

Tried something that works but throws a warning. However, the warning never ends the program. Basically I set parent in the parameter and pass a ToolbarController when I call this method. A hybrid between the two other attempts. I didn't think it would work and it's probably bad practice, but it does fix the problem.

-initWithStyle:(UITableViewStyle) style parent:(ToolbarController*) myParent {
     self.title=@"Widgets List";
     UIBarButtonItem *rightButton = 
     [[UIBarButtonItem alloc] initWithTitle:@"Dismiss" 
              style:UIBarButtonItemStyleDone
             target:myParent
             action:@selector(dismissModelController)
      ];
     self.navigationItem.rightBarButtonItem=rightButton;
     [rightButton release];
    return self;
}
LoganFrederick