【发布时间】: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