繁体   English   中英

如何通过索引路径方法中的didselect行从一个视图控制器调用表数据到另一个视图控制器

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

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

我的代码是

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

尝试这个。 这是一个最佳选择。 在您的.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. 转到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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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