UIAlertController subclass issue swift

前端 未结 2 968
终归单人心
终归单人心 2021-01-06 06:28

I want to create a UIAlertController\'s subclass but I\'m going crazy because I have problem with constructor, this is my subclass:

class loginAlert :  UIAle         


        
相关标签:
2条回答
  • 2021-01-06 07:19

    function that create a UIAlertController outside a class

    public  func alert () {
        let alert = UIAlertController(title: "Title", message: "I don't feel creative", preferredStyle: UIAlertControllerStyle.Alert)
        // As many action as you want
        let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)
        alert.addAction(action)
        self.presentViewController(alert, animated: true, completion: nil)
    }
    

    but as beyowulf said you can use an extension if you want to add method to UIAlertController

    0 讨论(0)
  • 2021-01-06 07:22

    From the UIAlertController Class Reference:

    Subclassing Notes The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

    You can create a view controller whose view contains transparency and whose UIModalPresentationStyle is .OverCurrentContext and UIModalTransitionStyle is .CrossDissolve for a very similar effect.

    Or you can write an extension on UIAlertController that can add methods that need to be shared across classes (e.g. a method that presents a reoccurring alert). For more information about extensions, see here.

    0 讨论(0)
提交回复
热议问题