开发者

UITableView w/ Button Initiating with Wrong Parent

开发者 https://www.devze.com 2022-12-13 19:52 出处:网络
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

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:(UI开发者_C百科TableViewStyle) 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;
}


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消