acedanger acedanger - 1 year ago 170
Objective-C Question

iOS equivalent of Android finish()

I am relatively new to iOS development. I developed an app for Android first, and now am porting it to iOS. One thing that I can't figure out is how to make a

"go away". I'm accustomed to the
method in Android. With that method, the current activity ends itself and the user is presented with the the previous screen that was open prior to opening the current screen.

What I'm trying to accomplish is making my "create" screen go away after a record is saved. In the Android world, I would just call the
method and that would be taken care of. What is the iOS equivalent?

I have tried the following code in my iOS app, hoping that the view would be animated away.

[self.navigationController popToRootViewControllerAnimated:YES];

The view was presented as below.

SettingsViewController *vc = [[SettingsViewController alloc] initWithNibName:@"SettingsView"];
controllers = [NSArray arrayWithObject:vc];
self.sideMenu.navigationController.viewControllers = controllers;
[self.sideMenu setMenuState:MFSideMenuStateClosed];

is defined as such:

NSArray *controllers = nil;

Answer Source

How did you present this view controller?

Did you use presentViewController:animated:completion:? If so, you want something like this:

[self dismissViewControllerAnimated: YES];

If you are using pushViewController:animated:, you are not talking about a modal view. You are talking about a normal ViewController you pushed onto the stack. To "undo" this, you need to pop the view controller:

[self.navigationController popViewControllerAnimated: YES];