在UITableViewControllers之间发送数据模型

时间:2013-11-28 05:19:50

标签: ios objective-c uitableview xcode5

我知道已经有很多关于这方面的问题,但他们中没有人帮助过我。我有两个UITableViewControllers。我想将一个QuicklistViewController(使用它像快速菜单列出不同的节目)推送到CurrentShowViewController上。

我有一个Show模型,它现在只包含一个name属性。我无法将数据从QuicklistViewController中的选择移动到CurrentShowViewController,以便我可以在那里显示currentShow信息。

我完全不知道如何做到这一点。请帮忙!

这是我的CurrentShowViewController:

#import "CurrentShowViewController.h"
#import "QuicklistViewController.h"


@interface CurrentShowViewController ()

@end

@implementation CurrentShowViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        self.tabBarItem.image = [UIImage imageNamed:@"tab_icon_episodes.png"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    //self.navigationItem.rightBarButtonItem = self.editButtonItem;
    // Add a UIBarButton button that will display a Quicklist Modal View
    UIBarButtonItem *quicklistButton = [[UIBarButtonItem alloc] initWithTitle:@"Quicklist"
                                                                        style:UIBarButtonItemStylePlain
                                                                       target:self
                                                                       action:@selector(quicklistButtonPressed)];
    self.navigationItem.leftBarButtonItem = quicklistButton;
    self.currentShow = [[Show alloc] init];
    NSLog(@"Current show name is: %@", self.currentShow.name);
    self.title = self.currentShow.name;

}

- (void) viewWillAppear:(BOOL)animated {
    self.title = self.currentShow.name;
}

- (void) quicklistButtonPressed {
    QuicklistViewController *quicklistVC = [[QuicklistViewController alloc] init];
    [self.navigationController pushViewController:quicklistVC animated:YES];
}

这是我的QuicklistViewController:

#import "QuicklistViewController.h"
#import "CurrentShowViewController.h"

@interface QuicklistViewController ()

@end

@implementation QuicklistViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        self.title = @"Quicklist";
        self.tabBarItem.image = [UIImage imageNamed:@"tab_icon_quicklist.png"];

        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                       style:UIBarButtonItemStylePlain
                                                                      target:self
                                                                      action:@selector(backButtonPressed)];
        self.navigationItem.leftBarButtonItem = backButton;

        // Create a temp array of Show objects filled with test data
        NSMutableArray *temp= [[NSMutableArray alloc] init];
        [temp addObject:[[Show alloc] initWithName:@"The Walking Dead"]];
        [temp addObject:[[Show alloc] initWithName:@"How I Met Your Mother"]];
        [temp addObject:[[Show alloc] initWithName:@"Grey's Anatomy"]];
        [temp addObject:[[Show alloc] initWithName:@"The Mentalist"]];
        [temp addObject:[[Show alloc] initWithName:@"Stargate SG1"]];

        NSArray *testShows = [[NSArray alloc] initWithArray:temp];
        self.shows = testShows;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.currentShow = [[Show alloc] init];
}

- (void) backButtonPressed {
    //Archive changes to currentShow object
    [Show saveShow:self.currentShow];
   [self.navigationController popViewControllerAnimated:YES];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [self.shows count];
}

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

    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    cell.textLabel.text = [self.shows[indexPath.row] name];

    return cell;

}

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // Change the current show to the selected show
    self.currentShow = self.shows[indexPath.row];

    //Archive changes to currentShow object
    [Show saveShow:self.currentShow];
}

2 个答案:

答案 0 :(得分:0)

你错误地实现了didDeselectRowAtIndexPath而不是didSelectRowAtIndexPath。

答案 1 :(得分:0)

使用委托。

CurrentShowViewController

    - (void) quicklistButtonPressed
    {
        QuicklistViewController *quicklistVC = [[QuicklistViewController alloc] init];
        quicklistVC.delegate = self;
        [self.navigationController pushViewController:quicklistVC animated:YES];
    }

    - (void) selectedShow:(Show) show
    {
          // show : current show
    }

QuicklistViewController

    @protocol QuicklistViewControllerDelegate 
    - (void) selectedShow:(Show) show
    @end

    @property (nonatomic, weak) id delegate;

    - (void) backButtonPressed
    {
        //Archive changes to currentShow object
        [Show saveShow:self.currentShow];
        [delegate selectedShow:self.currentShow];
       [self.navigationController popViewControllerAnimated:YES];
    }