简体   繁体   中英

how to call table data from one view controller to another via didselect row at indexpath method

I want to perform didselecterowatindexpath method please let me know how to call second viewcontroller from first view controller using below method and passing the data to other view controller :

My code is

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
pun=[tableData objectAtIndex:indexPath.row];

     NSLog(@"PUN IS :%@",pun);
     appDelegate.matri=pun;
     NSLog(@"matriC:%@",appDelegate.matri);
     SecViewController *SecView = [[SecViewController alloc] initWithNibName:@"SecViewController" bundle:nil];

     [self.navigationController pushViewController:SecView animated:YES ];

Try this. its a best option. In your .h file create a method

- (id)initWithNibName:(NSString *)nibNameOrNil Yourvariable:(yourdatatype *)variable Yourvariablex:(yourdatatype *)variablex bundle:(NSBundle *)nibBundleOrNil;

and in your .m file

- (id)initWithNibName:(NSString *)nibNameOrNil Yourvariable:(yourdatatype *)variable Yourvariablex:(yourdatatype *)variablex bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //assign or use data here.

    }
    return self;
}

Hi Mate follow below steps :

  1. Goto SecViewController.h file and create a property of NSMutableArray.

    @property(retain,nonatomic) NSMutableArray *newArray;

  2. In tableView:didSelectRowAtIndexPath method write

    SecViewController *secView = [[SecViewController alloc] initWithNibName:@"SecViewController" bundle:nil]; secView.newArray = [tableData objectAtIndex:indexPath.row]; [self.navigationController pushViewController:secView animated:YES ];

  3. In SecViewController print the below line in viewDidLoad Method

    NSLog(@"%@",newArray);

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 NSString *tempString =[tableData objectAtIndex:indexPath.row];    
 NSLog(@"PUN IS :%@", tempString);
 SecViewController *SecView = [[SecViewController alloc] initWithNibName:@"SecViewController" bundle:nil];
 SecView.usernameString= tempString;
 [self.navigationController pushViewController:SecView animated:YES ];
 }

//  .h file of SecViewController
 #import <UIKit/UIKit.h>

@interface SecViewController : UIViewController
@property (nonatomic,strong) NSString *usernameString;
@end

// .m file of SecViewController

#import "ViewController.h"

@interface SecViewController ()
{

}
@end

@implementation SecViewController
@synthesize usernameString;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM