Note that there are some explanatory texts on larger screens.

plurals
  1. POSending a data model between UITableViewControllers
    primarykey
    data
    text
    <p>I know there are already lots of questions about this but none of them have helped me yet. I have two UITableViewControllers. I want to push a QuicklistViewController (using this like a quick menu to list different shows) onto a CurrentShowViewController. </p> <p>I have a Show model that only contains a name property right now. I am have trouble moving the data from my selection in QuicklistViewController to the CurrentShowViewController so that I can display the currentShow information there.</p> <p>I am totally stuck on how I can do this.Please help!</p> <p>Here is my CurrentShowViewController:</p> <pre><code>#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]; } </code></pre> <p>Here is my QuicklistViewController:</p> <pre><code>#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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload