Search code examples
iphoneiosxcode4automatic-ref-counting

SBTableAlert not working


First of all, I'm just starting on iPhone development. I'm trying to get an SBTableAlert working (see https://github.com/blommegard/SBTableAlert )

My initial setup is simple: I have a UIViewController with a button. On the button press, I do the following (as per the SBTableAlert example):

- (IBAction)myBtn_Press
{
    SBTableAlert *alert;
    alert   = [[SBTableAlert alloc] initWithTitle:@"Apple Style" cancelButtonTitle:@"Cancel" messageFormat:nil];
    [alert.view setTag:2];
    [alert setStyle:SBTableAlertStyleApple];

    MySecondViewController *myWGVC = [[MySecondViewController alloc] init];

    [alert setDelegate:myWGVC];
    [alert setDataSource:myWGVC];

    [alert show];
}

MySecondViewController is declared as:

@interface MySecondViewController : NSObject <SBTableAlertDelegate, SBTableAlertDataSource>

which means it will function as a delegate for the table view. I also include the following (pasted from the example):

@implementation MySecondViewController

#pragma mark - SBTableAlertDataSource

- (UITableViewCell *)tableAlert:(SBTableAlert *)tableAlert cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;

    if (tableAlert.view.tag == 0 || tableAlert.view.tag == 1) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    } else {
        // Note: SBTableAlertCell
        cell = [[SBTableAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }

    [cell.textLabel setText:[NSString stringWithFormat:@"Cell %d", indexPath.row]];

    return cell;
}

- (NSInteger)tableAlert:(SBTableAlert *)tableAlert numberOfRowsInSection:(NSInteger)section {
    if (tableAlert.type == SBTableAlertTypeSingleSelect)
        return 3;
    else
        return 10;
}

- (NSInteger)numberOfSectionsInTableAlert:(SBTableAlert *)tableAlert {
    if (tableAlert.view.tag == 3)
        return 2;
    else
        return 1;
}

- (NSString *)tableAlert:(SBTableAlert *)tableAlert titleForHeaderInSection:(NSInteger)section {
    if (tableAlert.view.tag == 3)
        return [NSString stringWithFormat:@"Section Header %d", section];
    else
        return nil;
}

#pragma mark - SBTableAlertDelegate

- (void)tableAlert:(SBTableAlert *)tableAlert didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableAlert.type == SBTableAlertTypeMultipleSelct) {
    UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:indexPath];
        if (cell.accessoryType == UITableViewCellAccessoryNone)
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        else
            [cell setAccessoryType:UITableViewCellAccessoryNone];

            [tableAlert.tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
}

- (void)tableAlert:(SBTableAlert *)tableAlert didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"Dismissed: %i", buttonIndex);
}

The error message I'm getting is:

2013-04-25 00:13:35.389 MyTestProject[3386:c07] *** -[SBTableAlert tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x682ed80

however I have no idea how to trace this or debug it. It appears it could have something to do with ARC, since the demo project doesn't use it, but I can't pinpoint how to fix this.

Any help is appreciated!


Solution

  • Try creating properties with strong attribute in your main UIViewController subclass for both alert and myWGVC objects. They seem to be deallocated because of ARC before the alert is presented on screen since there are no strong references to the alert's delegate/datasource and the alert itself.