Use selected row in another class

This, I think, is the main problem, because stackoverflow is full of it and because of google. But nothing helped me. I need to pass an integer vaule that says which row is selected to another class. Here is my code:

TableViewController.h

#import <UIKit/UIKit.h> @interface TableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> { NSArray *tabledata; } @property (nonatomic, retain) NSArray *tabledata; @property (strong, nonatomic) IBOutlet UITableView *tableView; @property(nonatomic) NSInteger selectedRow; @end 

TableViewController.m

 #import "Instruments.h" @interface Instruments () @end @implementation Instruments @synthesize tabledata; @synthesize tableView; @synthesize selectedRow; - (void)viewDidLoad { [super viewDidLoad]; tabledata = [[NSArray alloc] initWithObjects:@"Row1", @"Row2", @"Row3", nil]; self.selectedRow = 0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [tabledata count]; } - (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; cell = [tableview dequeueReusableCellWithIdentifier:@"Row1"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Row1"]; } cell.textLabel.text = [tabledata objectAtIndex:indexPath.row]; if (indexPath.row == self.selectedRow) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } - (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row != self.selectedRow) { self.selectedRow = indexPath.row; } [tableView reloadData]; } - (void)viewDidUnload { [self setTableView:nil]; [super viewDidUnload]; } @end 

FirstViewController.h

 //Some code which calls my variable 'selectedrow' ... 

FirstViewController.m

 ... if selectedrow = 0 { //some code } if selectedrow = 1 { //some code } if selectedrow = 2 { //some code } ... 

How can I declare my variable 'selectedrow' to use it in FirstViewController?

+1
source share
2 answers

In the MVC world, everything you are going to share between multiple view controllers belongs to the model. Your model class is available worldwide; one controller sets the value; another reads it.

Title: MyAppModel.h

 @interface MyAppModel // <<<=== Use an appropriate name here +(MyAppModel*)instance; @property (readwrite) NSInteger selectedRow; @end 

Implementation: MyAppModel.m

 @implementation MyAppModel +(MyAppModel*)instance { static MyAppModel *statInst; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ statInst = [[MyAppModel alloc] init]; }); return statInst; } @synthesize selectedRow; @end 

Usage: view controllers

 // Set the selected row [MyAppModel instance].selectedRow = indexPath.row; // Access the last selected row switch ([MyAppModel instance].selectedRow) { case 1: // some code break; case 2: // some code break; case 3: // some code break; } 
+1
source

What you do is set a variable for the TableViewController class, but not for the FirstViewController

So, in the TableViewController , when you present the FirstViewController view, also set the value. For instance,

  FirstViewController* newObject = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; newObject.selectedIndex = self.selectedIndex; //Here self indicates the present class which is `TableViewController` //Now whatever your insertion method. ie [self.navigationController pushViewController:newObject animated:Yes]; // or presenting your modal view controller, this depends totally on you. 

Hope this helps! :)

+1
source

All Articles