siddle
siddle

Reputation: 137

Back screen button working in IOS

i have a view controller on which i have a mapview. Above the mapview there is a back button on which when click it should move back to view controller. But when i hit back button it does not go to the back screen. My code is this in view didLoad,

 UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(10, 20, 30,30)];
[backButton setImage:[UIImage imageNamed:@"row.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
- (void) popVC{
HomeViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
[self.navigationController pushViewController:presales animated:YES];

}

the button is like this, enter image description here

Upvotes: 0

Views: 45

Answers (2)

trungduc
trungduc

Reputation: 12144

To move back to HomeViewController

If you use pushViewController to show current view controller. You have to use popViewControllerAnimated instead of pushViewController. Try replace your popVC with my code below.

- (void) popVC{
    [self.navigationController popViewControllerAnimated:YES];
}

If you use presentViewController to show current view controller. You have to use dismissViewControllerAnimated instead of pushViewController. Try replace your popVC with my code below.

- (void) popVC{
    [self dismissViewControllerAnimated:YES completion:nil];
}

Upvotes: 1

Faysal Ahmed
Faysal Ahmed

Reputation: 7669

Try this:
By Writing the First Line you get the Indexes of all View Controllers and from second Line You will reach up to your Destination.

NSArray *array = [self.navigationController viewControllers];

[self.navigationController popToViewController:[array objectAtIndex:2] animated:YES];

Upvotes: 0

Related Questions