如何通过indexpath方法的didselect行将表数据从一个视图控制器调用到另一个视图控制器

时间:2013-12-24 05:48:05

标签: ios uitableview

我想执行didselecterowatindexpath方法请让我知道如何使用以下方法从第一个视图控制器调用第二个viewcontroller并将数据传递给其他视图控制器:

我的代码是

-(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 ];

3 个答案:

答案 0 :(得分:1)

试试这个。这是一个最好的选择。 在.h文件中创建方法

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

并在您的.m文件中

- (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;
}

答案 1 :(得分:0)

嗨Mate按照以下步骤操作:

  1. 转到SecViewController.h文件并创建NSMutableArray的属性。

    @property(retain,nonatomic) NSMutableArray *newArray;

  2. tableView:didSelectRowAtIndexPath方法中写

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

  3. 在SecViewController中,在viewDidLoad方法

    中打印以下行

    NSLog(@"%@",newArray);

答案 2 :(得分:0)

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