0

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

- 第一视图控制器:

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

- SecondViewcontroller.h:

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

- SecondViewcontroller.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%@", self.queryValue);
}
4

5 回答 5

3

在你的代码中你做错了你应该初始化你推送到导航控制器的viewController,你正在初始化viewTo但你正在推送self.subViewController

应该像这样初始化;

self.subViewController= [[SubViewViewController alloc] initWithNibName:@"SubViewViewController" bundle:[NSBundle mainBundle]];
于 2013-04-05T15:05:08.537 回答
1

在这里改变

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

在 SecondViewcontroller.h 文件中进行更改

{
NSString             *queryValue;
}
@property (nonatomic, strong) NSString *queryValue;
于 2013-04-05T15:04:46.097 回答
1

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

[self.navigationController pushViewController:viewTwo animated:YES];
于 2013-04-05T15:05:53.610 回答
0

添加:
self.subViewController = viewTwo;

于 2013-04-05T15:05:40.603 回答
0

请使用以下代码

- 第一视图控制器:

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

- SecondViewcontroller.h:

@property (nonatomic, retain) NSString *queryValue;

- SecondViewcontroller.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%@", self.queryValue);
}
于 2013-04-05T15:12:41.783 回答