Xcode - 从Segue传递数据,根据按下的按钮过滤掉数据

时间:2014-08-06 10:13:32

标签: ios uitableview parse-platform segue viewcontroller

我是Xcode的新手。我试图根据按下的按钮通过肌肉类型从Parse.com过滤掉我的数据。例如,如果我按下陷阱按钮,表格视图控制器将仅显示用于训练陷阱的锻炼。非常感谢帮助!

以下是我的编码:

Fitness.h

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

@interface Fitness : UIViewController

@property (nonatomic, strong) NSString *workoutName; // name of workout
@property (nonatomic, strong) NSString *muscleType; // name of workout
@property (nonatomic, strong) NSString *instruction; // name of workout
@property (nonatomic, strong) NSString *repsPerSets; // reps per sets
@property (nonatomic, strong) PFFile *imageFile1; // first image filename of fitness
@property (nonatomic, strong) PFFile *imageFile2; // second image filename of fitness

@end

Fitness.m

#import "Fitness.h"

@implementation Fitness

@synthesize workoutName;
@synthesize muscleType;
@synthesize instruction;
@synthesize repsPerSets;
@synthesize imageFile1;
@synthesize imageFile2;

@end

ViewController.h

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

@interface project124905TViewFitnessController : PFQueryTableViewController

@end

ViewController.m

#import "project124905TViewFitnessController.h"
#import "project124905TViewDetailFitnessController.h"
#import "Fitness.h"
#import <Parse/Parse.h>

@interface project124905TViewFitnessController ()

@end

@implementation project124905TViewFitnessController

- (id)initWithCoder:(NSCoder *)aCoder
{
    self = [super initWithCoder:aCoder];
    if (self) 
    {
        NSLog(@"Testing");
        // The className to query on
        self.parseClassName = @"Workout";

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

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

        // Whether the built-in pagination is enabled
        self.paginationEnabled = NO;
    }
    return self;
}

- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    query.cachePolicy = kPFCachePolicyCacheThenNetwork;

    return query;
}

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

    UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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


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

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

    return cell;
}

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

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


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showWorkoutDetails"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        project124905TViewDetailFitnessController *destViewController = segue.destinationViewController;
        PFObject *object = [self.objects objectAtIndex:indexPath.row];
        Fitness *fitness = [[Fitness alloc] init];
        fitness.muscleType = [object objectForKey:@"MuscleType"];
        fitness.workoutName = [object objectForKey:@"WorkoutName"];
        fitness.instruction = [object objectForKey:@"WorkoutInstruction"];
        fitness.repsPerSets = [object objectForKey:@"RepsPerSets"];
        fitness.imageFile1 = [object objectForKey:@"WorkOutImage1"];
        fitness.imageFile2 = [object objectForKey:@"WorkOutImage2"];
        destViewController.fitness = fitness;
    }
}

@end

FitnessViewController.h

#import <UIKit/UIKit.h>

@interface project124905TViewController : UIViewController

- (IBAction)trapsButton:(id)sender;
- (IBAction)pectoralsButton:(id)sender;
- (IBAction)bicepsButton:(id)sender;
- (IBAction)abdominalsButton:(id)sender;
- (IBAction)quadsButton:(id)sender;
- (IBAction)posteriorButton:(id)sender;
- (IBAction)shouldersButton:(id)sender;
- (IBAction)tricepsButton:(id)sender;
- (IBAction)backButton:(id)sender;
- (IBAction)forearmsButton:(id)sender;
- (IBAction)hamstringsButton:(id)sender;

@end

FitnessViewController.m

#import "project124905TViewController.h"
#import "Fitness.h"
#import "project124905TViewFitnessController.h"

@interface project124905TViewController ()

@end

@implementation project124905TViewController

- (IBAction)trapsButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}

- (IBAction)pectoralsButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}

- (IBAction)bicepsButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}

- (IBAction)abdominalsButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}

- (IBAction)quadsButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}

- (IBAction)posteriorButton:(id)sender 
{
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.80];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                       forView:self.view cache:NO];
    [UIView commitAnimations];

}

- (IBAction)shouldersButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}

- (IBAction)tricepsButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}

- (IBAction)backButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}

- (IBAction)forearmsButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}

- (IBAction)hamstringsButton:(id)sender {
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}



@end

2 个答案:

答案 0 :(得分:0)

假设每个锻炼都有一个&#34; muscleType&#34;分配给它的列,只需在performSegue方法中设置你的muscleType,然后使用queryForTable

- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    [query whereKey:@"muscleType" equalTo:self.muscleType];
    query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    return query;
}

编辑:按要求添加了segue方法

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   //It is common practice to capitalize the first letter of any class
   project124905TViewFitnessController  *vc = segue.destinationViewController;
   vc.muscleType = @"enter your muscle type here";

}

答案 1 :(得分:0)

您可以在prepareForSegue方法中设置muscleType,如下所示

- (IBAction)trapsButton:(id)sender {
    self.muscleType = @"traps";
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}
.
.
.
.

- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender {
    Fitness * fitnessViewController = segue.destinationViewController;
    // set the muscleType property saved when you click the button
    fitnessViewController.muscleType = self.muscleType
}