Im trying to add a UIAlertView to warn the user that the link will open in Safari. The user can then choose OK(open the url) or cancel which should just close the alert and return to the links.
I have three different UIButtons which has 3 different URLs.
Right now ive added a IBAction to all buttons and all buttons has Tags (which i think i can use somehow :D). I guess - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: delegate will be good to use to..
My question: how should the UIAlertView know what URL to open i user clicks ok?
I suggest that you use a subclass of
UIAlertViewthat is able to track some more properties along. I do this in all my projects and it is much simpler.UIAlertViewtoMyAlertViewand add a@property(nonatomic, retain) id userInfo;or@property(nonatomic, retain) NSURL* urlToOpen. Thus you can attach custom data to yourUIAlertViewand retrieve it in the delegate method to do whatever you need with it.UIAlertView, to be able to useUIAlertViewusing the blocks API instead of using adelegate. This is particularly useful if you use multipleUIAlertViewsin the same class and with the same delegate, as using a single delegate to handle the different instances is a mess.I personally use this technique all the time, as it also makes my code more readable by having the code that executes when the button is tapped right next to the code that shows the alert, instead of having it at a complete different places when you use delegate methods.
You can look at my
OHAlertViewsubclass here on GitHub that implement this already. The usage is really simple and allow you to use blocks for each alert view instead of a common delegate, see below.Usage Example
Then each button can have its own action:
Or you can have a common IBAction for all your buttons opening URLs: