2013-12-08 1 views
-1

У меня есть класс, к которому можно получить доступ со всех других страниц, так же как и в facebook chat head bubble.Its cart в моем приложении. Это может быть добавлено в корзину из разных представлений. ? Пытался с NSNotification и не working.The массива внутри вида корзинки Джеттиг, как null.Any помочь Что я попытался это:установка массива из другого представления в объекте c

-(IBAction)addToCart:(id)sender{ 
    NSMutableArray *itemToCart=[[NSMutableArray alloc]init]; 
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:gearImgView.image forKey:@"equipImage"]; 
    [dict setObject:nameLbl.text forKey:@"name"]; 
    [dict setObject:priceLbl.text forKey:@"price"]; 
    [dict setObject:self.shopifyIDString forKey:@"shopifyID"]; 
    [itemToCart addObject:dict]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"equipmentSelected" object:nil userInfo:dict]; 
} 

И в представлении корзины,

- (void)viewDidLoad 
    { 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadEquipmentview:) name:@"equipmentSelected" object:nil]; 
    [super viewDidLoad]; 
    } 

-(void)loadEquipmentview:(NSNotification *)notification{ 

    NSDictionary *dict = [notification userInfo]; 

    NSString *shopifyID = [dict objectForKey:@"shopifyID"]; 

     NSLog(@"%@",dict); 
} 
+0

смог подтвердить, что словарь не ноль Вы, прежде чем отправлять уведомление? Лучший подход может заключаться в том, чтобы иметь класс singleton Cart, который содержит массив всех приобретенных продуктов, поскольку это лучше разделяет модель и представление. –

+0

Да, я могу напечатать это правильно, прежде чем отправлять уведомление. –

ответ

0

Если вы не используются ARC, вы можете изменить следующую строку:

NSDictionary *dict = [notification userInfo]; 

To;

NSDictionary *dict = [notification userInfo] retain]; 
0

Ваш массив существует только в области метода addCart. попробуйте так:

-(IBAction)addToCart:(id)sender{ 
    NSMutableArray *itemToCart=[[NSMutableArray alloc]init]; 
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:gearImgView.image forKey:@"equipImage"]; 
    [dict setObject:nameLbl.text forKey:@"name"]; 
    [dict setObject:priceLbl.text forKey:@"price"]; 
    [dict setObject:self.shopifyIDString forKey:@"shopifyID"]; 

    [itemToCart addObject:dict]; 

    // Create an other dictionnary to hold the array of dictionary 
    NSMutableDictionary *otherDict = [NSMutableDictionary dictionary]; 
    otherDict[@"yourArrayKey"] = itemToCart; 

// The you post the otherDict 
[[NSNotificationCenter defaultCenter] postNotificationName:@"equipmentSelected" object:nil userInfo:otherDict]; //be careful here, you should post the otherDict and not the dic. 
} 

и вам получить массив, вы можете сделать так:

-(void)loadEquipmentview:(NSNotification *)notification{ 

    NSDictionary *dict = [notification userInfo]; 
    NSMutableArray *array = dic[@"yourArrayKey"]; 
    .... 
} 
Смежные вопросы