【问题标题】:iOS :data communication help requirediOS:需要数据通信帮助
【发布时间】:2016-01-28 06:05:53
【问题描述】:

我正在遵循 MVC 模式进行数据通信,我不知道到底发生了什么问题。需要解决这个问题。 场景是:
我有一类 ListingItems 即:模型


#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]);
    }
}

【问题讨论】:

  • Listing_Ads 是 NSObject 类还是 UIViewController ?
  • 您错过了两个主要代码:- 首先是在您存储对象时,其次将对象传递给另一个视图控制器。向我们展示代码。
  • Listing_Ads 是 NSObject 类
  • @Vizllx 我已经更新了代码 sn-p,希望现在有意义..
  • @magid 您错过了第二点...您如何将值从 FirstVC 传递给 DetailVC?代码在哪里?

标签: ios model-view-controller uistoryboardsegue


【解决方案1】:

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

 -(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];

    }

【讨论】:

  • 我这样做了,但应用程序崩溃了......不知何故......!
  • 你知道什么是调试吗?如何在 XCode 中设置断点。你知道如何查看崩溃报告吗?如果不是谷歌它;)
  • 请告诉我,initWithDictionary 你是如何给它赋值的......?
  • vc.detailsListing= [[Listing_Ads alloc] initWithDictionary:self.ads_Array[indexPath.row]];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 2016-08-06
  • 2021-10-05
相关资源
最近更新 更多