1

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);
        }
     }
Luuklag
  • 3,897
  • 11
  • 38
  • 57
Krish
  • 4,166
  • 11
  • 58
  • 110
  • Hi, you can prefer to this link: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 – Son Le Jun 14 '16 at 09:18
  • no no my question is diff compared to this – Krish Jun 14 '16 at 09:18
  • The way you wrote the question really looks like what @Son Le has provided. Did you look at this section of that link, 'Passing Data Forward using Segues' ? – Ro4ch Jun 14 '16 at 09:22

3 Answers3

1

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];
    }
 }
Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
0

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

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • above code i have added check box on all rows only that check information not going correctly – Krish Jun 14 '16 at 09:39
  • here when i check some check boxes in viewcontroller tableList and when i goes to Viewcontroller1 i want to show there which are checkd and which are unchecked,This is my main intection – Krish Jun 14 '16 at 09:39
0

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];
}
Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
  • above code i have added check box on all rows only that check information not going correctly – Krish Jun 14 '16 at 09:39
  • here when i check some check boxes in viewcontroller tableList and when i goes to Viewcontroller1 i want to show there which are checkd and which are unchecked,This is my main intection – Krish Jun 14 '16 at 09:40
  • did you check NSLog(@"%@", model1.Name); ..? – Suraj Sukale Jun 14 '16 at 09:44
  • name is going perfectly but check box is checked or not checked not going perfectly – Krish Jun 14 '16 at 09:45
  • if name is going perfectly then whats the issue now.?... just update the value of 'ischeck' key on your selection of table row. – Suraj Sukale Jun 14 '16 at 09:53