user1162328
user1162328

Reputation:

iOS Swift UIAlertView

I'm new to iOS app development, but I haven't been able to find a similar question.

Is it possible to have two or more unique alerts in the same view controller? I'm targeting from iOS 7.1, and therefore using the following deprecated UIAlertView method:

let errorAlert: UIAlertView = UIAlertView()
errorAlert.delegate = self
errorAlert.message = "Are you sure?"
errorAlert.addButtonWithTitle("Yes")
errorAlert.addButtonWithTitle("No")
errorAlert.show()

This alert goes to a function that holds some logic in a switch statement.

func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) { ...

So far all is OK, but when I create a second alert within the same view controller it also goes into the same function. For example if I fail to make a connection with a database I'll show a different error message, but this also goes to the above alertView function and runs through the same switch statement.

Is there an obvious error I'm doing?

Thanks in advance.

Upvotes: 4

Views: 761

Answers (3)

glyuck
glyuck

Reputation: 3397

You can use BlocksKit, or UIAlertView-Blocks to assign actions to alert buttons via blocks instead of delegate pattern. It's much more readable then creating lots of if alert == self.firstAlert {...} else if alert == self.secondAlert { ... } else ... conditions.

EDIT: Ok, I think we need moar options for solving this issue! Here is another (4th) approach: you can create separate class for each UIAlertView delegate:

class FirstErrorHandler: UIAlertViewDelegate {
    init() {
        let errorAlert: UIAlertView = UIAlertView()
        errorAlert.delegate = self
        errorAlert.message = "Are you sure?"
        errorAlert.addButtonWithTitle("Yes")
        errorAlert.addButtonWithTitle("No")
        errorAlert.show()
    }

    func alertView(view: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {
          // handle first kind of errors
    }
}

class SecondErrorHandler: UIAlertViewDelegate {
    init() { ... }

    func alertView(view: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {
          // handle another kind of errors
    }
}

class MyViewController {
    var alertHandler: UIAlertViewDelegate!  // store strong reference to alert handler

    ...
    func showSomeError() {
        self.alertHandler = FirstErrorHandler()
        ...
    }
}

It's good to remember that you can assign any object to UIAlertView.delegate, not only UIViewController. And it's much more readable then showing alert and labeling buttons on line 133 of MyViewController and then handling actions inside some block in list of if/else statements on line 765 of MyViewController.

Upvotes: 1

oltman
oltman

Reputation: 1792

The two answers above are correct and will work, but just to provide another alternative: it's possible to set the tag property of the alert views, so

let errorAlert: UIAlertView = UIAlertView()
errorAlert.tag = my_tag // just some arbitrary number that you can reference later
// other stuff
errorAlert.show()

and then

func alertView(view: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {
    if view.tag == my_tag {
         // view is errorAlert, perform the appropriate logic here
    }
}

Upvotes: 1

mkz
mkz

Reputation: 2302

You can try to write this:

func alertView(view: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {
    if view == errorAlert {
        // do your stuff..
    } else if view == anotherAlertView {
        // do your stuff for the second AlertView
    }
}

Upvotes: 1

Related Questions