Question

I am using the actionSheet variable passed by actionSheet:didDismissWithButtonIndex: to compare the calling actionSheet to a list of UIActionSheet variables in my class. This seems to be the way the delegate method was designed to differentiate between events.

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {

if (actionSheet == actionSheet1) {
    switch (buttonIndex) {
        case 0:
            // Do Something
            break;
        case 1:
            // Do Something Else
            break;
        case 2:
            // Cancel
            break;
    }
}

if (actionSheet == actionSheet2) {
    switch (buttonIndex) {
        case 0:
            // Do Something
            break;
        case 1:
            // Do Something Else
            break;
        case 2:
            // Cancel
            break;
    }
}

}

Each time a UIActionSheet is displayed, I alloc an instance of UIActionSheet to one of the class variables, set the display properties, display it, and release it.

This works smoothly, at first. The problem is that after running for a while, eventually both UIActionSheet pointers (class variables actionSheet1 & actionSheet2 in the code example above) will end up pointing to the same memory, depending on what happens at runtime, thereby causing both if statements in the delegate method to evaluate true. Not good.

Now, if I were to alloc each UIActionSheet only once per run and hold on to the memory (never call release), this should not happen. But I am trying to be conservative here. Does Apple intend for the delegate to be used in this way?

Was it helpful?

Solution

When you create your UIActionSheet, give it a tag:

actionSheet.tag = 1; // or 2 or 3 or ...

Then, in your delegate method, switch based on the tag to determine the behavior of that particular sheet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top