Krish Krish -5 years ago 138
iOS Question

How to pass NSobject class data from one class to another class in ios

Hi i am new for ios and in my app i have created one NSModel Object for displaying tableList and when i tapped on tableList row i want to take that model class object data to another class

How can i do this please help me some one


NSMutableArray * mainArray;

//TableList Delegate Methods:-

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

return 1;

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

return mainArray.count;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"HistoryCell";

UITableViewCell *cell = (UITableViewCell *)[MaintableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

model1 = mainArray[indexPath.row];
cell.textLabel.text = model1.Name;
cell.detailTextLabel.text = model1.MasterId;

newBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[newBtn setFrame:CGRectMake(250,5,30,30)];
[newBtn addTarget:self action:@selector(urSelctor:) forControlEvents:UIControlEventTouchUpInside];
UIImage *btnImage = [UIImage imageNamed:@"uncheck.png"];
[newBtn setImage:btnImage forState:UIControlStateNormal];
[cell addSubview:newBtn];

return cell;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

ViewController1 *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
//ModelObject1 *model = mainArray[indexPath.row];
model1 = mainArray[indexPath.row];
controller.modelobj = model1;
controller.modelobjArray = mainArray;
[self.navigationController pushViewController:controller animated:YES];

-(void)urSelctor :(UIButton*)sender{

UIImage *currentImage = sender.imageView.image;
isValidate = [currentImage isEqual:[UIImage imageNamed:@"uncheck.png"]]?NO:YES;
NSLog(@"VALUE IS : %@", (isValidate) ? @"YES" : @"NO");
[sender setImage:[UIImage imageNamed:((isValidate) ? @"uncheck.png" : @"check.png")] forState:UIControlStateNormal];

if (isValidate) {
model1.ischeck = YES;
model1.ischeck = NO;



#import "ModelObject1.h"

@implementation ModelObject1

@synthesize MasterId,Name,ischeck;

-(void)loadingservices :(id)mainDictionary{

NSLog(@"loadingservices %@",mainDictionary);

Name = [mainDictionary objectForKey:@"Name"];
MasterId = [mainDictionary objectForKey:@"MasterId"];



#import "ViewController1.h"

@interface ViewController1 ()


@implementation ViewController1
@synthesize modelobj,modelobjArray;

- (void)viewDidLoad {
[super viewDidLoad];

for (int i=0; i<modelobjArray.count;++i) {

modelobj = modelobjArray[i];
NSLog(@"Name is=======>%@",modelobj.Name);
NSLog(@"Check value is%hhd",modelobj.ischeck);

Answer Source

Create property of your model class into destination view controller and in source controller, create the object of destination controller and set the value of the model property.

Modify your method like this:

-(void)urSelctor :(UIButton*)sender{
    model1 = mainArray[indexPath.row];
    if (model1.ischeck) 
        model1.ischeck = NO;
     [sender setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
        model1.ischeck = YES;
       [sender setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download