0

Scenario
1)I start Editing keyboard comes.
2)Then I Touch a button
3)On botton touch I have Added AlertView,before adding i have resigned first responder
4)On click of AlertView OK Button I pop the viewController
5)after it pops then keyBoard Appears on that screen for a while and dismiss.
6)It should be dismissed on the same controller not on the previous controller

code -

- (IBAction)cancelSkipButtonTouchUpInside:(id)sender {

  [self.textMobileNumber resignFirstResponder];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Entered number will not be saved" delegate:self cancelButtonTitle:nil otherButtonTitles: @"OK",@"Cancel", nil];
    alertView.tag = ktagYourNumberWillNotBeSaved;
    [alertView show];
    alertView = nil ;

}


- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
case ktagYourNumberWillNotBeSaved:
   {
    [self.navigationController popToViewController:self animated:YES];
     }      
 }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Rohit Pradhan
  • 3,867
  • 1
  • 21
  • 29

4 Answers4

1

in here you are added self it means your navigation stack call the same ViewController, please mostly avoid choice no- 1 & 2

change

 [self.navigationController popToViewController:self animated:YES];

into

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

else choice no-2

 for (UIViewController *vc in self.navigationController.viewControllers) {
   if ([vc isKindOfClass:[ViewController2 class]]) // ViewController2 --> call your view controller where you want to pop
 {
    [self.navigationController popToViewController:vc animated:YES];
}
}

else choice-3

[self.navigationController popViewControllerAnimated:YES]
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • For simply moving to previous viewController use `[self.navigationController popViewControllerAnimated:YES]` – Jaideep Nagra Sep 03 '15 at 09:08
  • ya your answer also fine , just wait I modify my answer, tanx Jaideep – Anbu.Karthik Sep 03 '15 at 09:09
  • can you give reason for why you put at negative mark, we don't know exactly which scenario you working on , so we submit our answer in randomly – Anbu.Karthik Sep 03 '15 at 09:12
  • I saw it some time secs back – Anbu.Karthik Sep 03 '15 at 09:14
  • Do check [this](http://meta.stackexchange.com/questions/12984/is-there-a-way-to-see-who-voted-on-your-posts) – Jaideep Nagra Sep 03 '15 at 09:17
  • Remove 1 and 2 choices they are bad. In case if he need to go to previous VC - the correct one is 3rd. – AlexZd Sep 03 '15 at 09:20
  • @AlexZd -- we don't know the scenario , thats why I given the options , Ok I mentioned that preferable answer for 3rd – Anbu.Karthik Sep 03 '15 at 09:21
  • please can you give solution about how to hide keypad in case 8.4 – Rohit Pradhan Sep 03 '15 at 10:19
  • you can do this three ways 1. add toolbar above the keypad, the tool bar contains "done" button , call the method for done method, 2. add the tap gesture for view controller , in gesture call [self.view end editing:yes]; 3. touch event , when the touch began is call , add [self.view end editing:yes];, else finally call viewwillDisappear set [yourkeyboardname resignfirestResponder]; – Anbu.Karthik Sep 03 '15 at 10:25
  • see [tap gesture](http://stackoverflow.com/questions/5711434/how-can-i-dismiss-the-keyboard-if-a-user-taps-off-the-on-screen-keyboard), – Anbu.Karthik Sep 03 '15 at 10:28
1

There is two option you have
1) You can change

[self.navigationController popToViewController:self animated:YES];    

into

[self.navigationController popViewControllerAnimated:YES];

2)

for (UIViewController *controller in self.navigationController.viewControllers) {

        if ([controller isKindOfClass:[ViewController class]]) {

            [self.navigationController popToViewController:controller
                                                  animated:YES];
            break;
        }
    }
Popeye
  • 11,839
  • 9
  • 58
  • 91
Jayesh Mardiya
  • 770
  • 7
  • 17
0

In case if you need simply close Keyboard when you tapping button which calls cancelSkipButtonTouchUpInside: use following line inside:

[self.view endEditing:YES];
AlexZd
  • 2,152
  • 2
  • 18
  • 29
0

I have solved this by using UIAlertController instead of UIAlertView as it is deprecated for iOS 8

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *alertActionOk = [UIAlertAction actionWithTitle:NSLocalizedString(@"Ok", @"Ok action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self.navigationController popToViewController:self animated:YES];
    }];
    UIAlertAction *alertActionCancel = [UIAlertAction actionWithTitle:NSLocalizedString(CANCEL_ALERT_BUTTON, @"Cancel action") style:UIAlertActionStyleDefault handler:nil];

    }
    [alertController addAction:alertActionOk];
    [alertController addAction:alertActionCancel];

    [self presentViewController:alertController animated:YES completion:nil];
Rohit Pradhan
  • 3,867
  • 1
  • 21
  • 29