Jules
Jules

Reputation: 7774

iPhone, need the IF for dismissModalViewControllerAnimated ELSE removeFromSuperview?

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

Upvotes: 1

Views: 1267

Answers (3)

atticus
atticus

Reputation: 960

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.

Upvotes: 1

aegzorz
aegzorz

Reputation: 2219

Try this in you modal viewcontroller:

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

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

Upvotes: 0

Nimrod
Nimrod

Reputation: 5133

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.

Upvotes: 0

Related Questions