Not working Orientation Notifications in VIewController under modal, with iOS5 - orientation

Excuse my English.. :)
In my viewController (A) there is a orientation notifier like this:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{ UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
if (newOrientation == UIDeviceOrientationUnknown || newOrientation == UIDeviceOrientationFaceUp || newOrientation == UIDeviceOrientationFaceDown) {
UIInterfaceOrientation mainOrientation = [[UIApplication sharedApplication] statusBarOrientation];
newOrientation = (UIDeviceOrientation) mainOrientation;
}
This is working properly when this is the controller.
There is a ModalViewController (B) that sometimes appear, and this could be reoriented to new orientation, and do it perfect. BUT, when this modal is dismissed, the main view controller remains in the same orientation it was before.
Controlling by breakpoints I´ve checked that the code in the method above is not being executed.
All this has been working perfectly before I actualized IOS5 SDK.
(I´ve not refactoriced to ARC, because of CGPLOT issues).
Any one could help me, or has suffered same issue?
The obvious solution is to call this method from the modal view... but it´s annoying..:(
Thanks you very much and have a nice day!

Send a notification using
[[NSNotificationCenter defaultCenter] postNotificationName:#"changeOrientation" object:nil];
from view where didRotate... worked and in other class use:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doWork) name:#"changeOrientation" object:nil];
to catch this event.
Лайтбрингер с ЕГ-форума?

Related

UIViewController device rotation delegate methods are not getting called in iOS8

I have been working an iOS 7 app to make it compatible for ios 8 (beta 5). In this application, UIViewController (vc1) presents another UIViewController (vc2). vc1 supports both Portrait and Landscape orientations; vc2 supports only Portrait orientation. When vc2 is presented, it asks vc1: shouldAutorotateToInterfaceOrientation: and this returns YES.
In iOS8 (Beta 5) willRotateToInterfaceOrientation: and didRotateFromInterfaceOrientation: are not getting called as well as the new iOS 8 API method viewWillTransitionToSize. But, this works fine in iOS7.
I know willAnimateRotationToInterfaceOrientation and didRotateFromInterfaceOrientation are deprecated in iOS 8, but even iOS 8 delegate methods are not getting called. Every time when launched vc2 from vc1 always screens loads in portrait mode only even though I mentioned supported interface orientation as landscape left.
Any ideas... is it a bug in iOS8?
Well, I didn't figure out your problem best but as soon I have a bunch of lines working fine with rotation in iOS 8.1 I will present them to you. They are just taken and a little bit of edited from the Apple API Reference.
Simply I put this in every VC and i just edit the code when needed. For example I have an app that have initial view controller in portrait and then VC changes ( segue is done ) to a LandscapeVC with different features.
This is the portrait view methods leading to a rotation in LandscapeView.
bool isShowingLandscapeView = false;
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
isShowingLandscapeView = YES;
[self performSegueWithIdentifier:#"toLandscape" sender:self];
}
I hope I made it simple for understanding. Don't hesitate to improve my answer, we all learn in this life !

Change interface on orientation programmatically

In my iOS app, the interface is created by programmatically adding multiple UIViews to the UIViewController that is currently on the screen. Currently the app works in the portrait orientation, but I want to add the ability to work in landscape. I want to change the interface when the app is put in the landscape orientation. Hoe can I do this?
P.S. I can't use the interface builder
You need to watch for the orientation to change via the NSNotificationCenter then handle it.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Then create the method to handle the rotation
- (void)orientationChanged:(NSNotification *)notification
{
// Get device orientation
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsPortrait(deviceOrientation))
{
// Portrait
}
else if(UIDeviceOrientationIsLandscape(deviceOrientation))
{
// Landscape
}
}

When using different view controllers (portrait and landscape) to handle orientation changes, is a segue the most appropriate way to handle this?

I have been looking into ways of setting up separate landscape and portrait view controllers to handle a changing orientation. The code posted below is from Apple stating how to do this. I noticed they use performSegueWithIdentifier. It seems odd that a segue is being used.
In order to create a segue on the storyboard I'm assuming I must create a hidden button and drag the connection from the portrait to the landscape view controller. I can then set the segue identifier to "DisplayAlternateView". What is the default segue animation? Or is the default to turn the animation off?
Also why is this code in the awakeFromNib method? Shouldn't it be in viewDidLoad? Is awakeFromNib called before viewDidLoad?
Also I'm assuming I must have a different target action for every scene of my storyboard. If I have portrait view A, B and C with a corresponding landscape view A, B and C, should I have the following changes to the Apple code
on my A view:
selector:#selector(orientationChangedA:)
then on my B
selector:#selector(orientationChangedB:)
then on my C
selector:#selector(orientationChangedC:)
This way each method can perform it's own segue.
I feel like I might be over complicating things here. Are the separate segues causing me to do extra work or is this how how orientation switching to separate view controllers normally handled?
Here is the code from Apple saying how to handle orientation changes with different view controllers:
#implementation PortraitViewController
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}

Modal view controller not presented anymore after being presented 2/3 times

