代码之家  ›  专栏  ›  技术社区  ›  Marckaraujo

将NSString发送到另一个UIViewController给出null

  •  0
  • Marckaraujo  · 技术社区  · 13 年前

    这很简单,但我真的不知道为什么它总是给我空。

    -FirstView控制器:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
            SubViewViewController *viewTwo = [[SubViewViewController alloc] initWithNibName:@"SubViewViewController" bundle:[NSBundle mainBundle]];
    
    
        viewTwo.queryValue = [NSString stringWithFormat:@"%d",indexPath.row];
    
        [self.navigationController pushViewController:self.subViewController animated:YES];
    }
    

    -SecondView控制器.h:

    NSString             *queryValue;
    @property (nonatomic, retain) NSString *queryValue;
    

    -SecondView控制器.m:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSLog(@"%@", self.queryValue);
    }
    
    5 回复  |  直到 13 年前
        1
  •  3
  •   nsgulliver    13 年前

    在您的代码中,您做错了,您应该初始化正在推送到navigationController的viewController,您正在初始化 viewTo 但你在努力 self.subViewController .

    应该这样初始化;

    self.subViewController= [[SubViewViewController alloc] initWithNibName:@"SubViewViewController" bundle:[NSBundle mainBundle]];
    
        2
  •  1
  •   Dilip Manek    13 年前

    在这里找零

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

    在SecondViewcontroller.h文件中进行更改

    {
    NSString             *queryValue;
    }
    @property (nonatomic, strong) NSString *queryValue;
    
        3
  •  1
  •   Levi    13 年前

    你在推 self.subViewController 但你正在将字符串传递给 viewTwo 。这是两个不同的对象。请改用此行:

    [self.navigationController pushViewController:viewTwo animated:YES];
    
        4
  •  0
  •   Dan Shelly    13 年前

    添加:
    self.subViewController = viewTwo;

        5
  •  0
  •   Vinodh    13 年前

    请使用以下代码

    -FirstView控制器:

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
    
            [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.subViewController = [[SubViewViewController alloc] initWithNibName:@"SubViewViewController" bundle:[NSBundle mainBundle]];
    
    
            viewTwo.queryValue = [NSString stringWithFormat:@"%d",indexPath.row];
    
            [self.navigationController pushViewController:self.subViewController animated:YES];
        }
    

    -SecondView控制器.h:

    @property (nonatomic, retain) NSString *queryValue;
    

    -SecondView控制器.m:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSLog(@"%@", self.queryValue);
    }