-1

In my ios app I have created one NSModel Object for displaying tableList and when I tapped on tableList row I want to take that particular model class object data to another class

How can I do this?

my code:

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";

    // Similar to UITableViewCell, but
    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;

    return cell;
}

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

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

@end

ModelObject1:

#import "ModelObject1.h"

@implementation ModelObject1

@synthesize MasterId,Name;

-(void)loadingservices :(id)mainDictionary{

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

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

@end
Cœur
  • 37,241
  • 25
  • 195
  • 267
Krish
  • 4,166
  • 11
  • 58
  • 110

3 Answers3

0

Try like below:

1) Create one model object in your ViewController1

 @property (strong,nonatomic) ModelObject1 *modelObj;

2) Write below code in viewDidLoad of ViewController1,

 modelObj = [[ModelObject1 alloc]init];

Then Replace your didSelect method like this,

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

    ViewController1 *Controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
    Controller.indexPosition = indexPath.row;
    ModelObject1 *model = mainArray[indexPath.row];
    Controller.modelObj = model;
    [self.navigationController pushViewController:Controller animated:YES];
}

Then get you data from model class in viewDidLoad of ViewController1

e.g, NSLog("%@", modelObj.Name);

This way you can pass model class object to other view controller. Hope this will help you.

Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
0

Create one model object in your Destination View controller

@property (strong,nonatomic) ModelClass *job;

in your did select method get that model data and pass it

ModelClass *SelectedJob;
SelectedJob = [muarrJobs objectAtIndex:indexPath.row];

JobDescriptionViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"JobDescription"];
    obj.job = SelectedJob;
    [self.navigationController pushViewController:obj animated:YES];
Viraj Padsala
  • 1,388
  • 1
  • 13
  • 31
0

Use this Code,

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

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

Another ViewControllerB:

ViewControllerB.h

declare the property Value

@property (strong, nonatomic)modelName *modelVal;

ViewControllerB.m

print the name value in viewDidLoad:

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

hope its helpful

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30