Xcode - 使用segue标识符查询parse.com数据库

时间:2014-09-03 18:18:47

标签: xcode menu parse-platform segue

我是Xcode的新手(请善待),我正在开发一个使用parse.com作为后端的汽车销售应用程序。我发现了一个很棒的教程并根据我的需要进行了修改。

但是,我想在前端有一个菜单,用户可以在其中选择他们感兴趣的汽车的“系列”,并从菜单和表视图控制器之间的segue使用它来过滤解析。 com查询。

我尝试了很多方法,但似乎都没有用,有人可以帮助我。

我在故事板中创建了3个视图控制器:menu - > tableViewController - >视图控制器。

我目前的代码如下:

Car.h

#import <Foundation/Foundation.h>
#import <Parse/Parse.h>

@interface Car : UIViewController

@property (nonatomic, strong) NSString *model;
@property (nonatomic, strong) NSString *price;
@property (nonatomic, strong) PFFile *Photo1;
@property (nonatomic, strong) NSString *mileage;
@property (nonatomic, strong) NSString *year;
@property (nonatomic, strong) NSString *series;
@property (nonatomic, strong) NSString *BodyType;

@end

Car.m

#import "Car.h"

@implementation Car

@synthesize model;
@synthesize price;
@synthesize Photo1;
@synthesize mileage;
@synthesize year;
@synthesize series;
@synthesize BodyType;

@end

MenuViewController.h

#import <UIKit/UIKit.h>

@interface MenuViewController : UIViewController

- (IBAction)serieone:(id)sender;
- (IBAction)serietwo:(id)sender;
- (IBAction)seriethree:(id)sender;
- (IBAction)seriefour:(id)sender;
- (IBAction)seriefive:(id)sender;
- (IBAction)seriesix:(id)sender;

@end

MenuViewController.m

#import "Car.h"
#import "MenuViewController.h"
#import "CarTableViewController.h"

@interface MenuViewController ()

@end

@implementation MenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)serieone:(id)sender {
// self.series = @"1 Series"
// [self performSegueWithIdentifier:@"serieOneSegue" sender:self];
}

- (IBAction)serietwo:(id)sender {
.
.
// Other series types repeated
.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Car * CarTableViewController = segue.destinationViewController;
// CarTableViewController.series = self.series
}

    }
@end

CarTableViewController.h

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "Car.h"

@interface CarTableViewController : PFQueryTableViewController

@end

CarTableViewController.m

#import "CarTableViewController.h"
#import "CarDetailViewController.h"
#import "Car.h"
#import "MenuViewController.h"

@interface CarTableViewController ()

@end

@implementation CarTableViewController {

}

- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
    // Custom the table

    // The className to query on
    self.parseClassName = @"Car";

    // The key of the PFObject to display in the label of the default cell style
    self.textKey = @"Model";

    // Whether the built-in pull-to-refresh is enabled
    self.pullToRefreshEnabled = YES;

    // Whether the built-in pagination is enabled
    self.paginationEnabled = YES;

    // The number of objects to show per page
    self.objectsPerPage = 10;
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}


- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (PFQuery *)queryForTable
{

PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

// need to replace the equal to as a variable which is provided from the button selector in MenuViewController

//[query whereKey:@"Series" equalTo:result];

query.cachePolicy = kPFCachePolicyCacheThenNetwork;

// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
/*    if ([self.objects count] == 0) {

 }*/

//    [query orderByAscending:@"name"];

return query;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = @"CarCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

// Configure the cell
PFFile *thumbnail = [object objectForKey:@"Photo1"];
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.image = [UIImage imageNamed:@"placeholder.jpg"];
thumbnailImageView.file = thumbnail;
[thumbnailImageView loadInBackground];

UILabel *modelLabel = (UILabel*) [cell viewWithTag:101];
modelLabel.text = [object objectForKey:@"Model"];

UILabel *priceLabel = (UILabel*) [cell viewWithTag:102];
priceLabel.text = [object objectForKey:@"Price"];

UILabel *mileageLabel = (UILabel*) [cell viewWithTag:103];
mileageLabel.text = [object objectForKey:@"Mileage"];

UILabel *yearLabel = (UILabel*) [cell viewWithTag:104];
yearLabel.text = [object objectForKey:@"Year"];

UILabel *seriesLabel = (UILabel*) [cell viewWithTag:105];
seriesLabel.text = [object objectForKey:@"Series"];

UILabel *bodytypeLabel = (UILabel*) [cell viewWithTag:106];
bodytypeLabel.text = [object objectForKey:@"BodyType"];

return cell;
}

- (void) objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];

NSLog(@"error: %@", [error localizedDescription]);
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showCarDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    CarDetailViewController *destViewController = segue.destinationViewController;

    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    Car *car = [[Car alloc] init];
    car.model = [object objectForKey:@"Model"];
    car.photo1 = [object objectForKey:@"Photo1"];
    car.price = [object objectForKey:@"Price"];
    car.series = [object objectForKey:@"Series"];
    car.derivative = [object objectForKey:@"BodyType"];
    car.mileage = [object objectForKey:@"Mileage"];

    destViewController.car = car;

}

}

@end

CarDetailViewController.h

#import <UIKit/UIKit.h>
#import "Car.h"

@interface CarDetailViewController : UIViewController

@property (weak, nonatomic) IBOutlet PFImageView *mainPhoto;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *seriesLabel;
@property (weak, nonatomic) IBOutlet UILabel *modelLabel;
@property (weak, nonatomic) IBOutlet UILabel *deivativeLabel;
@property (weak, nonatomic) IBOutlet UILabel *mileageLabel;

@property (nonatomic, strong) Car *car;

@end

CarDetailViewController.m

#import "CarDetailViewController.h"

@interface CarDetailViewController ()

@end

@implementation CarDetailViewController

@synthesize mainPhoto;
@synthesize priceLabel;
@synthesize seriesLabel;
@synthesize modelLabel;
@synthesize deivativeLabel;

@synthesize car;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.title = car.model;
self.priceLabel.text = car.price;
self.seriesLabel.text = car.series;
self.modelLabel.text = car.model;
self.deivativeLabel.text = car.derivative;
self.mileageLabel.text = car.mileage;
self.mainPhoto.file = car.Photo1;

- (void) viewDidUnload
{
[self setMainPhoto:nil];

[self setPriceLabel:nil];
[super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end

0 个答案:

没有答案