This is my viewController.m file
- (IBAction)defaultAction:(id)sender {
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
[self presentViewController:simpleView animated:YES completion:nil];
}
- (IBAction)flipAction:(id)sender {
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
[simpleView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:simpleView animated:YES completion:nil];
}
- (IBAction)dissolveAction:(id)sender {
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
[simpleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:simpleView animated:YES completion:nil];
}
- (IBAction)pageCurlAction:(id)sender {
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
[simpleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentViewController:simpleView animated:YES completion:nil];
}
I have another class SimpleViewController.There is a button action like this in this class.
- (IBAction)dismissMeAction:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
My default, flip and dissolve button works well but when i click pageCurl my apps crashes. What is the reason behind this?
The UIModalTransitionStylePartialCurl is a style that requires additional special configuration. Here is from Apple's doc:
Declaration
UIModalTransitionStylePartialCurl
Discussion
When the view controller is presented, one corner of the current view
curls up to reveal the presented view underneath. On dismissal, the
curled up page unfurls itself back on top of the presented view. A
view controller presented using this transition is itself prevented
from presenting any additional view controllers. This transition style
is supported only if the parent view controller is presenting a
full-screen view and you use the UIModalPresentationFullScreen modal
presentation style. Attempting to use a different form factor for the
parent view or a different presentation style triggers an exception.
so you have to make sure your self view is a full-screen and add full-screen presentation style to simpleView as below
- (IBAction)pageCurlAction:(id)sender {
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
[simpleView setModalPresentationStyle:UIModalPresentationFullScreen];
[simpleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentViewController:simpleView animated:YES completion:nil];
}
Related
I am trying to switch views in my application. I have the following code to take me from the Main View to the first level view:
-(IBAction)levelOneButton
{
levelOneView *testView = [[levelOneView alloc] initWithNibName:nil bundle:nil];
testView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:testView animated:YES completion:NULL];
}
Which works GREAT! Then the Code to take me to the next level is the exact same but slightly different!
-(IBAction)nextLevelButton
{
LevelTwoView *levelTwo = [[LevelTwoView alloc] init];
levelTwo.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:levelTwo animated:YES completion:nil];
}
Now when I want to go back to the Main menu from here is where the issue occurs. I have the following code which only dismisses the level2 and shows level1 again.
- (IBAction)goBackButton {
[self dismissViewControllerAnimated:YES completion:NULL];
}
Like I said when this code is used it just releases the current view and takes you back to the last view instead of taking you back to the main menu. After realizing it was not working I tried the following code to take me back to the main menu.
- (IBAction)goBackButton {
ViewController *mainMenu = [[ViewController alloc] init];
mainMenu.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:mainMenu animated:YES completion:nil];
}
When this code is run, I get presented with a black screen.
My question remains how may I go back to the view ViewController? Thank you so much!!
Try this when dismissing the second level view controller.
[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES]
I am trying to have it so a user can choose a photo from their album into an imageview. It seems to work fine if I'm using a push segue to that viewcontroller that has the button to perform the action, but when I change it to modal segue, nothing happens when I click the button to run the function. What is the cause of this exactly?
Method:
- (IBAction)choosePhotoFromAlbum:(id)sender {
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.navigationController presentViewController:imgPicker animated:YES completion:nil];
}
You're trying to present it from the navigation controller, but your view controller is modal. Change
[self.navigationController presentViewController:imgPicker animated:YES completion:nil];
to
[self presentViewController:imgPicker animated:YES completion:nil];.
you're trying to present it from the navigation controller, but your view controller is modal. so just write
[self presentViewController:YourImagePickerController animated:YES completion:nil];.
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
}];
}
This only accours if you are presenting in a view controller that is managed by a navigation controller.
The reproduction steps are:
1 - Present a view controller using UIModalPresentationCurrentContext
self.definesPresentationContext = YES;
ViewController* viewController = [[ViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentOnViewController presentViewController:viewController animated:YES completion:nil];
2 - Present a view controller over the top using the default full screen presentation style
ViewController* viewController = [[ViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
3 - Dismiss the top presented view controller (the full screen one)
[self dismissViewControllerAnimated:YES completion:nil];
Now the problem is the 2nd view controller (presented using UIModalPresentationCurrentContext) disappears. Also it is impossible to present another view controller using UIModalPresentationCurrentContext, because the system thinks its still there.
I believe the issue is a bug in the framework. As mentioned it only occurs when the presenting in a view controller managed by a navigation controller. There is a nasty work around which uses the containment API. It creates a dummy view controller which views are presented from. The steps are:
1 - When presenting a view in context who's parent is a navigation controller, use a dummy view controller:
- (void)presentInContext
{
UIViewController* presentOnViewController = self;
if ([self.parentViewController isKindOfClass:[UINavigationController class]])
{
// Work around - Create an invisible view controller
presentOnViewController = [[DummyViewController alloc] init];
presentOnViewController.view.frame = self.view.frame;
// Containment API
[self addChildViewController:presentOnViewController];
[self.view addSubview:presentOnViewController.view];
[presentOnViewController didMoveToParentViewController:self];
presentOnViewController.definesPresentationContext = YES;
}
ViewController* viewController = [[ViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentOnViewController presentViewController:viewController animated:YES completion:nil];
}
2 - When dismissing the view controller tidy up
- (void)dismissSelf
{
__weak UIViewController* presentingViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
// Remove the dummy view controller
if ([presentingViewController isKindOfClass:[DummyViewController class]])
{
[presentingViewController willMoveToParentViewController:nil];
[presentingViewController.view removeFromSuperview];
[presentingViewController removeFromParentViewController];
}
}];
}
Thats it... The fix is dirty, but does the trick with no visual flicker.
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