Marcus Ataide
Marcus Ataide

Reputation: 7540

Send NSString to another UIViewController gives null

It is simple, but I really don't know why it always gives me null.

- FirstViewcontroller:

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

Upvotes: 0

Views: 127

Answers (5)

Levi
Levi

Reputation: 7343

You are pushing self.subViewController but you are passing the string to viewTwo. These are 2 different objects. Use this line instead:

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

Upvotes: 1

Vinodh
Vinodh

Reputation: 5268

Please use the following code

- FirstViewcontroller:

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

Upvotes: 0

nsgulliver
nsgulliver

Reputation: 12671

In your code you are doing wrong you should initialize the viewController which you are pushing to the navigationController, you are initializing viewTo but you are pushing self.subViewController.

should be initializing like this;

self.subViewController= [[SubViewViewController alloc] initWithNibName:@"SubViewViewController" bundle:[NSBundle mainBundle]];

Upvotes: 3

Dan Shelly
Dan Shelly

Reputation: 6011

add:
self.subViewController = viewTwo;

Upvotes: 0

Dilip Manek
Dilip Manek

Reputation: 9143

change here

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

Do change in SecondViewcontroller.h file

{
NSString             *queryValue;
}
@property (nonatomic, strong) NSString *queryValue;

Upvotes: 1

Related Questions