Objective C Blocks in an Array Passed To Subclass of UIAlertView - objective-c

(A working solution, based on the responses, is provided at the end of this post.)
I thought this would be a tidy way to handle the callbacks that a particular alert view needs to address, so I don't have a single delegate method filtering all of the alert button presses. Here is the code:
#import "LSAlertView.h"
#implementation LSAlertView
- (id) initWithTitle:(NSString *)title
message:(NSString *)message
actionBlocks:(NSArray*)_actionBlocks
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
self = [super initWithTitle:title
message:message
delegate:self
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:otherButtonTitles,nil];
if (self) {
self.cancelButtonIndex = 0;
actionBlocks = [_actionBlocks retain];
[self show];
}
return self;
}
- (void) dealloc {
[actionBlocks release];
[super dealloc];
}
- (void) alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
void (^action)(void) = [actionBlocks objectAtIndex:buttonIndex];
action();
}
#end
This works fine for two buttons set up like this:
- (void) restartSearches {
NSArray *actionBlocks = [NSArray arrayWithObjects:
^{NSLog(#"Cancel Button Selected");},
^{NSLog(#"Delete Button Selected");},
nil];
alertDeletingSearches = [[LSAlertView alloc]
initWithTitle:#"You Are About To Delete Your Current Searches"
message:#"Select Delete to Continue"
actionBlocks:actionBlocks
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Delete", nil];
[alertDeletingSearches release];
}
But as soon as I add some useful calls in one of the blocks, like this
- (void) restartSearches {
NSArray *actionBlocks = [NSArray arrayWithObjects:
^{NSLog(#"Cancel Button Selected");},
^{
[mapController.theMap removeAnnotations:mapController.theMap.annotations];
[dataInterface deleteDB];
[[NSNotificationCenter defaultCenter]
postNotificationName:#"changeToFavorites"
object:nil];
NSLog(#"Delete Button Selected");
},
nil];
alertDeletingSearches = [[LSAlertView alloc]
initWithTitle:#"You Are About To Delete Your Current Searches"
message:#"Select Delete to Continue" actionBlocks:actionBlocks
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Delete", nil];
[alertDeletingSearches release];
}
it freezes, and I get a EXC_BAD_ACCESS error.
Am I doing something fundamentally wrong, or is there a minor error in my logic?
UPDATE
Handled the variadic problem problem using Firoze's suggestion below. (Follows the examples given at Numbergrinder)
- (id) initWithTitle:(NSString *)title message:(NSString *)message actionBlocks:(NSArray*)_actionBlocks cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
if (self) {
va_list args;
va_start(args, otherButtonTitles);
NSString* buttonTitle;
while ((buttonTitle = va_arg(args, NSString *))) {
[super addButtonWithTitle:buttonTitle];
}
self.cancelButtonIndex = 0;
actionBlocks = [_actionBlocks retain];
[self show];
}
return self;
}
Here is the header file:
#interface LSAlertView : UIAlertView <UIAlertViewDelegate> {
NSArray *actionBlocks;
}
- (id) initWithTitle:(NSString *)title message:(NSString *)message actionBlocks:(NSArray*)_actionBlocks cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;
#end

So I see a couple of issues with this.
One is that you need to copy those blocks as you put them in the array. Those blocks are created on the stack. If you want to pass them to your alert view and you expect the alert view to hold onto them for later use, you need to copy them to the heap first.
So something like this should work:
NSArray *actionBlocks = [NSArray arrayWithObjects:
[[^{NSLog(#"Cancel Button Selected");} copy] autorelease],
[[^{
[mapController.theMap removeAnnotations:mapController.theMap.annotations];
[dataInterface deleteDB];
[[NSNotificationCenter defaultCenter] postNotificationName:#"changeToFavorites" object:nil];
NSLog(#"Delete Button Selected");
} copy] autorelease]
, nil];
Note the [^someBlock copy] around each block literal there. That should solve one issue.
The other issue, to which I don't know the answer, is that this is a variadic method (takes a variable number of arguments). I don't know of a way in a variadic method to turn around and call another variadic method (the UIAlertView initializer), unless you have a variation of the second method that takes a va_list. This is the same issue we have in C, inherited in Objective C as far as I understand it.
I think you haven't run into that yet because you haven't tried enough buttons for that.
EDIT
Thinking about this further, I guess you could get around the second issue by iterating through the varargs and then calling [self addButtonWithTitle:arg] for each of them.

You might find Lambda Alert useful:
LambdaAlert *alert = [[LambdaAlert alloc]
initWithTitle:#"Test Alert"
message:#"See if the thing works."];
[alert addButtonWithTitle:#"Foo" block:^{ NSLog(#"Foo"); }];
[alert addButtonWithTitle:#"Bar" block:^{ NSLog(#"Bar"); }];
[alert addButtonWithTitle:#"Cancel" block:NULL];
[alert show];
And:
LambdaSheet *sheet = [[LambdaSheet alloc] initWithTitle:#"Action Sheet"];
[sheet addButtonWithTitle:#"Miles" block:^{ NSLog(#"Trumpet"); }];
[sheet addButtonWithTitle:#"Trane" block:^{ NSLog(#"Saxophone"); }];
[sheet addDestructiveButtonWithTitle:#"Monk" block:^{ NSLog(#"Piano"); }];
[sheet addCancelButtonWithTitle:#"Back to the Head"];
[sheet showInView:window];
Static library, easy to include with your project using an Xcode workspace.

Related

UIAlertView with a user supplied context and [self autorelease]

I have looked over some ideas for how to supply a context to a UIAlertView. The common answers are save it in a dictionary or subclass UIAlertView. I don't like the idea of saving the context in a dictionary, it's the wrong place for the data. Subclassing UIAlertView is not supported by Apple, so by my standard, is not a good solution.
I came up with an idea, but I'm not sure what to make of it. Create an instance of a context object that is the delegate of UIAlertView. The alert view context, in turn, has it's own delegate which is the view controller.
The trouble is releasing memory. I set alertView.delegate to nil and call [self autorelease] to free the context object in -alertView:didDismissWithButtonIndex:.
THE QUESTION IS: What problems am I causing myself? I have a suspicion that I'm setting myself up for a subtle memory error.
Here is the simple version which only supports -alertView:clickedButtonAtIndex:
Use
- (void)askUserIfTheyWantToSeeRemoteNotification:(NSDictionary *)userInfo
{
[[[[UIAlertView alloc] initWithTitle:[userInfo valueForKey:#"action"]
message:[userInfo valueForKeyPath:#"aps.alert"]
delegate:[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo]
cancelButtonTitle:#"Dismiss"
otherButtonTitles:#"View", nil] autorelease] show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex withContext:(id)context
{
if (buttonIndex != alertView.cancelButtonIndex)
[self presentViewForRemoteNotification:context];
}
Interface
#protocol WantAlertViewContextDelegate <NSObject>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex withContext:(id)context;
#end
#interface WantAlertViewContext : NSObject <UIAlertViewDelegate>
- (id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context;
#property (assign, nonatomic) id<WantAlertViewContextDelegate> delegate;
#property (retain, nonatomic) id context;
#end
Implementation
#implementation WantAlertViewContext
- (id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context
{
self = [super init];
if (self) {
_delegate = delegate;
_context = [context retain];
}
return self;
}
- (void)dealloc
{
[_context release];
[super dealloc];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self.delegate alertView:alertView clickedButtonAtIndex:buttonIndex withContext:self.context];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
alertView.delegate = nil;
[self autorelease];
}
#synthesize delegate = _delegate;
#synthesize context = _context;
#end
You can use the concept of associated objects. Using the functions objc_setAssociatedObject() and objc_getAssociatedObject(). You can use these properties to essentially add a new property, in your case to hold an NSDictionary, to an object through a category.
Here is an example of a UIAlertView category. These files should be compiled without ARC, -fno-objc-arc flag set if the project is using ARC.
UIAlertView+WithContext.h:
#import <UIKit/UIKit.h>
#interface UIAlertView (Context)
#property (nonatomic, copy) NSDictionary *userInfo;
#end
UIAlertView+WithContext.m:
#import "UIAlertView+WithContext.h"
// This enum is actually declared elseware
enum {
OBJC_ASSOCIATION_ASSIGN = 0,
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
OBJC_ASSOCIATION_RETAIN = 01401,
OBJC_ASSOCIATION_COPY = 01403
};
#implementation UIAlertView (Context)
static char ContextPrivateKey;
-(void)setUserInfo:(NSDictionary *)userInfo{
objc_setAssociatedObject(self, &ContextPrivateKey, userInfo, 3);
}
-(NSDictionary *)userInfo{
return objc_getAssociatedObject(self, &ContextPrivateKey);
}
#end
This category is easily used.
SomeViewController.m: a UIAlertViewDelegate using ARC or not.
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.userInfo = [NSDictionary dictionaryWithObject:#"Hello" forKey:#"Greeting"];// autorelease if MRC
[alert show]; // release if MRC
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(#"userInfo:%#",alertView.userInfo);
}
When you press the alertview's OK button you will see:
userInfo:{
Greeting = Hello;
}
A couple of notes:
1) Make sure the association type matches the property declaration so things behave as expected.
2) You probably shouldn't use userInfo for the property/association since Apple may well decide to add a userInfo property to UIAlertView in the future.
Edit To address your concerns about your [self autorelease];
It is imperative that you balance your implicit alloc retain from this line: delegate:[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo]. You achieve this balance by calling [self autorelease]; in the final UIAlertView delegate method.
Granted, this does feel wrong. Mostly because there is no way when looking at this that it doesn't at first blush look like memory mis-management. But there is one simple way to avoid this "controlled leak" API you are creating; Have the instance of WantAlertViewContext explicitly retain itself. For example:
-(id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context{
self = [super init];
if (self) {
_delegate = delegate;
_context = [context retain];
}
return [self retain]; // Explicitly retain self
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
alertView.delegate = nil;
[self autorelease]; // Or just [self release]; doesn't make much difference at this point
}
Now your class has some internal harmony. I say some because this is still not perfect. For example, if an instance is never an alert-view delegate it will never be released. It is still just a "semi-controlled" memory leak.
Anyway, now your instantiation call can look more logical:
delegate:[[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo] autorelease];
I think that this particular design pattern is fraught with danger. If you do end up using it keep a close eye on it.
I've come up with a simpler solution that may fit in some circumstances. Because you get the NSAlertView context when the delegate gets called, I use the actual address of the object to make a tag (NSString*) which I then use to store custom values in a global or object specific NSDictionary. Here is an example:
+(NSString*)GetTag:(id)ObjectIn
{
return [NSString stringWithFormat:#"Tag-%i",(int)ObjectIn];
}
In the Delegate:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString* MyID = [CommandManager GetTag:alertView];
[CurrentActiveAlerts removeObjectForKey:MyID];
}
Calling:
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:title_text
message:#""
delegate:self
cancelButtonTitle:nil
otherButtonTitles:button_text ,nil];
CurrentActiveAlerts[[CommandManager GetTag:myAlert]] = CommandToRun; // Querky way to link NSDict to UIAlert, but the best I could think of
[myAlert show];
[myAlert release];
The keys will end up looking like "Tag-226811776". Hope this helps.

iOS: Simple NSMutableArray Call

I need to call removeObject in one of my methods, but I can't figure out how to do this correctly. I'm very new to Objective-C, and am still learning the basics. I have an app that behaves somewhat like a photo gallery, and displays UIImageViews. I'm implementing the option to have the user delete photos from their gallery. To accomplish this, I decided to place an invisible button over each picture. When the user hits an "Edit" button, the hidden delete button over each picture becomes active (I'm using the same IBOutlet over each of the hidden buttons, for simplicity). When the user taps the button over the picture, an alert view appears asking if they really want to delete it. If they click yes, deleteAlertView comes into play:
- (void)deleteAlertView:(UIAlertView *)deleteButtonPressed
didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [deleteButtonPressed cancelButtonIndex]) {
[array removeObject:#"%#", deleteButtonPressed];
}
The issue here is [array removeObject:#"%#", deleteButtonPressed];, I did the %# so that this will automatically determine which object in the array was tapped, rather than manually putting in a new method and button for each UIImageView (I may have to end up doing that). I'm getting errors regarding "array" and "deleteButtonPressed" (use of undeclared identifier), I can't for the life of me figure out what to put instead. I'm still learning the basics and how inheritance in this language works. Any help or advice would be great! I should probably post the whole view controller file to show the related inheritance:
- (IBAction)grabImage {
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
_popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else {
[self presentModalViewController:imgPicker animated:YES];
}
[self.imgPicker resignFirstResponder];
}
// Sets the image in the UIImageView
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView.image == nil) {
imageView.image = img;
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
if (imageView2.image == nil) {
imageView2.image = img;
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
if (imageView3.image == nil) {
imageView3.image = img;
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
}
- (void)viewWillAppear:(BOOL)animated
{
self.user = [NSUserDefaults standardUserDefaults];
NSMutableArray* array = [[self.user objectForKey:#"images"]mutableCopy];
while(array == nil)
{
[self.user setObject:[NSMutableArray arrayWithObject:#""] forKey:#"images"];
array = [[self.user objectForKey:#"images"]mutableCopy];
NSLog(#"%#",#"attempting to create an array to store the images in");
}
}
- (void)applicationDidEnterBackground:(UIApplication*)application {
NSLog(#"Image on didenterbackground: %#", imageView);
NSMutableArray* array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]];
[self.user setObject:array forKey:#"images"];
[user synchronize];
}
- (void)viewDidLoad
{
self.user = [NSUserDefaults standardUserDefaults];
NSLog(#"It is %#", self.user);
NSMutableArray* array = [[self.user objectForKey:#"images"]mutableCopy];
imageView.image = [[UIImage alloc] initWithData:[array objectAtIndex:0]];
imageView2.image = [[UIImage alloc] initWithData:[array objectAtIndex:1]];
imageView3.image = [[UIImage alloc] initWithData:[array objectAtIndex:2]];
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:app];
backToGalleryButton.hidden = YES;
tapToDeleteLabel.hidden = YES;
deleteButton1.hidden = YES;
[super viewDidLoad];
}
- (IBAction)deleteButtonPressed:(id)sender {
UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:#"Delete"
message:#"Are you sure you want to delete this photo?"
delegate:self
cancelButtonTitle:#"Yes"
otherButtonTitles:#"No", nil];
[deleteAlertView show];
}
- (void)deleteAlertView:(UIAlertView *)deleteButtonPressed
didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [deleteButtonPressed cancelButtonIndex]) {
[array removeObject:#"%#", deleteButtonPressed];
}
}
There is one thing wring here, but first a relatively minor point:
- (void)deleteAlertView:(UIAlertView *)deleteButtonPressed didDismissWithButtonIndex:(NSInteger)buttonIndex
the phrase "deleteButtonPressed" implies an even, as it ends in a verb. It Is actually referring to an object, specifically a parameter of the type UIAlertView. you should call it something more like AlertView.
Secondly this line is quite wrong:
[array removeObject:#"%#", deleteButtonPressed];
You are trying to remove a string. If that method accepted an argument list (where you pass multiple objects separated by a comma), you would be removing literally "deleteButtonPressed". You want to remove the object that is being pointed to by the deleteButtonPressed variable. So all you have to do is:
[array removeObject:deleteButtonPressed];
The issue here is [array removeObject:#"%#", deleteButtonPressed];
Yes, that is one of the issues (even ignoring the invalid syntax). The array does not contain your UIAlertView, it contains whatever objects [user objectForKey:#"images"] contains. Which seem like they should be NSData instances and which in any case are definitely not your UIAlertView instance(s).
So in other words, you can't pass the UIAlertView to the array in order to have the array magically work out what item the UIAlertView is supposed to correspond to. Instead what you should do is tag the UIAlertView with the index it corresponds to when you create it. You can do this like:
UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:#"Delete"
message:#"Are you sure you want to delete this photo?"
delegate:self
cancelButtonTitle:#"Yes"
otherButtonTitles:#"No", nil];
int imageIndex = <figure out the index of the associated array element based upon 'sender'>;
deleteAlertView.tag = imageIndex;
...and then when the button is pressed, you do:
[array removeObjectAtIndex:deleteButtonPressed.tag];
And to fix up that "undeclared identifier" issue, you should declare array in your header and not in viewDidLoad. You want it to be a private instance variable, not a local variable.
Also note that deleting an element from [[user objectForKey:#"images"] mutableCopy] will not automatically cause the corresponding element to be deleted from [user objectForKey:#"images"]. You need to write the modified array back to [NSUserDefaults standardUserDefaults] if you want the modification to actually persist.
You are getting the error "Use of undeclared identifier array" because you declare the array in different methods, but not in your deleteAlertView method. I suggest reading up on variable scope.
Fixing that, however, will not get your code to work because you have some fundamental design flaws that need to be worked out.
You mention that you are a beginner, so I would suggest reading through and completing several beginner tutorials before attempting this app. I know it is fun to dive right into a project, but you will very likely get frustrated and also develop bad habits with respect to app design/engineering. In particular, I would try to get a firmer understand of variable scope and MVC design patterns.

Displaying UIAlertViews Sequentially

I have several UIAlertViews that i want to display in a sequential order, and only move on to display the next UIAlertView once the previous one has been dismissed (by user clicking okay).
I know about the didDismissWithButtonIndex delegate and adding a tag, but this doesn't really help too much as there could be upto 3 UIAlertViews invoked and not necessarily in the same order everytime. see code:
if(condition 1){
alert1 = // UIAlertView[[.....
[alert1 show]
}
if(condition 2){
alert2 = // UIAlertView[[.....
[alert2 show]
}
if(condition 3){
alert3 = // UIAlertView[[.....
[alert3 show]
}
The above will just add 3 Alerts on top of each other (depending on how many conditions are met) which is not what I want. I want to be able to only show one at a time and then the next one (if there is one) after the user hits the ok button.
I had the idea of maybe adding the messages to a queue and then processing that queue removing the alert every time an alert is dismissed but i'm not sure how id go about doing that.
Any ideas would be much appreciated.
Thanks
You could easily do it in the UIAlertView delegate method, called once an alert view is dismissed. So, UIAlertViewDelegate defines the following delegate method:
– alertView:didDismissWithButtonIndex:
Implement that method, and make sure your class is the delegate of the UIAlertViews you create. This method is the perfect place to then show the next alert based on the one being dismissed by the user.
If your requirement is "Display up to three alerts, sequentially, but not always in the same order" i'd probably put the alerts in to an array, and then in the delegate method get the next alert out of the array to show. It doesn't have to be any more complex than that really; the key thing is that the delegate method implementation is the best place to show the next alert.
Pseudo Code Example:
Define an array; NSMutableArray * alerts_;
- (void)showAlertSequence {
if ( !alerts_ ) {
alerts_ = [[NSMutableArray alloc] init];
}
[alerts_ addObjects;<My alerts>];
[self showSequencedAlertFrom:nil];
}
- (BOOL)showSequencedAlertFrom:(UIAlertView *)sourceAlertView {
if ( !sourceAlertView ) {
[[alerts_ objectAtIndex:0] show];
}
else {
NSInteger index = [alerts_ indexOfObject:sourceAlertView];
if ( index < [alerts_ count] ) {
[[alerts_ objectAtIndex:index++] show];
}
}
return NO;
}
– alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)index {
// Show the next alert or clean up if we're at the end of the sequence.
if ( ![self showSequencedAlertFrom:alertView] ) {
[alerts_ removeAllObjects];
}
}
As an aside; three sequential alerts will really annoy you users ;)
One thing that I've done is used block based UIAlertViews by adding a category on AlertView.
Here is the .h file
#interface UIAlertView (WithBlocks)
- (id) initWithTitle:(NSString *)title message:(NSString *)message;
- (void) addButtonWithTitle:(NSString *)title andBlock:(void(^)())block;
#end
Here is the .m file
static NSString *BUTTON_BLOCK_KEY = #"alertview-button-blocks";
#interface UIAlertView()
- (void) runBlock: (void (^)())block;
#end
#implementation UIAlertView (WithBlocks)
/**
* Initialized an alert view with a title and message.
*/
- (id) initWithTitle:(NSString *)title message:(NSString *)message
{
self = [self initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
if (self) {
self.delegate = self;
NSMutableArray *buttonBlocks = [NSMutableArray array];
objc_setAssociatedObject(self, BUTTON_BLOCK_KEY, buttonBlocks, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return self;
}
/**
* Adds a button with a title and a block to be executed when that button is tapped.
*/
- (void) addButtonWithTitle:(NSString *)title andBlock:(void (^)())block
{
// Add the button
[self addButtonWithTitle:title];
NSMutableArray *buttonBlocks = objc_getAssociatedObject(self, BUTTON_BLOCK_KEY);
if (!block) {
block = ^{ /* empty block */ };
}
[buttonBlocks addObject:[[[block copy] retain] autorelease]];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSMutableArray *buttonBlocks = objc_getAssociatedObject(self, BUTTON_BLOCK_KEY);
void (^block)() = (void (^)()) [buttonBlocks objectAtIndex:buttonIndex];
// Due to a timing issue, the current window is still the UIAlertView for a very
// short amount of time after it has been dismissed which messes up anything
// trying to get the current window in the blocks being run.
// Ergo, the block is being delayed by a tiny bit. (Amount determined through limited testing)
[self performSelector:#selector(runBlock:) withObject:block afterDelay:0.25];
}
- (void) runBlock: (void (^)())block
{
block();
}
#end
Then you can call chain the alertviews together by the following code
void(^continueBlock)(void) = ^{
// Display more alertviews here
};
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message"];
[alert addButtonWithTitle:#"Continue" andBlock:continueBlock];
[alert addButtonWithTitle:#"Dismiss" andBlock:^{
// Display more alertviews here
}
[alert show];
[alert release];
I was looking for a solution to this problem as well. Here's the way I ended up solving it for my own app:
static BOOL alertShowing = FALSE;
UIAlertView *alert0 = [[UIAlertView alloc] initWithTitle:#"AlertView 0" message:#"This is the first alert" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Yes",#"No", nil];
[alert0 setTag:0];
alertShowing = TRUE;
[alert0 show];
while (alertShowing) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
}
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:#"AlertView 1" message:#"This is the second alert" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Yes",#"No", nil];
[alert1 setTag:1];
alertShowing = TRUE;
[alert1 show];
while (alertShowing) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
}
// add some more alerts here for dramatic effect ...
Your button handler must set alertShowing = FALSE' in every exit path.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// Deal with handling responses for your different alerts here.
switch ([alertView tag]) {
case 0:
// handler first alert here
break;
case 1:
// handler second alert here
break;
default:
// etc.
break;
}
alertShowing = FALSE;
}
There may be better ways to sit and spin than creating a new run loop, and there's some duplicate code that probably could be genericized better. On the plus side, it's straightforward and doesn't require a bunch of queuing logic. I'm using a #define for this pattern to keep from having to hand-type it, and it has worked fine in my case.
Here's how I did it using a queue of alert's like you suggested.
#property (strong, nonatomic) NSMutableArray *alertQueue;
#property (nonatomic) BOOL showingAlert;
- (void)showAlert:(UIAlertView *)alert {
if (self.showingAlert) {
[self.alertQueue addObject:alert];
}
else {
self.showingAlert = YES;
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([self.alertQueue count]) {
UIAlertView *alert = [self.alertQueue objectAtIndex:0];
[self.alertQueue removeObjectAtIndex:0];
[alert show];
}
else self.showingAlert = NO;
}
Then whenever you want to display an alert, you just create the UIAlertView and pass it to the showAlert method and it will only show up after all earlier alerts have been dismissed.

Action sheet getting caught on tabbar

This is a tricky one that I've been working on for a while. I have a an action sheet that appears in a view with a tabbar, sometimes when I dismiss the action sheet it gets "caught" in the tabbar and am unable to do anything.The app doesn't crash because if i minimize it and then come back in it is dimissed, it just appears that the modal behavior isn't being dismissed properly.
I recently changed how the action sheet is displayed from "showInView" to "showFromTabBar" and am not sure if this is creating the issue.
Thanks,
William
Edit:
Below is the code I use to dismiss my actionsheet:
-(void)doneButtonPressed:(id)sender{
if ([[self viewWithTag:1] isKindOfClass:[PickerView class]]) {
PickerView *picker = (PickerView *)[self viewWithTag:1];
if (picker.selectedRow == nil) {
[picker populateSelectRowForRow:0 andComponent:0];
}
NSNotification *note = [NSNotification notificationWithName:#"doneButtonPressed" object:self.indexPath userInfo:picker.selectedRow];
[[NSNotificationCenter defaultCenter] postNotification:note];
}else {
DatePickerView *picker = (DatePickerView *)[self viewWithTag:1];
NSDictionary *extraInfo = [[NSDictionary alloc] initWithObjects:[[NSArray alloc] initWithObjects:[self formatDateToString:[picker date]], nil] forKeys:[[NSArray alloc] initWithObjects:#"value", nil]];
NSNotification *note = [NSNotification notificationWithName:#"doneButtonPressed" object:self.indexPath userInfo:extraInfo];
[[NSNotificationCenter defaultCenter] postNotification:note];
}
[self dismissWithClickedButtonIndex:0 animated:YES];
}
and the notification method that is called:
-(void)pickerUpdate:(NSNotification *)note{
NSIndexPath *indexPath = [note object];
NSDictionary *extraInfo = [note userInfo];
NSDictionary *dict = [[tableController.sectionAndFields objectAtIndex:indexPath.section] objectAtIndex:(indexPath.row + kHeaderAndFooterOffset)];
[tableController.formDetails setObject:[extraInfo objectForKey:#"value"] forKey:[dict objectForKey:#"key"]];
NSArray *reloadArray = [[NSArray alloc] initWithObjects:indexPath, nil];
[indexPath release];
[self.tv reloadRowsAtIndexPaths:reloadArray withRowAnimation:NO];
[reloadArray release];
}
I did manage to solve this however it feels very much like a hack, I introduced a delay into the code using:
[self performSelector:#selector(dismissFromActionSheet) withObject:nil afterDelay:0.2];
With dismissFromActionSheet being:
-(void)dismissFromActionSheet{
[self dismissWithClickedButtonIndex:0 animated:YES];
}

Make UIAlertView blocking

I need make UIAlertView blocking. Because i have function and i need to return UIAlertView choice. But problem is that after UIAlertView is shown my function code is executing further so i can't catch UIAlertView choice (i can do it in delegate methods, but i need to return function result).
I tried to make UIAlertVIew blocking with NSCondition. But the code don't works.
condition = [NSCondition new];
result = 0 ;
[condition lock];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Fingerprint" message:#"test" delegate:window_self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alert setDelegate:self];
[alert show];
while (result == 0) [condition wait];
[condition unlock] ;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[condition lock] ;
if (buttonIndex == 0)
{
result = 2;
}
else if (buttonIndex == 1)
{
result = 3 ;
}
[condition signal] ;
[condition unlock] ;
}
Maybe how to fix this code or any other suggestions ? Thanks
There's no way to achieve what you want. Only through the delegate. You should redesign your function or refuse using UIAlertView
This doesn't make it blocking, but I have written a subclass to add block style syntax which makes it much easier to handle the buttonClickedAtIndex method without having to do a delegate and a whole bunch of if statements if you have multiple UIAlertViews in one class.
#import <UIKit/UIKit.h>
#interface UIAlertViewBlock : UIAlertView<UIAlertViewDelegate>
- (id) initWithTitle:(NSString *)title message:(NSString *)message block: (void (^)(NSInteger buttonIndex))block
cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_AVAILABLE(10_6, 4_0);
#end
#import "UIAlertViewBlock.h"
#interface UIAlertViewBlock()
{
void (^_block)(NSInteger);
}
#end
#implementation UIAlertViewBlock
- (id) initWithTitle:(NSString *)title message:(NSString *)message block: (void (^)(NSInteger buttonIndex))block
cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_AVAILABLE(10_6, 4_0)
{
if (self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil])
{
_block = block;
}
return self;
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
_block(buttonIndex);
}
#end
Then to call it here is some example code. The other cool part is that because a block closes around the local variables, I can have access to all the state that existed at the time I show the UIAlertView. Using the traditional delegate approach, you would have to store all that temporary state into class level variables to have access to it in the call to buttonClickedAtIndex in the delegate. This is so much cleaner.
{
NSString *value = #"some random value";
UIAlertViewBlock *b = [[UIAlertViewBlock alloc] initWithTitle:#"Title" message:#"Message" block:^(NSInteger buttonIndex)
{
if (buttonIndex == 0)
NSLog(#"%#", [value stringByAppendingString: #" Cancel pressed"]);
else if (buttonIndex == 1)
NSLog(#"Other pressed");
else
NSLog(#"Something else pressed");
}
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Other", nil];
[b show];
}
I was just facing the same problem. Although no solution, there are at least 2 workarounds that I thought of.
Loop "solution"
Right after you call the UIAlert you start a loop that looks for the change in a variable that is global to your object (not to the whole project, mind you) that variable is the one you set in the UIAlert delegate that takes the answers. So basically you wait for "is A == 1, if not DoEvents" and loop on it.
Then on the delegate you make A=1 when you have the answer
and before someone says that there is no DoEvents in Cocoa:
void MyTestClass::DoEvents()
{
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
    untilDate:[NSDate distantPast]
    inMode:NSDefaultRunLoopMode
    dequeue:YES];
  if (event) {
    [NSApp sendEvent:event];
    [NSApp updateWindows];
  }
  [pool release];
}
Delegate Solution
Instead of having the code that deals with Answer A, B or C in the function that calls the Alert, have the code in the delegate itself.
Hope it helps.
I used the second one in my project and it worked.
I just found this question by accident and even by entering Apple hell by posting this, I hereby proclaim this as a proof of concept:
#interface EvilShitClass () <UIAlertViewDelegate>
#end
#implementation EvilShitClass {
BOOL _isCanceled, _wasYes;
}
Here is the static method for a yes/no query:
+ (BOOL)yesNoQueryWithTitle:(NSString*)title text:(NSString*)text {
EvilShitClass *shit = [EvilShitClass new];
UIAlertView *alertView = [UIAlertView new];
alertView.delegate = shit;
alertView.title = title;
alertView.message = text;
[alertView addButtonWithTitle:#"Yes"];
[alertView addButtonWithTitle:#"No"];
NSRunLoop *run_loop = [NSRunLoop currentRunLoop];
[alertView show];
while( !shit->_isCanceled ) {
BOOL tmp = [run_loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate date]];
}
return shit->_wasYes;
}
and finally the delegate method for handling the button click and
stop the runloop-processing:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
_wasYes = (buttonIndex == 0);
_isCanceled = YES;
}
This works but remember: you shouldn't do it this way :-D
Pretty please don't argue about style and stuff, it's just a 5 minutes quick hack to proof it can be done! This should work without ARC (new -> autorelease)
but if I'm wrong you know how to handle it ;)
Disclaimer: I'm not responsible for any possible damage the use of this snippet could do to your application or devices. Thank you.
Use the UIAlertView with blocks from Joseph and add a semaphore to it.
Declare a global semaphore
dispatch_semaphore_t generateNotificationsSemaphore;
And signal the semaphore in the block handler
[alert showWithHandler:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == [alertView cancelButtonIndex]) {
} else {
}
dispatch_semaphore_signal(generateNotificationsSemaphore);
}];
After calling the showWithHandler add a waiting loop using the semaphore
while (dispatch_semaphore_wait(generateNotificationsSemaphore, DISPATCH_TIME_NOW )) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:20]];
}
Your actual timeout value may be different depending on your needs.