Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly display a new view and how to go back to the previous view

I'm very new to iPhone development and Objective-C. Today, I figured out how to open a new ViewController and how to return to the previous one.

Here's how I currently do this:

// In the main view controller I have a method called openSecondView that is defined like this:

- (void) openSecondView:(id)sender {
    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:secondView animated:YES];
}

// In the SecondViewController I have a back button that calls a method called closeView that is defined like this:

- (void)closeView:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

My question is, how do you do properly accomplish this?

Should I call [secondView release] after calling presentModalViewController or is this done some what behind the scenes? I ask this because when I was debugging I noticed that presentModalViewController doesn't seem to be a blocking code, the next few lines of code I added seem to execute immediately, without calling dismissModalViewControllerAnimated. Are there any consequences of calling [secondView release] after presentModalViewController?

Any help/advise would be much appreciated.

like image 284
Christian Resma Helle Avatar asked Dec 06 '25 16:12

Christian Resma Helle


1 Answers

Just call [secondView release] after calling presentModalViewController. The view controller will be retained until it is dismissed.

like image 184
Felix Avatar answered Dec 08 '25 06:12

Felix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!