iOS:需要数据通信帮助

时间:2016-01-28 06:05:54

标签: ios model-view-controller uistoryboardsegue

我正在关注MVC模式进行数据通信,我不知道究竟发生了什么问题。需要解决这个问题。 情景是:
我有一类 ListingItems ,即:model


#import "Listing_Ads.h"

@implementation Listing_Ads


#pragma mark -
#pragma mark Properties Synthesized
@synthesize     item_id         =   _item_id;
@synthesize     title_value     =   _title_value;
@synthesize     desc_value      =   _desc_value;
@synthesize     kategory        =   _kategory;
@synthesize     imagesAtIndex   =   _imagesAtIndex;

@synthesize     reviews         =   _reviews;


#pragma mark -
#pragma mark Data_Allocation
- (id)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];

    if (self)
    {
        self.item_id            =   [dictionary valueForKey:@"id"];
        self.title_value        =   [dictionary  valueForKey:@"title"];
        self.desc_value         =   [dictionary  valueForKey:@"description"];
        if ([[dictionary valueForKey:@"my_images"] isKindOfClass:[NSNull class]])
        {
            self.image          =  @"";
        }
        else
        {
            NSString * rawImages;

            rawImages           = [dictionary valueForKey:@"my_images"];
            self.imagesAtIndex  = [rawImages componentsSeparatedByString:@","];
            self.image          =   self.imagesAtIndex[0];
        }

            self.kategory       =   [dictionary  valueForKey:@"category"];
    }

            self.reviews        =   [dictionary valueForKey:@"reviews"];

    return self;
}

-(NSDictionary*)details_Listing

{
    NSDictionary * detailsDictionary= [NSDictionary dictionary];


    [detailsDictionary setValue:self.item_id forKey:@"itemID"];
    [detailsDictionary setValue:self.title_value forKey:@"title"];
    [detailsDictionary setValue:self.desc_value forKey:@"description"];
    [detailsDictionary setValue:self.imagesAtIndex forKey:@"images"];


    NSLog(@"details of product are here:%@",detailsDictionary);

    return detailsDictionary;
}


我在第一个视图控制器中发出请求并获取正在显示的所有结果。但我无法将值传递给详细视图控制器
在FirstVC.h中

@property (nonatomic, retain) NSMutableArray * ads_Array;
在FirstVC.m

[manager GET:self.urlString parameters:nil progress:nil success:^(NSURLSessionTask * task, id responseObject)
 {

     NSLog(@"Response for #%d request is: %@",_currentPage,responseObject);

     _totalPages = [[responseObject objectForKey:@"total_pages"]integerValue];


     for (int i = 0;i<[[responseObject valueForKey:@"items"]count];i++)

     {
         Listing_Ads * ads = [[Listing_Ads alloc] initWithDictionary:[responseObject objectForKey:@"items"][i]];
         if (![self.ads_Array containsObject:ads])
         {
             [self.ads_Array addObject:ads];
         }
     }



在FirstVC.m传递数据在这里..
不确定该怎么做......如果我错了,请纠正我..

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Listing_Ads * selectedItem = self.ads_Array[indexPath.row];


        NSLog(@"ads%@",selectedItem);

        UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        DetailsVC * vc = [storyboard instantiateViewControllerWithIdentifier:@"DetailsVC"];

        [self presentViewController:vc animated:YES completion:nil];

    }



在DetailsVC.h中

#import "Listing_Ads.h"
@class Listing_Ads;

@interface DetailsVC : NavigationHandler<UITableViewDataSource,UITableViewDelegate>

@property(strong,nonatomic) Listing_Ads * detailsListing;

@end


在DetailVC.m我有

@interface DetailsVC ()
@end
@implementation DetailsVC

- (void)viewDidLoad
{
    // Update the user interface for the detail vehicle, if it exists.
    if (self.detailsListing)
    {
        //Set the View Controller title, which will display in the Navigation bar.
        NSLog(@"All Values here:%@",[self.detailsListing details_Listing]);
    }
}

1 个答案:

答案 0 :(得分:0)

这样做,你甚至没有传递价值: -

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        DetailsVC * vc = [storyboard instantiateViewControllerWithIdentifier:@"DetailsVC"];
         //you missed this line-- pass value
         vc.detailsListing= [[Listing_Ads alloc] initWithDictionary:self.ads_Array[indexPath.row]];
        [self presentViewController:vc animated:YES completion:nil];

    }