- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *second=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
second.getString=[getArray objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:@"Action" sender:nil]; }
如何将数据从一个视图传递到另一个视图,在我的应用程序中有一些我想要的对象,当我点击任何单元格时,该值应显示在具有UILabel
的下一个ViewController上?
答案 0 :(得分:1)
在SecondViewController中创建一个init方法,如下所示。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withData:(NSString *) data {
if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.data = data;
// write your own code here
}
return self;
}
在你的tableview' didSelectRowAtIndexPath
行方法中获取字符串,然后按下图所示推送控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSSting *stringData = @"hi";// get your data and passing to stringData
SecondViewController *secondVC=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil withData:stringData];
[self.navigationController pushViewController: secondVC animated:YES];
}
答案 1 :(得分:0)
试试这个
创建实例变量NSString * sendingText ;// if you want to send text(string)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
sendingText = [getArray objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:@"Action" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Action"]) // this check is optional if there is only on segue
{
SecondViewController *second= (SecondViewController *)segue.destinationViewController;
second.getString = sendingText;
}
}
SecondViewController.h
中的
@interface SecondViewController : UIViewController
{
UILabel * exampleLabel;
}
@property NSString * getString;
@end
并在SecondViewController.m
-(void)viewDidLoad
{
exampleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];
[self.view addSubview:exampleLabel];
}
-(void)viewWillAppear:(BOOL)animated
{
exampleLabel.text = getString;
}