将 NSString 从一个视图控制器传递到另一个视图不起作用



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

因此,我将字符串分配给在另一个视图控制器中声明的属性。

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

当我到达第三个视图控制器时,该值显示为 (null)。有什么想法吗?

你应该尝试:

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

而不是

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

如果我正确理解了您的代码,那么您将以错误的方式进行赋值:

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

应该是

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

看来你混淆了同义顺序

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

你应该使用

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

在你的爱点击方法中,它应该是

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

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

 @property NSString * str;

然后在 SecondViewController 中合成。

@synthesize str;

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

NSString * str1;

然后像贝洛代码一样传递..

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

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

NSLog(@"%@",str);

输出是 abcd..

遵循这种方式

最新更新