将NSString从一个View Controller传递到另一个不起作用的视图

时间:2013-10-24 06:57:10

标签: ios objective-c nsstring

所以我看了一堆stackoverflow答案,但似乎没有任何帮助。也许这是一个特例?到目前为止,这是我的代码:

所以我将字符串赋给在另一个视图控制器中声明的属性。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {

     FinalRead *readings = [_fetchedResultsController objectAtIndexPath:indexPath];
     FinalReadingViewController *detailViewController = [[FinalReadingViewController alloc] init];

     detailViewController.lover = [NSString stringWithFormat:@"%@", readings.a1];
     detailViewController.lover2 = [NSString stringWithFormat:@"%@", readings.a2];
     detailViewController.monies = [NSString stringWithFormat:@"%@",readings.a3];
     detailViewController.monies2 = [NSString stringWithFormat:@"%@", readings.a4];
     detailViewController.healthy = [NSString stringWithFormat:@"%@", readings.a5];
     detailViewController.healthy2 = [NSString stringWithFormat:@"%@",readings.a6];

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

 }

然后在下一个视图控制器中,我将字符串分配给另一个视图控制器中的另一个属性:

- (IBAction)loveClicked:(id)sender {

    LoveDetailViewController *love = [[LoveDetailViewController alloc] init];


    NSString *stringLove1 = self.healthy;
    NSString *stringHealth2 = self.healthy2;

    stringLove1 = love.loverly;
    stringLove2 = love.loverly2;

    [self.navigationController pushViewController: love animated:YES];
}

当我到达第3个视图控制器时,值出现为(null)。有什么想法吗?

5 个答案:

答案 0 :(得分:3)

你应该尝试:

love.loverly = stringLove1;
love.loverly2 = stringLove2;

而不是

 stringLove1=love.loverly ;
 stringLove2=love.loverly2;

答案 1 :(得分:2)

如果我正确理解您的代码,则会使分配错误:

stringLove1 = love.loverly;
stringLove2 = love.loverly2;

应该是

love.loverly = stringLove1;
love.loverly2 = stringLove2;

答案 2 :(得分:0)

似乎你混淆了作业顺序

stringLove1 = love.loverly;
stringLove2 = love.loverly2;

你应该使用

love.loverly = stringLove1;
love.loverly2 = stringLove2;

答案 3 :(得分:0)

在你的loveClicked方法中,它应该是

love.loverly = stringLove1;
love.loverly2 = stringLove2;

答案 4 :(得分:0)

在SecondViewController中首先声明属性 喜欢这个

 @property NSString * str;

然后在SecondViewController中合成。

@synthesize str;

然后转到你的FirstViewController在.h文件中声明一个字符串。

NSString * str1;

然后传递像belo代码..

 str1=@"abcd";
 SecondViewController * svc=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
 svc.str=str1;
 [self.navigationController pushViewController:svc animated:YES];

然后在SecondViewController.m文件中记录str

NSLog(@"%@",str);

//输出是abcd ..

按照这种方式