iOS 6 force an orientation - ios

In iOS5 you could use this snippet to force the orientation:
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];
However this causes an EXC_BAD_ACCESS in iOS6. How can a certain orientation be forced in iOS6?

Just to complete the previous answer, you should do this:
UIViewController *viewController = [[UIViewController alloc] init];
[self presentViewController:viewController animated:NO completion:^{
[viewController dismissModalViewControllerAnimated:NO];
}];
And iOS 6 is no longer under NDA.

In case anyone still cares about this, here's the iOS6 code snippet (I placed it in my viewDidLoad routine):
UIViewController *viewController = [[UIViewController alloc] init];
viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:viewController animated:NO completion:^{
[self dismissViewControllerAnimated:NO completion:nil];
}];

At first presentModalViewController and dismissModalViewControllerAnimated are deprecated and probably iOS6 will not use these methods correctly. You should use similar methods with complition block instead.
The second thing is that [self dismissModalViewControllerAnimated:NO]; tries to dismiss self firstly. Is this correct in your case?
And last thing iOS6 is under NDA

Related

dismissViewControllerAnimated not being called on iPhone

I have the following code where I am trying to show a PDF preview. It words perfectly on an iPad however when I am trying to do it on a iPhone it dosnt work.
QLPreviewController* preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:preview animated:YES completion:nil];
}];
The thread on the iPhone never makes it to this line
[self presentViewController:preview animated:YES completion:nil];
but works fine on ipad.. I am not sure what to even look at. Any help would be appreaciated.
To access the instances/variables (that are declared outside of the block) inside a block, you need to declare those instances/variables like this:
__block type identifier = initial value (optional) e.g, in your case use
__block QLPreviewController* preview = [[QLPreviewController alloc] init];
Try to use
[self.presentingViewController presentViewController:preview animated:YES completion:nil];
instead of
[self presentViewController:preview animated:YES completion:nil];

xcode changing view controllers issues

I have been developing an iPhone app and have come across a few issues. I do not have a storyboard in my app and have nothing in my xibs. I have initialised and set everything up through code. When I go to the GameViewController from my main viewcontroller everything is fine, however when I come back through my back button I get this issue:
Presenting view controllers on detached view controllers is discouraged .
When I re-arrive to the main view controller, there are little changes such as the view changing before its supposed to. Here is the code for the button on my
GameViewController *game = [[GameViewController alloc] initWithNibName:nil bundle:Nil];
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:game animated:YES completion:NULL];
Here is the code for the back button:
ViewController *home = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:home animated:NO completion:NULL];
If anyone can help me to see what I am doing wrong that would be great.
You can not present home view controller from self because it is already dismissed. You should change
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:home animated:NO completion:NULL];
to
UIViewController *parentViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^
{
[parentViewController presentViewController:home animated:NO completion:nil];
}];

Muiltple Modal UIViewControllers | Dismiss Top Modal UIViewController Only

My UIViewController stack looks as follows:
+------ UIViewController_C (presented)
+---- UIViewController_B (presented)
+-- UIViewController_A (pushed)
When I call -dismissViewController:animated on UIViewController_C, UINavigationController dismisses both UIViewController_C and UIViewController_B together, as per the docs with animation on _C, and none on _B.
What is the most compliant way to dismiss _C only?
try as below
after pushing to UIViewController_A present UIViewController_B as below code.
UIViewController_B *bbp=[[UIViewController_B alloc]initWithNibName:#"UIViewController_B" bundle:nil];
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:bbp];
passcodeNavigationController.navigationBar.hidden=YES;
[self.navigationController presentModalViewController:passcodeNavigationController animated:YES];
[passcodeNavigationController release];
now from UIViewController_B try to present in UIViewController_C as below code.
UIViewController_C *bbp=[[UIViewController_C alloc]initWithNibName:#"UIViewController_C" bundle:nil];
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:bbp];
passcodeNavigationController.navigationBar.hidden=YES;
[self.navigationController presentModalViewController:passcodeNavigationController animated:YES];
[passcodeNavigationController release];
last and final thing on every back button of view controller write below line of code.
[self dismissModalViewControllerAnimated:YES];
if you want more help than comment bellow.
One Solution:
UIViewControllers presented modally are not necessarily deallocated on -dismissViewController:animated.
This means that by passing a reference to UIViewController_A through _B to _C, you can call -presentViewController:animated and -dismissViewController:animated for the respective UIViewControllers via UIViewController_A.
Code:
1. UIViewController_B
- (void) showUIViewController_C {
[self dismissViewControllerAnimated:TRUE completion:^{
UIViewController_C *controller_C = [[UIViewController_C alloc] init];
controller_C.parentController = self;
[self.parentController controller_C animated:TRUE completion:nil];
}];
}
2. UIViewController_C
- (void) dismissUIViewController_C {
[self dismissViewControllerAnimated:TRUE completion:^{
[self.parentController.parentController presentViewController:self.parentController animated:TRUE completion:nil];
}];
}
Where I am using *parentController as the naming convention for whatever class your previous UIViewController on the stack may be.
It dips back to UIViewController_A briefly because I am calling -dismiss and -present in the completion block, though that actually looks rather fun.

Setting transition to partial curl programmatically/code

In StoryBoard we can set it visually as below
How can we do the same thing using .Xib/nib file ?
[self presentViewController:self.infoController animated:YES completion:nil];
Above code is just using modal style but how to set transition style to Partial Curl.
You can do this using this code:
#import "InfoController.h"
- (IBAction)goToSecond:(id)sender {
InfoController *vc = [[InfoController alloc] initWithNibName:#"InfoController" bundle:nil];
vc.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:vc animated:YES completion:^{
//completion code here
}];
}

Presenting ModalViewController Modally on iPad

Im calling this code from the MasterViewController in a UISplitVC for an iPad app:
-(void)viewWillAppear:(BOOL)animated{
//PRESENT MODALVC
ModalViewController *modalVC = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[self setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:modalVC animated:YES];
}
but it doesn't work. No ModalVC appears.
Try this code:
ModalViewController *modalVC = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[modalVC setModalPresentationStyle:UIModalPresentationFullScreen]; //You set the presentation style of the controller that would be presented, not the presenting controller
//This check is needed, because presentModalViewController:animated is depreciated in iOS5.0 and presentViewController:animated:completion must be used instead. The same is valid for dismissModalViewControllerAnimated and dismissViewControllerAnimated:completion
if([self respondsToSelector:#selector(presentViewController:animated:completion:)])
[self presentViewController:modalVC animated:YES completion:nil];
else
[self presentModalViewController:modalVC animated:YES];
If you are targeting iOS5.0+ only this check is not needed and you should use only presentViewController:animated:completion and dismissViewControllerAnimated:completion

Resources