iOS - Change View with the device orientation - objective-c

I'm using iOS5 and storyboard. I have two view with two different identifier.
I want change the view when change the orientation of the device.
This is my code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
self.view = [self.storyboard instantiateViewControllerWithIdentifier:#"Landscape"];
}
else if(((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
self.view = [self.storyboard instantiateViewControllerWithIdentifier:#"Portrait"];
}
return YES;
}
But the app crash.
Which is the problem?
EDIT:
If I add two uiview and I put the two id to the uiview when I rotate the device It crash.
This is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void) orientationChanged:(id)object
{
portraitView = [self.storyboard instantiateViewControllerWithIdentifier:#"TermoregolatoreTop"];
landscapeView = [self.storyboard instantiateViewControllerWithIdentifier:#"Landscape"];
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portraitView;
}
else
{
self.view = self.landscapeView;
}
}

self.view = [self.storyboard instantiateViewControllerWithIdentifier:#"Portrait"];
Will return an id to a UIViewController and you are assigning it to a UIView.

You have to remove the observer on UIDeviceOrientationDidChangeNotification when releasing the view controller.
The crash happens because that notification is triggered on the class which most likely has been removed from memory.

Related

IPad Camera Orientation issue

I am implementing an Application Regarding to Camera Mode Functionality But App Run Only Landscape But the Camera Opens in Some part of the IPad Only Remaining is in Black.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
camPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
camPicker.delegate = self;
camPicker.allowsEditing = NO;
[self presentModalViewController:camPicker animated:YES];
For Orientations Fixed as
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
}else
{
return interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
Set orientation as:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

Warning: Attempt to present ViewController whose view is not present in the window hierarchy

I'm using Vserv complete sdk for addintegeration.I'm calling my adViewController to present my add.if the failed add fails I'm taking to a SplashViewController from my addViewController.But I'm getting this warning
Warning: Attempt to present <SplashViewController: 0x1dd697b0> on <AdViewController: 0x1dd68690>
whose view is not in the window hierarchy! and not able to navigate to my next view that is SplashViewController..I've tried all the methods below is the code.Could u guys help me out:
AdViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
// Do any additional setup after loading the view from its nib.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if([Util isInternetAvailable])
{
//Initializes SDK
[VservAdManager initializeSDK];
CGRect frame1;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
frame1 = CGRectMake(0,0,1024,768);
else
frame1 = CGRectMake(0,0,320,480);
vservAdView = [[VservAdView alloc] initWithFrame:frame1];
[vservAdView requestAd:self:#"7825":nil];
[self.view addSubview: vservAdView];
}
else{
appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.window.rootViewController = appDelegate.splashViewController;
[self presentModalViewController:appDelegate.splashViewController animated:NO];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
else {
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
}
- (void)adReceivedNotification:(VservAdView*)vservAd{
NSLog(#"adReceivedNotification");
}
- (void)adFailedNotification:(VservAdView*)vservAd{
NSLog(#"adFailedNotification");
appDelegate.window.rootViewController = appDelegate.splashViewController;
[self presentViewController:appDelegate.splashViewController animated:NO completion:Nil];
}
- (void)adSkipedNotification:(VservAdView*)vservAd{
NSLog(#"adSkipedNotification");
appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.window.rootViewController = appDelegate.splashViewController;
[self presentModalViewController:appDelegate.splashViewController animated:NO];
}
SplashViewController.h
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self setupMovie];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
else {
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
}
-(void)setupMovie{
NSLog(#"setup Movie");
NSString* moviePath = [[NSBundle mainBundle] pathForResource:#"Splash" ofType:#"mp4"];
NSURL* movieURL = [NSURL fileURLWithPath:moviePath];
playerCtrl = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
playerCtrl.scalingMode = MPMovieScalingModeFill;
playerCtrl.controlStyle = MPMovieControlStyleNone;
// playerCtrl.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
[playerCtrl.view setFrame:CGRectMake(0, 0, 1024, 768)];
}
else{
[playerCtrl.view setFrame:CGRectMake(0, 120, 320, 240)];
}
// [playerCtrl.view setTransform:CGAffineTransformMakeRotation(-M_PI/2)];
[self.view addSubview:playerCtrl.view];
[playerCtrl setRepeatMode:MPMovieRepeatModeNone];
[playerCtrl play];
}
-(IBAction)moviePlayBackDidFinish:(id)sender{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[self presentModalViewController:appDelegate.revealController animated:NO];
appDelegate.window.rootViewController = appDelegate.revealController;
}
Why are you presenting view on self if you set the root view of windows to splashViewController?
appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.window.rootViewController = appDelegate.splashViewController;
[self presentModalViewController:appDelegate.splashViewController animated:NO];
instead use this
appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
//appDelegate.window.rootViewController = appDelegate.splashViewController;
[appDelegate.window presentModalViewController:appDelegate.splashViewController animated:NO];

Error in iOS 6 after adding GameCenter to a landscape-only cocos2d app

I'm having the issue described here:
https://devforums.apple.com/thread/165384?tstart=0
Where my app crashes trying to load the GameCenter login screen because the screen is in portrait and my app only supports landscape.
I've tried every solution described in the above thread, as well as all the solutions on the following thread:
Crash on presenting UIImagePickerController under iOS 6.0
And here:
http://www.cocos2d-iphone.org/forum/topic/36639
None of the solutions work. Either the crash still occurs, or the login works fine, and either my app then freely rotates between landscape and portrait, or it locks itself into portrait and screws up the entire UI.
What I want is for the GameCenter login to work in portrait, and then for everything else in the app to occur in landscape.
Here are all the rotation methods contained in my app. These are the ones from the myNavigationController implementation in appdelegate.m:
-(NSUInteger)supportedInterfaceOrientations {
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationMaskLandscape;
// iPad only
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
// iPad only
// iPhone only
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
-(BOOL)shouldAutorotate{
return NO;
}
And from the AppController implementation in appDelegate.m:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Contained in RootViewController.m:
-(BOOL)shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
#if GAME_AUTOROTATION==kGameAutorotationNone
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#elif GAME_AUTOROTATION==kGameAutorotationCCDirector
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
} else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
}
#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
#else
#error Unknown value in GAME_AUTOROTATION
#endif // GAME_AUTOROTATION
// Shold not happen
return NO;
}
kGameAutorotationUIViewController
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect = CGRectZero;
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
}
CCDirector *director = [CCDirector sharedDirector];
UIView *glView = [[CCDirector sharedDirector] view];;
float contentScaleFactor = [director contentScaleFactor];
if( contentScaleFactor != 1 ) {
rect.size.width *= contentScaleFactor;
rect.size.height *= contentScaleFactor;
}
glView.frame = rect;
}
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController
Just answered this question a few days ago here:
Cocos 2d 2.0 shouldAutorotate not working?
There are instructions on what you need to do to get this to work in that answer. Hope this helps!

Screen Orientation for Portrait and Landscape Orientation in iOS (Objective-C)

I have a situation where I want to call separate method for different Orientation (Portrait and Landscape).
For example:
If (Orientation == Portrait)
{
Some method a;
}
Elseif (Orientation == Landscape)
{
Some method b;
}
I use [[UIApplication sharedApplication] statusBarOrientation] to know the orientation.
then I use this method
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
//Do something if landscape
} else {
//Do something in portrait
}
don't use [[UIDevice currentDevice] orientation] because it doesn't work properly if the device is on a table, for example.
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}
UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait are the macro's.
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
if(deviceOrientation == UIDeviceOrientationPortrait)
...
else ...
the enum is UIDeviceOrientation[something]
TRY THIS:
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{// perform ua operation for landscape mode
} else{
//perform ua operation for portrait mode
};
Objective-c:
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
if (UIDevice.currentDevice.orientation == UIInterfaceOrientationPortrait) {
printf("Portrait");
} else {
printf("Landscape");
}
}

UISplitViewController: How to present popover?

I've a UISplitViewController which is a UISplitViewControllerDelegate with the following delegate method:
splitViewController:willHideViewController:withBarButtonItem:forPopoverController:
When the iPad is started in Portrait, I would like the Popover from the SplitView to be visible. How can I do that?
I've tried the following code:
- (void)splitViewController:(UISplitViewController *)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)pc
{
//setting the barButtonItem in the toolbar in the detail view.
[pc presentPopoverFromBarButtonItem:barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
But the above code gives me the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
there only one problem, wrong place to call presentPopover method, splitViewController:*WillHide*ViewController....... so, barButtonItem exist but not present on screen. i used next code and it worked for me.
For handle all cases u need to use 2 methods.
- (void)viewDidAppear:(BOOL)animated
{
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
if (self.view.window != nil) {
[_masterPopoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
}
}
[super viewDidAppear:animated];
}
and
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (fromInterfaceOrientation == UIDeviceOrientationLandscapeLeft || fromInterfaceOrientation == UIDeviceOrientationLandscapeRight) {
if (self.view.window != nil) {
[_masterPopoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
}
}
}