Here is the thing, I have to integrate an augmented reality functionality into an app. After testing it aside, now I'm ready to integrate it. The app always runs in Portrait, I decided to show the augmented reality when rotating the iPhone in Landscape Right or Left (and of course, if I go back in Portrait the original view controller is shown). In fact the augmented reality is presented modally.
I've got viewController calling ARViewController (modally). It works fine if I rotate like 2 or 3 times, but then this ARViewController is not called anymore, but the app is still running, no crash, no freeze. This ARViewController is initialised with ARController, a class computing all needed for the augmented reality. If I call this ARViewcontroller without the ARController, switching between the view controllers works very fine, a blank window will be called but at least I can rotate the device as much as I want. So, this must come from the ARController, I documented myself on memory leaks (by the way I'm using ARC), I think this could be the reason to the issue since I'm using this ARController many times.
But before going further, I'd like to know if I'm doing anything wrong that could influence the ARController by switching between view controllers:
Here is how I call the ARViewController in viewController:
if (orientation == UIDeviceOrientationLandscapeLeft) {
NSLog(#"ViewController Landscape left");
ARViewController *arVC = [[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil];
[self setCameraViewController:arVC];
[arVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
[[self navigationController] presentModalViewController:cameraViewController animated:YES];
arVC = nil;
}
else if (orientation == UIDeviceOrientationLandscapeRight) {
NSLog(#"ViewController Landscape Right");
ARViewController *arVC = [[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil];
[self setCameraViewController:arVC];
[arVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
[[self navigationController] presentModalViewController:cameraViewController animated:YES];
arVC = nil;
}
the initialisation of ARViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.arController = [[ARController alloc] initWithViewController:self];
arController.filterDiscover = filterDiscover;
arController.filterEat = filterEat;
arController.filterSleep = filterSleep;
// Listen for changes in device orientation
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//if ViewController presents this modal ARViewController
if(!arController.filterDiscover && !arController.filterEat && !arController.filterSleep)
[arController callAlertViewToFilter];
else
[arController loadData];
}
And finally here is how I go back to the original view controller if I rotate to Portrait in ARViewController:
if (orientation == UIDeviceOrientationPortrait){
NSLog(#"ARViewController Portrait");
[self setArController:nil];
[[super presentingViewController] dismissModalViewControllerAnimated:YES];
}
I tried to be as clear as possible, if anyone has ever had an issue similar to this, it could be great have some clues to solve this. I could have shown the code of the ARController but it is a little too long, for now I'd just like to know if there is anything wrong here. If needed I'll show it.
This might help, I found this output in the debug area when the ARViewController is not being displayed anymore:
2012-10-24 17:57:51.072 beiceland[20348:907] ViewController Landscape Right
2012-10-24 17:57:51.073 beiceland[20348:907] Warning: Attempt to present <ARViewController: 0x203f0c60> on <RevealController: 0x1cd5dca0> while a presentation is in progress!
My bad I was way out of the solution here.
I used the debugger and breakpoints and repeated the critical sequence, I found out I was not entering:
- (void)deviceOrientationDidChange:(NSNotification *)notification
anymore. So this is the first time I see such things, the listener for device orientation changes suddenly stops firing. Consequently my solution is quite brutal but at least it has the merit of stopping the issue, just after detecting the device orientation change I stop the listener:
if (orientation == UIDeviceOrientationLandscapeLeft) {
NSLog(#"ViewController Landscape left");
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
ARViewController *arVC = [[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil];
[self setCameraViewController:arVC];
arVC = nil;
[cameraViewController setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
[[super navigationController] presentModalViewController:cameraViewController animated:YES];
}
And each time I'm using the viewController (the calling view controller), I reinitialize the listener, it means in viewDidAppear:
// Listen for changes in device orientation
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Well now it's solved but I have to admit I'm disappointed of finding such kind of solution.
Well, I never used ARC before, but according to what I see, you initialize a ARViewController everytime you change orientation ( on landscape ). Why not instantiating 2 ARViewController for good, and calling them everytime ?
Except if you are sure that these are released everytime you switch to portrait.
Plus, why don't you just use pushViewController:animated: ?
And one last thing, you presentModalViewController to you navigationController, but you dismiss it in [super presentingViewController], maybe you could add this one ?

Navigation Bar and Tab Bar when rotating

I have four controllers for the tab bar and it's views, when I rotate phone I want it to call the method shouldAutorotateToInterfaceOrientation for only one of the controllers but it calls the method for all of them.
How can I call shouldAutorotateToInterfaceOrientation for only one of the controllers?
You don't "call" these methods, those are methods that are being called for any class that inherits from UIViewController.
There is no reason to want them not to be called anyway.
What you can do is decide to override any of these methods for any of your controller.
The method returns a boolean, if you return true, then the view is being rotated, if you return false, then it is not.
You can also decide to return YES/NO based on the orientation, in order to support one specific orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
In this case, the view is being rotated only if the new orientation is portrait.
Thank you JP Hribovsek, I solved it using beginGeneratingDeviceOrientationNotifications that notifies me when the device´s orientation is changed. For example:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(deviceRotate:)
name: UIDeviceOrientationDidChangeNotification
object: nil];
-(void) deviceRotate: (NSNotification*) notification
{
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSlog(#"Landscape");
}
}

Resources