2014-07-22 84 views
0

我试图在数组可用时将通知放入数组中,但是当我推送新通知时,数组的计数重置为1。获取具有通知的数组

这是代码:

int r = 0; 
    listMsgReceived = [[NSMutableArray alloc] init]; 

    if (notification) 
    { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notification received" message:[NSString stringWithFormat:@"%@", message] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alertView show]; 
     [listMsgReceived insertObject:message atIndex:r]; 
     r++; 
     NSLog(@"apres: %d \n", [listMsgReceived count]); 
    } 
+0

看起来你可能在每个通知之前使用一个局部变量'r'来设置为0,但很难确定代码是否有限。 – Stonz2

+0

是的,你是对的。谢谢 ! – user3866054

回答

2

看起来要初始化变量r并在收到您的通知时间(尽管很难从你提供的上下文来告诉)listMsgReceived

你不应该这样做,因为每次你插入一个对象时,你会得到一个新的数组 - 因此在每次通知后计数将是1。

你可以尝试移动你的方法之外的数组初始化;将其声明为类中的属性并在初始化程序中初始化它。

+0

是的,你是对的。非常感谢您的回答,并花时间回答我的问题。祝你有美好的一天! – user3866054