Segue不会设置数据

时间:2013-11-18 08:03:03

标签: ios iphone objective-c segue

我在MainViewControllerBillTrackerViewController之间有一个segue,它们都是UIViewController个类,但当我尝试在BillTrackerViewController上设置名为event_name的@property时来自MainViewController,该属性保持为空。

由于此属性,我无法使用Parse进行属性查询,如下面的代码所示,应用程序最终崩溃。

MainViewController的代码(有问题的segue名称是“toBillTracker”):

#import "MenuViewController.h"
#import "AgendaViewController.h"
#import "BillTrackerViewController.h"
#import "ResearchViewController.h"

#import "Parse/Parse.h"

@interface MenuViewController ()

@end

@implementation MenuViewController {}

@synthesize event_name_label;
@synthesize event_name;

- (void)viewDidLoad
{
event_name_label.text = event_name;
[super viewDidLoad];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"toBillTracker"]) {
    BillTrackerViewController *controller=(BillTrackerViewController *)segue.destinationViewController;
    controller.event_name = event_name;
}
}

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

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

@end

以下是BillTrackerViewController.m的代码:

#import "BillTrackerViewController.h"
#import "Parse/Parse.h"

@interface BillTrackerViewController ()

@end

@implementation BillTrackerViewController

@synthesize billView;
@synthesize refreshButton;

@synthesize event_name;
@synthesize event_id;

@synthesize resolution_names;
@synthesize resolution_pro_speakers;
@synthesize resolution_con_speakers;
@synthesize resolution_results;

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

- (IBAction)loadResolutions:(id)sender
{
resolution_names = [[NSMutableArray alloc] init];
resolution_pro_speakers = [[NSMutableArray alloc] init];
resolution_con_speakers = [[NSMutableArray alloc] init];
resolution_results = [[NSMutableArray alloc] init];

// Find event_id of the event
PFQuery *event_id_query = [PFQuery queryWithClassName:@"Event"];
[event_id_query whereKey:@"event_name" equalTo:event_name];
[event_id_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for (PFObject *object in objects) {
            event_id = object.objectId;
        }
    } else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

// Now, use that event_id to locate all of its resolutions
PFQuery *resolution_query = [PFQuery queryWithClassName:@"Resolution"];
[resolution_query whereKey:@"event_id" equalTo:event_id];
[resolution_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for (PFObject *object in objects) {
            [resolution_names addObject:[object objectForKey:@"resolution_name"]];

            // Sort resolutions in alphabetical order
            resolution_names = (NSMutableArray*)[resolution_names sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
        }

        // After sorting alphabetically, load other resolution data (pro speaker, result, etc.)
        for (NSString *resolution_name in resolution_names) {
            PFQuery *query = [PFQuery queryWithClassName:@"Resolution"];
            [query whereKey:@"resolution_name" equalTo:resolution_name];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                if (!error) {
                    for (PFObject *object in objects) {
                        [resolution_pro_speakers addObject:[object objectForKey:@"resolution_pro_speaker"]];
                        [resolution_con_speakers addObject:[object objectForKey:@"resolution_con_speaker"]];
                        switch ((int)[object objectForKey:@"resolution_result"]) {
                            case 0:
                                [resolution_results addObject:@"In Progress"];
                            case 1:
                                [resolution_results addObject:@"Passed"];
                            case 2:
                                [resolution_results addObject:@"Failed"];
                            case 3:
                                [resolution_results addObject:@"Passed with Amendment(s)"];
                            default:
                                [resolution_results addObject:@"In Progress"];
                        }
                    }
                } else {
                    NSLog(@"Error: %@ %@", error, [error userInfo]);
                }
            }];
        }
    } else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];
}

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

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [resolution_names count];
}

- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

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

NSString *cellText = [NSString stringWithFormat:@"%@\nPro: %@\nCon: %@\nResult: %@",resolution_names[indexPath.row], resolution_pro_speakers[indexPath.row], resolution_con_speakers[indexPath.row], resolution_results[indexPath.row]];

cell.textLabel.text = cellText;

return cell;
}

@end

非常感谢!

0 个答案:

没有答案