tags:

views:

32

answers:

3

I need to add this to my dismiss button :-

[self dismissModalViewControllerAnimated:YES];
[self release];

else

[self.view removeFromSuperview];

I thought

if( self.navigationController.modalViewController ) {

would work be it nevers true

A: 

Normally for a "dismiss" button I would call a method in the controller that presented the modal controller (use a delegate), not try to dismiss the modal view controller from within itself. I don't quite get what youre trying to do though, but that [self release] looks bad. I don't think you ever want to release self like that.

Nimrod
I`m reusing the same view, but I have to call it differently, adding it as a subview and pushing it modally.
Jules
I think I see what you mean. Perhaps create your own ivar called BOOL subviewPresentedModally, have that be set by whatever presents the view, then in the "dismiss" method just if() on that. Basically just create your own version of the variable you're trying to if() on.
Nimrod
A: 

Try this in you modal viewcontroller:

- (IBAction)close:(id)sender {
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

Then just connect the button's action to that method.

aegzorz
No that doesn`t work, as I`ve calling the view with [window addSubview:lvc.view]; I need an IF and ELSE as I`m calling the view two different ways
Jules
A: 

A couple of things:

1) You shouldn't ever release yourself in an object. If you're presenting a modal view controller, you should perform the release there since the view controller will now be retained by the view controller's .modalViewController property:

(In the parent):

UIViewController *someViewController = [[UIViewController alloc] init];
[self presentModalViewController:someViewController animated:YES];
[someViewController release];

2) The parent will store its child modal view controller in .modalViewController. The child will have its .parentViewController property set in this case. If the view has been added as a subview, its .superview property will be set. These are not mutually exclusive, however, so be careful. Generally speaking, UIViewControllers are intended to host full-screen views, and if you're adding the view as a subview, you should ask yourself if the view should just be a UIView subclass, and move the logic into the parent view controller.

That said, I suppose you could check your case (assuming you don't present modal view controller and add as a subview simultaneously):

if (self.parentViewController) {
    [self dismissModalViewControllerAnimated:YES];
} else if (self.view.superview) {
    [self.view removeFromSuperview]
}

In the latter superview case, the view controller will still be hanging around, so you'd need to let the other view controller know via delegate method or something to release you. In the first case, if you have released the presented view controller already as I described above, it will be released automatically when the parent view controller sets its .modalViewController property to nil.

atticus