Krish
Krish

Reputation: 4228

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 tap on tableList row I want to take that model class object data to another class.

How can I do this?

Viewcontroller:-

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;
    }else{
        model1.ischeck = NO;
    }
 }

@end

Modelobject1:-

#import "ModelObject1.h"

@implementation ModelObject1

@synthesize MasterId,Name,ischeck;

-(void)loadingservices :(id)mainDictionary{

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

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

@end

Viewcontroller1:-

#import "ViewController1.h"

    @interface ViewController1 ()

    @end

    @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);
        }
     }

Upvotes: 1

Views: 818

Answers (3)

TheHungryCub
TheHungryCub

Reputation: 1960

Try this,May be this is helpful to you..:)

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

        ViewController1 *fController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
        model1 = mainArray[indexPath.row];
        fController.modelVal = model1;
        [self.navigationController pushViewController:Controller animated:YES];
    }

In next view controller:

declare the property

@property (strong, nonatomic)modelName *modelVal;

In .m file

in viewDidLoad:

NSLog(@"%@",modelVal.Name);

For handling the button use this:

-(void)checkboxSelected:(id)sender {
    model1 = mainArray[indexPath.row];

if([checkbox1 isSelected]==YES) {
    [checkbox1 setSelected:NO];
} else {
    [checkbox1 setSelected:YES];
}

Upvotes: 0

Bhadresh Mulsaniya
Bhadresh Mulsaniya

Reputation: 2640

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];
    }
    else
    {
        model1.ischeck = YES;
       [sender setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    }
 }

Upvotes: 1

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16436

Follow this

in your destination view Controller .h file create property like this

#import <UIKit/UIKit.h>
#import "YourObjectClass.h"

@interface DestinationView : UIViewController

@property (strong, nonatomic) YourObjectClass           *objectValue;




@end

and when you need to pass then

 DestinationView *newObjc = // Create object of class

and after that

  newObjc.objectValue = yourObjectValue;

hope it helps

Upvotes: 0

Related Questions