I have a UIViewController then with this code I present a modal view controller for entering the password.
[self performSegueWithIdentifier:#"SeguePassword" sender:self];
in Storyboard:
Storyboard Segue
Identifier = SeguePassword
Style = Modal
Transition = default
Animates = not checked
When I click "Cancel" on the modal which has the following code:
[self dismissViewControllerAnimated:NO completion:^{}];
Now when I get back the keyboard is hidden. I have code to show a toolbar with a button called hide. And I see that, but not the keyboard.
Does anyone have any ideas or directives they have taken to fix an issue like this? It seems it recently started after converting changes for iOS 7.
My solution:
I found this because after I clicked the but on an alert box a few times out of frustration I saw the keyboard in the wrong orientation. I.e.., a landscape keyboard when the app is portrait only on iPhone.
Before:
- (NSUInteger) supportedInterfaceOrientations {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
} else {
**return (UIInterfaceOrientationPortrait);**
}
}
After:
- (NSUInteger) supportedInterfaceOrientations {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
} else {
**return (UIInterfaceOrientationMaskPortrait);**
}
}
I changed the Portrait to use the Mask integer and everything started working again.
The keyboard is only visible if some object is the firstResponder. Look at the documentation for the UIResponder class.
If your modal view controller uses the keyboard, which it sounds like it does, it takes first responder status and whatever had it on your original screen loses it. If you want to return to the same state from the modal view controller, with some object having input focus -- i.e. being the firstResponder -- with the keyboard visible you will have to have write code to make that happen. In particular, your original view controller has to know when the modal view controller terminates so that it can make your input object, textField or textView or whatever, be the first responder with the statement:
[objectIWantToBeFirstResponder becomeFirstResponder];
Now how do you know when the modal view controller is done?
There are a couple obvious techniques.
If you want to use a segue then the modal view controller will have to
explicitly let the presenting view controller know that it is
terminating. You will probably want to define a protocol in the
modal view controller and have the presenting view controller adopt
it. When the modal view controller wants to exit it first tells the presenting
view controller.
Since you are manually triggering the segue perhaps
you're willing to present the modal view controller in code. If so then
you can use a completion block:
ModalViewControllerClass *vc = [[ModalViewControllerClass alloc] init];
// whatever else you need to do to the vc
[self presentViewController:vc animated:YESorNO completion:^{
// This code gets executed when the presented view controller exits
[objectIWantToBeFirstResponder becomeFirstResponder];
}];
Related
Update for iOS 9 beta: Apple may have fixed this for iOS 9. If you work(ed) around this issue for iOS 8, make sure it also works correctly on iOS 9.
In storyboard, I've created a popover presentation segue to present a navigation and view controller from a button, as well as creating an unwind segue.
In portrait orientation, the modal (fullscreen) presentation is unwound/dismissed, as expected.
In landscape orientation, the unwind segue also gets called, however the popover presentation is not automatically dismissed.
Did I miss hooking something up? Do I have to dismiss the popover presentation myself?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)__unused sender
{
if ([[segue identifier] isEqualToString:#"showSelectBookChapter"])
{
UINavigationController *navigationController = segue.destinationViewController;
if ([navigationController.topViewController isKindOfClass:[BIBLESelectViewController class]])
{
BIBLESelectViewController *selectViewController = (BIBLESelectViewController *)navigationController.topViewController;
selectViewController.initialBookChapterVerse = self.bookChapterVerse;
}
}
}
- (IBAction)unwindToBIBLEChapterViewController:(UIStoryboardSegue *)segue
{
if ([segue.identifier isEqualToString:#"unwindToBIBLEChapterViewController"]) {
if ([segue.sourceViewController isKindOfClass:[BIBLESelectViewController class]])
{
BIBLESelectViewController *sourceViewController = (BIBLESelectViewController *)segue.sourceViewController;
self.bookChapterVerse = sourceViewController.selectedBookChapterVerse;
[self.tableView reloadData];
}
}
}
Update:
After looking at gabbler's sample code, I've narrowed the problem down to popover dismissing fine in a Single View Application, but not in a Master-Detail Application.
Update 2:
Here's what the hierarchy looks like (omitting navigation controllers for simplicity's sake), in answer to the question Luis asked:
Split view controller
Master view controller
Detail view controller
Chapter view controller (modal page sheet)
Select view controller (the problematic popover that unwinds to chapter view controller, but doesn't dismiss)
As I mentioned in the previous update, I created an new master/detail template, and simply presented a popover directly from (a button in) the detail view. It won't dismiss.
I ran into this problem too. I present a View Controller modally (as a form sheet), from the Master View Controller (UISplitViewController). The problem only occurred on the iPad (probably the iPhone 6+ in landscape mode too, but I didn't check it). I ended up doing the following in my unwind action method (using Swift), and it works good.
if !segue.sourceViewController.isBeingDismissed() {
segue.sourceViewController.dismissViewControllerAnimated(true, completion: nil)
}
If you segue as a popover from a view controller embedded in a navigation controller, the corresponding unwind fails to dismiss the popover.
It's a bug in -[UINavigationController segueForUnwindingToViewController:fromViewController:identifier]. The embedding navigation controller is supposed to supply a segue that will dismiss the popover but it doesn't. The fix then is to override this and supply a working segue, which we can get from the embedded view controller.
Here's a partial solution that will only handle unwinding to the top view controller of the navigation stack:
#implementation MyNavigationController
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController
fromViewController:(UIViewController *)fromViewController
identifier:(NSString *)identifier
{
if (toViewController == self.topViewController && fromViewController.presentingViewController == self)
return [toViewController segueForUnwindingToViewController:toViewController
fromViewController:fromViewController
identifier:identifier];
else
return [super segueForUnwindingToViewController:toViewController
fromViewController:fromViewController
identifier:identifier];
}
#end
It works on iOS 8 for both landscape/portrait iPad and landscape/portrait iPhone. The logic should be robust enough to survive on iOS 9.
It is/must be a behavior of the popOver segue, in normal situations or regularly we need that the popOver keeps in view, if the segue show something important is annoying that we lost that information just because we rotate the device, I guess that that is the reason of that native behavior. So if we want for it to dismiss automaticly we have to make that behaivor by our own, this works:
in the method - (void)viewDidLoadin the detailViewController.m add this:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
then create this method:
- (void) orientationChanged:(NSNotification *)note{
UIDevice * device = note.object;
//CGRect rect = [[self view] frame];
switch(device.orientation)
{
default:
[self dismissViewControllerAnimated:YES completion:nil];
break; }}
You said that in a single view happens what you want, but I've never seen that behavior when I used popOvers.
mbeaty's fix is great but as others have pointed out, this bug seems to know be fixed in iOS 9 and it also doesn't work well for universal device design. I have adapted his answer to handle both situations. Here is the code:
#IBAction func yourUnwindSegue(segue: UIStoryboardSegue) {
if #available(iOS 9, *) {
return // bug fixed in iOS 9, just return and let it work correctly
}
// do the fix for iOS 8 bug
// access your SplitViewController somehow, this is one example
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let splitVC = appDelegate.window!.rootViewController as! YourSplitViewController
// if the source isn't being dismissed and the splitView isn't
// collapsed (ie both windows are showing), do the hack to
// force it to dismiss
if !segue.sourceViewController.isBeingDismissed() && splitVC.collapsed == false {
segue.sourceViewController.dismissViewControllerAnimated(true, completion: nil)
}
}
This first checks if iOS 9 is running and just exit as the bug seems to be fixed. This will prevent the multiple views getting dismissed issue. Also to make sure this fix is only done when the splitView is showing two windows (to make it happen only on iPads and iPhone 6 Plus in landscape as well as future devices) I added the check to make sure it is not collapsed.
I have not exhaustively check this but it seems to work. Also not that my app is set for a min of iOS 7, I don't know if this bug existed then so you may need to look into that if you support below iOS 8.
I have a problem with an app I am working on that goes like this:
The app's window has a rootViewController which is set to a custom class (MenuViewController) of UIViewController. This view controller has a rootViewController property of it's own. Whenever set this happens (really short version of the code):
- (void)setRootViewController:(UIViewControlelr *)rootViewController
{
...
_rootViewController = rootViewController;
if (self.rootViewController) {
[self addChildViewController:rootViewController];
[self.view addSubview:rootViewController.view];
}
...
}
Now this MenuViewController can show a modal view controller on top of it's rootViewController.
I do that like this:
[self.rootViewController presentModalViewController:viewController animated:YES completition:nil];
Everything looks to be ok until here. Now on iPad whenever I call [self.presentingViewController dismissViewControllerAnimated:YES completion:nil] from my modal view controller the interface beneath it rotates to the same orientation (that is UIInterfaceOrientationLandscapeLeft) regardless of what the initial orientation was when the view controller was presented.
So to conclude, my view's hierarchy is this:
Window
|
- Menu View Controller
|
- Root View Controller
|
- Modal view controller
Does anyone know how I can fix this? It doesn't happen on the iPhone.
It sounds to me as though on the iPad your MenuViewController's rootViewController supports multiple interface orientations, whereas on the iPhone it does not. This is speculation, as you have not said anything about this.
If this is the case, and the rootViewController does indeed support multiple interface orientations, the fix would be to override - supportedInterfaceOrientations and return portrait, which seems to be what you are suggesting you would want.
I have an iOS application using storyboards where I display a view controller that I create from an .xib file to the user. This view controller accepts some user input, but I then have to dismiss it and return to the main application. I am able to display the view controller, which also has a button that calls a method to dismiss the view controller. My problem is that after the user presses the button to go back to the main application, the entire screen goes black. Here is my code for the button from the .xib view controller that is trying to remove itself from the display:
- (IBAction)myButtonAction:(id)sender {
[self.view removeFromSuperview];
}
Here is the code from my main application's view controller which calls the .xib view Controller in the first place:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
_nextView = [[NextLandscapeViewController alloc] initWithNibName:#"NextLandscapeViewController" bundle:nil];
[_nextView setDelegate:(id)self];
NextNavigationController *navigationController = [[NextNavigationController alloc] initWithRootViewController:_nextView];
[self presentViewController:navigationController animated:YES completion:nil];
}
NextNavigationController is a subclass of UINavigationController which I do for the purpose of loading _nextView in landscape mode instead of portrait mode. This part is working fine. My concern now is dismissing this viewController after the user is finished working with it, and return back to the calling view controller in the main application.
Is there any reason why my screen is black? How I can resolve this issue?
Don't use removeFromSuperview, use [self dismissViewControllerAnimated:YES completion:nil]; Just like you use a pop to undo a push, you use dismissViewController to undo a presentViewController. The reason you get a black screen is because presenting a view controller removes the view of the presenting view controller from the window's hierarchy. So, when you remove the view from the superview, there's nothing underneath but the window.
Let me explain. I have multiple UIViewControllers. On my MainPageController, I have 3 UIViews. Let's enumerate it this way: the first UIView is called LoginView, the second is called HomeView and the other one is called RegView. Now in HomeView, there are multiple buttons that will lead to other UIViewControllers. For example, one button will lead to StoreController. Now if I am inside StoreController and I want to go back to MainPageController, I simply call:
[self dismissModalViewControllerAnimated:YES completion:nil]
This will send me back to the HomeView.
That is good. However, inside the StoreController, there are buttons which will supposedly direct me to LoginView or RegView, whichever button was tapped. The problem is when the method [self dismissModalViewControllerAnimated:YES completion:nil], it only take me back to HomeView, no matter which button I pressed.
So how will I display the right UIView once the dismissModalViewControllerAnimated is called?
EDIT:
This is how I show the UIViews:
-(void)viewDidLoad
{
//Initialize the views here...
}
-(void)showViewByTag:(NSInteger)tag
{
if (tag == 1)
{
[self.view addSubview:loginView];
}
else if (tag == 2)
{
[self.view addSubview:homeView];
}
else
{
[self.view addSubview:regView];
}
}
Now I call the method showViewByTag: somewhere in my code to display the views.
What you could try and do is following: before calling [self dismissModalViewControllerAnimated:YES completion:nil] (and thus go back to your home view), change the view currently displayed in your MainPageController:
[(MainPageController*)self.presentingViewController showViewByTag:desiredViewTag];
[self dismissModalViewControllerAnimated:YES...];
If you are worried at the cast and you foresee that self.presentingViewController might be not of MainPageController type on some occasions, then you can check explicitly for its type:
if ([self.presentingViewController isKindOf:[MainPageController class]])
[(MainPageController*)self.presentingViewController showViewByTag:desiredViewTag];
[self dismissModalViewControllerAnimated:YES...];
For this to compile, MainPageController.h must be imported in your modal controller class.
dismissModalViewController will always bring back the viewController which presented it ,and that can be only one,so the ideal way would be to tell the navigationController to initWith your desired viewController..
eg on regButton click in the presented modalview
RegViewController *regViewController = [[RegViewController alloc]initWithNibNam:#"RegViewController" bundle:nil];
[self.navigationController initWithRootViewController:regViewController];
I haven't found anything corresponding to my situation so far...
FYI, I'm developing for iOS 5, using a storyboard.
I have a tab bar controller with 2 views in it (let's call them tab 1 and tab 2). I also have a separate landscape view, with no tab bar, which is used any time the device rotates during the application use. I use a segue launched manually in shouldAutorotateToInterfaceOrientation to switch to and from this view. I also use a NSString in the landscape view to know which tab I am coming from, to go back to correct one when I go back to portrait. So far, this works fine. I can go to and from landscape mode exactly the way I want.
My problem is :
When I launch the app, in portrait, I see the tab bar. If I go to landscape, it disappears. This is fine, that's what I did in my storyboard. But when I go back to portrait, the tab bar does not come back ! That's the problem.
Edit : code calling the rotation
I stopped using shouldAutorotateToInterfaceOrientation to rotate because it was conflicting with the custom segues. The problem with the tab bar was here before, so this is not the issue. I use didRotate instead.
Here is the code from FirstViewController.m (same code in SecondViewController.m, changing my segue identifier) :
-(void)didRotate:(NSNotification *)notification
{
UIInterfaceOrientation newOrientation = [[UIDevice currentDevice] orientation];
if ((newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight))
{
[self performSegueWithIdentifier: #"Page1ToLandscapeSegue" sender: self];
}
}
And from LandscapeViewController.m (previousView is a NSString, set before going to landscape, so I know which view I'm coming from) :
-(void)didRotate:(NSNotification *)notification
{
UIInterfaceOrientation newOrientation = [[UIDevice currentDevice] orientation];
if (newOrientation == UIInterfaceOrientationPortrait)
{
if ([previousView isEqualToString: #"View1"]) {
[self performSegueWithIdentifier: #"LandscapeToPage1Segue"
sender: self];
}
else if ([previousView isEqualToString: #"View2"]) {
[self performSegueWithIdentifier: #"LandscapeToPage2Segue"
sender: self];
}
}
}
Looking at your comments I'm thinking that your tab bar is disappearing because you're seguing from a view controller that is not embedded in a tab bar controller (this being your landscape view view controller), I'd suggest the following:
1) It seems complicated to setup segues to go back to the previous view, not to mention that you're creating more views/controllers and adding them to the stack, so discard the segues that go back to your original views.
2) Make the segues to the landscape view be modal, that way you won't have the tab bar show up when you segue to them, if you use push it will be embedded in the tab bar controller.
3) Since the landscape view would be a modal view, simply call this method in your rotate code in the landscapes view controller:
[[self presentingViewController] dismissModalViewControllerAnimated:YES];
This will push the view off the stack and go back to the view it came from.