user698200
user698200

Reputation: 399

How can I go back to the first view from the third view directly?

When I push cancel button in the third view, I want to go back to the first view directly. I also want to remove the second view. How can I do that?

This is the code.

// this part is in the first view.
self.second = [SecondController alloc] init];
[self.view addSubview:second.view];

// this part is in the second view.
ThirdController *thirdController = [[ThirdController alloc] initWithStyle:UITableViewStyleGrouped];             
self.navigationController = [UINavigationController alloc] initWithRootViewController:thirdController];
[self.view addSubview:navigationController.view];

// this part is in the third view.
- (void)cancel {
    [self.view removeFromSuperview]; // this only goes to the second view.
}

EDIT: Can I use popToViewController in called contoller? My app crashes. I thought popToViewController can be used only in calling controller. And popToViewController is used when it was pushed. I did add not push.

Upvotes: 2

Views: 2327

Answers (5)

nekno
nekno

Reputation: 19267

popToViewController:animated: is a UINavigationController method that you use when popping view controllers off the navigation controller stack. It doesn't fit for this scenario.

This user is adding subviews, not pushing them on a navigation controller stack.

As a note, it appears as a matter of design you should be using a navigation controller with the first view as the root controller, then the second pushed on the stack, and the third pushed on the stack. Then all you have to do is [self.navigationController popToRootViewControllerAnimated:YES].

I think this will work if you want to keep your current architecture:

// this part is in the third view.
- (void)cancel {
    // remove the second view (self.view.superview) from the first view
    [self.view.superview removeFromSuperView];
    // can't recall, possibly you still need to remove the third view, but i think removing the superview will do it.
    // [self.view removeFromSuperView];
}

If you prefer to try the UINavigationController route, then the easiest path is to create a new project in Xcode and select the type for a Navigation-Based Application or a Master-Detail Application. This will create a UINavigationController in a nib and add it to your window. You can then set the root view controller in Interface Builder to your FirstViewController class.

If you prefer to create the UINavigationController in code, then that is also possible. I show that below, along with the rest of the code you need, regardless of whether you create your UINavigationController in a nib in IB or in code.

I also recommend reading the View Controller Programming Guide for iOS.

In your app delegate or some other code:

-(void)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions [
    // I would recommend setting up the UINavigationController and FirstViewController as IBOutlets in your nib, but it can be done in code.
    FirstViewController* fvc = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:fvc];

    [window addSubView:navController.view];
    [window makeKeyAndVisible];

    [fvc release];
    [navController release];
}

In the first view controller:

SecondViewController* svc = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
[self.navigationController pushViewController:svc animated:YES];
[svc release];

In the second view controller:

ThirdViewController* tvc = [[ThirdViewController alloc] initWithNibName:@"ThirdView" bundle:nil];
[self.navigationController pushViewController:tvc animated:YES];
[tvc release];

In the third view controller:

-(void)cancel {
    // returns to the first view controller
    [self.navigationController popToRootViewControllerAnimated:YES];
}

Upvotes: 2

rptwsthi
rptwsthi

Reputation: 10172

// this part is in the third view.
- (void)cancel {
    self.first = [SecondController alloc] init];
    [self.view addSubview:second.view];
}

And I think if you have you don't need to be worried about removing beneath view, later these will removed.

Upvotes: 0

Tariq
Tariq

Reputation: 9979

[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];

Upvotes: 3

msgambel
msgambel

Reputation: 7340

Try this:

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

This will pop to the view at index 1. Hope that Helps!

Upvotes: 0

zaph
zaph

Reputation: 112857

Use

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

to go back to a specific view controller.

Upvotes: 0

Related Questions