Nick Harris

UIAlertView with an NSObject

with 3 comments

Here’s my situation… I have an operation that gets performed in the background. If that operation fails I need to alert the user and see what they would like to do about it. If they want to retry it, I just add the operation back onto the queue.

To do this, I’m sending a notification that the operation failed and am including the operation as the notifications object. I then set up a responder on my view controller for that notification. It creates a UIAlertView to ask the user what to do.

If the user selects “Retry”, it would be nice to have the operation right there on the alert view in alertView:clickedButtonAtIndex: so that I can just add it back to the operation queue. But there doesn’t seem to be any properties on UIAlertView for an arbitrary object. Instead I’ve subclassed UIAlertView and simply added a NSObject property.

Pretty easy code wise, just wondering if I’m missing something on UIAlertView that would handle the same thing.

Advertisements

Written by Nick Harris

March 26, 2010 at 5:00 pm

Posted in Code

Tagged with

3 Responses

Subscribe to comments with RSS.

  1. You could (and this is a bit of a hack) grab the layer of the view and then do a -setValue:forKey: on the layer as the layer will hold onto anything.

    No subclassing required.

    Marcus S. Zarra

    March 26, 2010 at 5:05 pm

  2. Core Animation… is there anything it *can’t* do? 🙂

    Nick Harris

    March 26, 2010 at 5:12 pm

  3. I guess since your ViewController is the one responding to the notification, you could store a pointer to the Operation right there. The UIAlertView would then use that ViewController as it’s delegate so when alertView:didDismissWithButtonIndex: is called you can get at the Operation to add back back on the queue.

    The problem here is if you have multiple ViewControllers you would have to do this for every one. Where as your UIAlertView subclass keeps all this logic in one place.

    Jay Graves

    March 26, 2010 at 5:21 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: