user698200
user698200

Reputation: 399

How can I go back to the previous view from root view?

- (void)cancel {
// What should I do here?
}

// root view controller
- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
    self.navigationItem.leftBarButtonItem = cancelButton;
    [cancelButton release];     
}

I added cancel button in the navigation bar. I want to go back to the previous view from root view when cancel button is pressed. What do I have to do in cancel? I added root view controller like this.

RootController *rootController = [[RootController alloc]initWithStyle:UITableViewStylePlain];               
UINavigationController *aNavigationController = [[UINavigationController alloc]initWithRootViewController:rootController];
self.naviController = aNavigationController;
[aNavigationController release];
[rootController release];
[self.view addSubview:[naviController view]];

Upvotes: 1

Views: 813

Answers (4)

Akshay
Akshay

Reputation: 2983

You can pop the view.

[self.navigationController popViewControllerAnimated:YES];

You can dismiss it as well.

dismissModalViewcontrollerAnimated

EDIT: If you are adding the view then you need to remove your view.

[self.view removeFromSuperView];

Upvotes: 3

rptwsthi
rptwsthi

Reputation: 10172

Add This.

[self.view removeFromSuperview]

Upvotes: 0

EmptyStack
EmptyStack

Reputation: 51374

It seems you are not pushing or presenting the aNavigationController. You are just adding it as a subview. Neither popViewController nor dismissModalViewController won't work here. You have to just remove it from its superView. Try this.

- (void)cancel {

    [self.view removeFromSuperView];
}

Upvotes: 0

visakh7
visakh7

Reputation: 26390

If you used a navigationController to push the view you can make use of popViewController: animated: method. If you presented the view as a modal view you can make use of dismissModalViewcontrollerAnimated: method

Upvotes: 1

Related Questions