I tried to programmatically popViewcontroller
By doing this
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[[self navigationController] popViewControllerAnimated:YES];
}
The problem is I have textFields in this VC.If the textField is active and keyboard is displaying, and if I display the AlertView with use to command to resign keyboard ( [[self view] endEditing:YES] or [textField resignFirstResponder] ). And then call the command popViewControllerAnimated:YES . The current VC is dismissed but briefly after the parent VC is appear. There will be a keyboard shown for like 1 second and then disappear.
This behaviour is very annoying. Are there anyway to solve this ? I noticed that by using [[self navigationController] popViewControllerAnimated:NO] The keyboard won't appear . But I prefer to have animation in my app.
Please help.
Thanks in advance
I solved this problem my making the [[self navigationController] popViewControllerAnimated:YES]; delayed when called.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
[[self navigationController] popViewControllerAnimated:YES];
});
#KongHantrakool 's answer works but also has a shortage, you can add [[self view] endEditing:YES] or [textField resignFirstResponder] in - (void)willPresentAlertView:(UIAlertView *)alertView ,it will be better.
You can also try this code:
#pragma mark - UIAlertView Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self performSelector:#selector(popViewController) withObject:nil afterDelay:0.1];
}
- (void)popViewController {
[self.navigationController popViewControllerAnimated:YES];
}
Try this i think its may help you
- (void)viewWillAppear:(BOOL)animated
{
[textField resignFirstResponder];
}
I encountered this problem too, and I found out that the delay solution does not work at all. alertView will remember the status of the keyboard, so when the alertView is dismissed, it will restore the keyboard. So the issue comes out: the keyboard appears about 1 second after we pop the viewController.
Here is my solution:
We just need to ensure the keyboard's status is hidden, before we pop the viewcontroller.
First we add a property and register the keyboard notifications to monitor the keyboard's status:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
#property (nonatomic) BOOL keyboardDidShow;
Implement the funs:keyboardDidHide: and keyboardDidShow:
- (void)keyboardDidHide:(NSNotification *)notification {
self.keyboardDidShow = NO;
if (self.needDoBack) {
self.needDoBack = NO;
[self showBackAlertView];
}
}
- (void)keyboardDidShow:(NSNotification *)notification {
self.keyboardDidShow = YES;
}
Do your pop:
- (void)back {
if (self.keyboardDidShow) {
self.needDoBack = YES;
[self.view endEditing:YES];
} else {
[self showBackAlertView];
}
}
Related
I have three view controllers, say root, middle, last. I want to go back from lastViewController to rootViewController.
I tried custom delegate and NSNotificationCenter. But while going back the middleViewController flashes for a second (it's viewWillDisappear gets called).
I'm using presentViewController/dismissViewController and not navigation. I don't want that middleViewController getting flashed.
I have below code in rootViewCotroller:
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(actionBackToRoot) name:#"BackToRoot" object:nil];
}
-(void)actionBackToRoot : (NSNotification *) notification{
NSDictionary *userInfo = notification.userInfo;
UIViewController *lastViewController = [userInfo objectForKey:#"viewController"];
[middleViewController dismissViewControllerAnimated:NO completion:nil];
[lastViewController dismissViewControllerAnimated:NO completion:nil];
}
On click of close button of lastViewController, code as below -
[dict setValue:self forKey:#"viewController"];
[[NSNotificationCenter defaultCenter]postNotificationName:#"BackToRoot" object:nil userInfo:dict];
Does anyone have solution for this?
Thanks in advance.
Did you tried lastViewController.navigationController.popToRootViewController(animated: true )
From past few days I am facing a weird keyboard issue that is only happening in iPhone 5c only.
I am using objective-C for Development in Xcode-6.4
My environment target is ios7.
Here is How I am handeling keyboard Notification.
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
To Deregister Notification I am writing this piece of code.To be sure I use -resignFirstResponder for every textField.
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self hideKeyBoard];
[self.view endEditing:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)hideKeyBoard{
[kAgeTextField resignFirstResponder];
[kSchoolTextField resignFirstResponder];
}
And In submit button I have checked some condition and then I am showing an AlertView.
- (IBAction)submitClicked:(id)sender
{
if(validated)
{
[self.view endEditing:YES];
[self hideKeyBoard];
[self.view resignFirstResponder];
[self makeApiCall];
}
}
Now when I get Success/Failure Response from server I am doing this.This is the block which runs after getting response from server:
-(void)SuccessfulWithServerInfo:(id)responseInfo
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
dispatch_async(dispatch_get_main_queue(),^{
[appDelegate hideProgressViewFromView:self.view];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"" message:#"Thanks for coming" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[self.navigationController popToRootViewControllerAnimated:YES];
});
}
Problem When I get alertBox and press ok. Then keyboard opens up and closes automatically. This is happening i iPhone 5C only. I checked it in 4s,5s,6 and 6Plus. All are working fine.
If anyone knows about it please assist.
You are displaying alert at same time you are also doing popToRootViewController. May be this will cause problem.
Display alert.
Handle alert view method.
Write [self.navigationController popToRootViewControllerAnimated:YES] in alert view's method.
[UIAlertView showWithTitle:#"" message:#"Thanks for coming" cancelButtonTitle:#"OK" otherButtonTitles:nil] alertViewStyle:UIAlertViewStyleDefault tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex, NSString *text)
{
if(buttonIndex == 1)
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
Hope this will help you.
After Some Research I found this answer In stackOverflow.
Their is some change in the behaviour of AlertView in ios7 and in ios8.
I use this code to solve my issue:
[self performSelector:#selector(showAlertView) withObject:nil afterDelay:0.6];
For details answer please refer to this SO answer
I have a UIPageViewController that has a vertical orientation when it scrolls but it will not scroll to the top when the status bar is pressed. There are no other scroll views on the page if that helps.
Is there a scrolltotop function for a page view controller?
For easier implementation you can use this code in your childVC. This setting will make only visible ChildVC to be scrollsToTop-able.
- (void)viewDidLoad
{
self.collectionView.scrollsToTop =NO;
}
-(void)viewDidAppear:(BOOL)animated
{ self.collectionView.scrollsToTop =YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
self.collectionView.scrollsToTop =NO;
}
UIPageViewController as far as I know does not have any scrollToTop ability. To provide such a feature, I add an observer to listen status bar touch events. I use your appcoda link as reference project, so if you insert codes into related files, it'll work perfectly.
// AppDelegate.m
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[[NSNotificationCenter defaultCenter] postNotificationName:#"touchStatusBarClick" object:nil];
}
// ViewController.m
- (void)viewDidLoad
{
// add this line at the bottom of the function
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(statusBarTouched) name:#"touchStatusBarClick" object:nil];
}
// This function is called when status bar is touched
-(void) statusBarTouched
{
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}
I hope it helps.
I have a strange problem in my app. When I'm pressed backButton from UINavigationController I trying to handle it with -(void) viewWillDisappear:(BOOL)animated in a next way:
-(void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated] ;
[self dismissViewControllerAnimated:YES completion:^{
[self.navigationController popToRootViewControllerAnimated:YES];
}];
}
After it I want do some code in -(void)viewDidAppear:(BOOL)animated of my rootTableViewController. But it's seems like that method wasn't called, but my controller
is changed to root(visual at least ). Have you any idea what I do wrong & how to resolve it?
(This an extension of iOS 7: Keyboard not showing after leaving modal ViewController)
I've got a HomeViewController that uses a NavigationController. Clicking a button takes you to ModalViewController using a modal segue. Pressing the back button then takes you back to the HomeViewController, which then pops up a keyboard. The weird part though is that the keyboard never shows up. I've verified that the UIKeyboardDidShowNotification does get fired , UIKeyboardDidHideNotification doesn't and [textfield isFirstResponder] returns true. Which means the keyboard should be shown right??
I've verified that this only occurs if I have a modal segue, and the NavigationController::shouldAutorotate returns NO.
I've copied the relevant code below. Any help would be greatly appreciated!!
NavigationController.m:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
HomeViewController.m:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (firstTimeComplete) {
UITextField *textField = [[UITextField alloc] init];
[self.view addSubview:textField];
[textField performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:3];
}
}
ModalViewController.m:
- (IBAction)back:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Fixed! The problem was because I was using UIInterfaceOrientationPortrait instead of UIInterfaceOrientationMaskPortrait. I think masks are the expected type for supportedInterfaceOrientations