2013-04-25 61 views
1

首先,我刚开始进行iPhone开发。我试图让SBTableAlert工作(请参阅https://github.com/blommegard/SBTableAlertSBTableAlert无法正常工作

我的初始设置很简单:我有一个带有按钮的UIViewController。在按钮按下,我这样做时(按照SBTableAlert示例):

- (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被声明为:

@interface MySecondViewController : NSObject <SBTableAlertDelegate, SBTableAlertDataSource> 

这意味着它将为表视图委托起作用。我还包括以下(从例如粘贴):

@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); 
} 

我得到的错误信息是:

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

,但是我不知道如何来跟踪这个或调试。看起来它可能与ARC有关,因为演示项目没有使用它,但我无法确定如何解决这个问题。

任何帮助表示赞赏!

+0

如果你不使用ARC,那么你需要'[alert autorelease];' – 2013-04-25 06:56:37

+0

我正在使用ARC。 – Sandy 2013-04-25 14:07:53

回答

3

尝试在alertmyWGVC对象的主UIViewController子类中使用strong属性创建属性。因为没有强烈的引用警报的委托/数据源和警报本身,因此在警报显示在屏幕上之前,它们似乎被ARC解除分配。

+0

这是做的伎俩,谢谢! – Sandy 2013-04-25 14:55:36