Do you need help on a specific subject? Use the contact form (Request a blog entry) on the right hand side.

2016-04-27

Replacing UIAlertView with UIAlertController

Now that I am taking a short break from OS-X programming to do some iOS development one of the first things that I encounter is the replacement of UIAlertView by UIAlertController.

Not to difficult, and I actually like the new way much better than the old.

This was the old code (still in Obj-C):

    UIAlertView *av = [[[UIAlertView alloc]
        initWithTitle:@"Error"
        message:@"Oopsie daisy"
        delegate:nil
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil] autorelease];

    [av show];

This is the new code (in Swift of course):

    let alert = UIAlertController.init(
        title: "Error",
        message: "Oopsie daisy",
        preferredStyle: UIAlertControllerStyle.Alert)
      
    alert.addAction(UIAlertAction(
        title: "OK",
        style: UIAlertActionStyle.Default,
        handler: nil))

    
    presentViewController(alert, animated: true, completion: nil)

I specifically like the way action items are added to the alert view. Makes a lot of sense, and we can associate a handler (closure) with each action. It is no longer necessary to demultiplex in a single action handler.

Happy coding...

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.

No comments:

Post a Comment