dismissModalViewControllerAnimated deprecated
The new method is:
[self dismissViewControllerAnimated:NO completion:nil];
The word modal has been removed; As it has been for the presenting API call:
[self presentViewController:vc animated:NO completion:nil];
The reasons were discussed in the 2012 WWDC Session 236 - The Evolution of View Controllers on iOS Video. Essentially, view controllers presented by this API are no longer always modal, and since they were adding a completion handler it was a good time to rename it.
In response to comment from Marc:
What's the best way to support all devices 4.3 and above? The new
method doesn't work in iOS4, yet the old method is deprecated in iOS6.
I realize that this is almost a separate question, but I think it's worth a mention since not everyone has the money to upgrade all their devices every 3 years so many of us have some older (pre 5.0) devices. Still, as much as it pains me to say it, you need to consider if it is worth targeting below 5.0. There are many new and cool APIs not available below 5.0. And Apple is continually making it harder to target them; armv6 support is dropped from Xcode 4.5, for example.
To target below 5.0 (as long as the completion block is nil) just use the handy respondsToSelector
: method.
if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){
[self presentViewController:test animated:YES completion:nil];
} else {
[self presentModalViewController:test animated:YES];
}
In response to another comment from Marc:
That could be quite a lot of If statements in my application!...I was
thinking of creating a category that encapsulated this code, would
creating a category on UIViewControler get me rejected?
and one from Full Decent:
...is there a way to manually cause that to not present a compiler warning?
Firstly, no, creating a category on UIViewController
in and of itself will not get your app rejected; unless that category method called private APIs or something similar.
A category method is an exceedingly good place for such code. Also, since there would be only one call to the deprecated API, there would be only one compiler warning.
To address Full Decent's comment(question), yes you can suppress compiler warnings manually. Here is a link to an answer on SO on that very subject. A category method is also a great place to suppress a compiler warning, since you're only suppressing the warning in one place. You certainly don't want to go around silencing the compiler willy-nilly.
If I was to write a simple category method for this it might be something like this:
@implementation UIViewController (NJ_ModalPresentation)
-(void)nj_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion{
NSAssert(completion == nil, @"You called %@ with a non-nil completion. Don't do that!",NSStringFromSelector(_cmd));
if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){
[self presentViewController:viewControllerToPresent animated:flag completion:completion];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[self presentModalViewController:viewControllerToPresent animated:flag];
#pragma clang diagnostic pop
}
}
@end
dismissModalViewControllerAnimated is deprecated
The new method is:
[self.navController dismissViewControllerAnimated:NO
completion:nil];
The word modal has been removed
presentModalViewController:Animated is deprecated in ios6
Use this line & check:
[self presentViewController:imagePicker animated:YES completion:nil];
Dismiss multiple ModalViewControllers iOS
So the easy way is to get to your A controller, which is not parent-child relationship in this case, but a presenting/presented relationship:
[self.presentingViewController.presentingViewController dismiss...]
^C ^B ^A
But, this is not very clean and will get you when you change your controller hierarchy, so I would advice setting a delegate to notify when it's ok to dismiss controllers.
UIAlertView is deprecated: first deprecated in iOS 9.0
You're getting these warnings/errors because those methods have been removed from the code base. I'm guessing you're trying to follow along with an old tutorial.
You should also post more of your code. What you have shown us is not where your warnings/errors are.
For the dismissModalViewControllerAnimated
use this instead.
[self dismissViewControllerAnimated:YES completion:nil];
For presentModalViewController:animated
use this.
[self presentViewController:newController animated:YES completion:nil];
Finally, for your UIAlertView you should now be using a UIAlertController:
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"title"
message:@"some message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
Related Topics
In iOS 12, When Does the Uicollectionview Layout Cells, Use Autolayout in Nib
Writing Handler for Uialertaction
How to Add Images for Different Screen Size from Assets.Xcassets in Xcode 8
Dtassetproviderservice Could Not Start Dtxconnection with Simulator
How to Determine Height of Uicollectionview with Flowlayout
What's the Difference Between "Architectures" and "Valid Architectures" in Xcode Build Settings
How to Implement Uipageviewcontroller That Utilizes Multiple Viewcontrollers
Replacement for Stringbyaddingpercentescapesusingencoding in iOS9
Swift3 iOS - How to Make Uitapgesturerecognizer Trigger Function
Force Landscape Mode in One Viewcontroller Using Swift
Uitableview with Static Cells Does Not Appear
Optional Binding with Try? and As? Still Produces an Optional Type
How to Pass Data from Modal View Controller Back When Dismissed
How to Give an "Identifier" to a View Controller Within My Storyboard
iPhone Core Data "Automatic Lightweight Migration"