As you can tell by my question I am new to Objective C. I can access static properties such as NSString but it seems like I am unable to access arrays or even other properties, must be doing something wrong. Basically, I have a Model file with my main properties.
Here is my model file called ListingManager.h
#import <Foundation/Foundation.h>
@interface ListingManager : NSObject
@property (nonatomic, strong) NSString *listingTitle;
@property (nonatomic, strong) NSString *listingDescription;
@property (nonatomic, strong) NSString *listingPrice;
@property (nonatomic, readonly) NSString *datePosted;
@property (nonatomic, retain) NSMutableArray *lists;
@property (nonatomic) BOOL *viewed;
@end
ListingManager.m
#import "ListingManager.h"
@implementation ListingManager
@synthesize lists;
@synthesize listingPrice;
@synthesize listingDescription;
@synthesize listingTitle;
@synthesize datePosted;
@end
Here is another view controller where I am using it ListingTableViewController.m
#import "ListingTableViewController.h"
@interface ListingTableViewController ()
@property (nonatomic, strong) ListingManager *manageListings;
@end
@implementation ListingTableViewController
@synthesize manageListings;
- (void) loadInitialData {
ListingManager *listing1 = [[ListingManager alloc] init];
listing1.listingTitle = @"Title here";
listing1.listingDescription = @"another description..Whatever it is";
listing1.listingPrice = @"100";
[self.manageListings.lists addObject:listing1];
NSLog(@"%@", listing1); //for debugging purposes
ListingManager *listing2 = [[ListingManager alloc] init];
listing2.listingTitle = @"Title here";
listing2.listingDescription = @"a long description here";
listing2.listingPrice = @"50";
[self.manageListings.lists addObject:listing2];
ListingManager *listing3 = [[ListingManager alloc] init];
listing3.listingTitle = @"Cool stuff";
listing3.listingDescription = @"Another descitpion righ there";
listing3.listingPrice = @"90";
[self.manageListings.lists addObject:listing3];
}
- (instancetype)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//self.listings= [[NSMutableArray alloc] init];
self.manageListings = [[ListingManager alloc] init];
[self.backdropTableView reloadData]; //reload table view
[self loadInitialData]; //load inital static data
}
/*
- (void)viewWillAppear:(BOOL)animated
{
[self.backdropTableView reloadData];
}
*/
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.manageListings.lists count]; //this here is not returning anything
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *custom = @"WXCustomCell";
WXTableCell *cell = (WXTableCell *)[tableView dequeueReusableCellWithIdentifier:custom];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WXCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
ListingManager *listingManager = [self.manageListings.lists objectAtIndex:indexPath.row];
cell.titleLabel.text = listingManager.listingTitle;
cell.descriptionlabel.text = listingManager.listingDescription;
cell.thumbnail.image = [UIImage imageNamed:@"nothumb.gif"];
cell.price.text = @"$50";
cell.dateLabel.text = @"N/A";
NSLog(@"%@", cell.titleLabel.text);
//use for later
/*
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"MM/dd/yyy HH:mm:ss"];
NSLog(@"%@",[DateFormatter stringFromDate:[NSDate date]]);
*/
return cell;
}
//Call segue
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"showDetail" sender:nil];
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showDetail"]) {
// Get the new view controller using [segue destinationViewController].
NSIndexPath *indexPath = [self.backdropTableView indexPathForSelectedRow];
DetailsViewController *vc = segue.destinationViewController;
// Pass the selected object to the new view controller.
ListingManager *listingManager = [self.manageListings.lists objectAtIndex:indexPath.row];
vc.titleName = listingManager.listingTitle;
vc.descriptionDetail = listingManager.listingDescription;
}
}
@end
For example when I return '[self.manageListings.lists count]', it doesn't return anything, if I hardcode it to 3 though, I get the 3 initial items, with the title, description, and title being null. There must be something I am doing wrong connecting my model and VC.