UIAlertController if iOS 8, otherwise UIAlertView

后端 未结 11 2039
清酒与你
清酒与你 2020-12-16 10:19

I want to conform to the UIAlertController used in iOS 8 since UIAlertView is now deprecated. Is there a way that I can use this without breaking support for iOS 7? Is there

11条回答
  •  难免孤独
    2020-12-16 10:50

    As others have already mentioned - always check whether a feature exists. I believe the safest approach is following:

    if (NSClassFromString(@"UIAlertController")) {
        // use UIAlertController
    } else {
        // use UIAlertView
    }
    

    With the obvious risk of entering a class name with a typo. :)

    From documentation of NClassFromString:

    [Returns] The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil.

    Availability iOS (2.0 and later)

提交回复
热议问题