【问题标题】:Sending a data model between UITableViewControllers在 UITableViewControllers 之间发送数据模型
【发布时间】:2013-11-28 05:19:49
【问题描述】:

我知道已经有很多关于此的问题,但没有一个对我有帮助。我有两个 UITableViewControllers。我想将 QuicklistViewController(使用它像一个快速菜单来列出不同的节目)推送到 CurrentShowViewController。

我现在有一个仅包含 name 属性的 Show 模型。我无法将数据从我在 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];
}

【问题讨论】:

    标签: ios objective-c uitableview xcode5


    【解决方案1】:

    使用委托。

    CurrentShowViewController

    - (void) quicklistButtonPressed { QuicklistViewController *quicklistVC = [[QuicklistViewController alloc] init]; quicklistVC.delegate = self; [self.navigationController pushViewController:quicklistVC Animation:YES]; } - (void) selectedShow:(显示) 显示 { // 显示:当前显示 }

    快速列表视图控制器

    @protocol QuicklistViewControllerDelegate - (void) selectedShow:(显示) 显示 @结尾 @property (nonatomic, weak) id 代表; - (void) backButtonPressed { //存档对currentShow对象的更改 [显示 saveShow:self.currentShow]; [委托 selectedShow:self.currentShow]; [self.navigationController popViewControllerAnimated:YES]; }

    【讨论】:

    • 非常感谢!我能够让它工作。我不知道如何正确使用委托,这对我很有帮助!
    【解决方案2】:

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

    【讨论】:

    • 谢谢你,你是对的,不敢相信我没有注意到!
    猜你喜欢
    • 2017-05-03
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 2012-07-15
    • 2016-08-12
    相关资源
    最近更新 更多