IOS Uialertcontroller Simple Use method

Source: Internet
Author: User

First of all, Uialertview and Uiactionsheet in iOS9 after the official Apple is not recommended, but use Uialertcontroller to replace.
There are pictures to testify


So it is necessary to learn how to use Uialertcontroller.

1. Get the Uialertcontroller class object:

Uialertcontroller *alertcontroller =[uialertcontroller alertcontrollerwithtitle:@ "Success" Message:nil Preferredstyle:uialertcontrollerstylealert];

2. Get the Uialertaction class object:


Uialertaction *doneaction = [uialertaction actionwithtitle:@ "Done" Style:uialertactionstyledefault handler:^ ( Uialertaction *action) {

NSLog (@ "123");
}];

3. Add Uialertaction class objects using the Addaction method of the Uialertcontroller class object Alertcontroller:

[Alertcontroller addaction:doneaction];

4. Using window's root view mode out Alertcontroller


[[UIApplication Sharedapplication].keywindow.rootviewcontroller Presentviewcontroller:alertcontroller Animated: YES Completion:nil];

If you want to use a similar interface with Uiactionsheet, just replace the Uialertcontrollerstylealert in the first step with Uialertcontrollerstyleactionsheet.

If the program uses this API, it will cause crash on the iOS7, so version compatibility is required


Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@ "title" message:@ "Message" PreferredStyle: Uialertcontrollerstylealert];

Uialertaction *cancel = [uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel Handler:nil];

Uialertaction *confirm = [uialertaction actionwithtitle:@ "OK" Style:uialertactionstyledefault handler:^ ( Uialertaction *action) {
[Mainviewdelegate dobackupwithtag:backup_statistics_tag_confirm];
}];

[Alert Addaction:cancel];
[Alert addaction:confirm];
[Self Presentviewcontroller:alert animated:yes completion:nil];

You can see that the biggest difference is that Uialertcontroller no longer uses the delegate method to trigger the callback, but directly to a block
delegate and block are not fundamentally different, just the way the callback is triggered, and the solution is "At some time in the future, call me" the question. Delegate is a bit more reusable, and after you create a delegate instance, you can set it to the delegate of multiple controls, reducing duplication. Block has the advantage of being more intuitive and easier to read, because the code is all in one place and you don't have to jump to read the code
but now that Apple's official use of block is getting more and more frequent, it may also represent a trend

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.