-1

MasterViewController.m

#import "DetailViewController.h"

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([segue.identifier isEqualToString:@"DetailViewControllerSeque"]) {
        DetailViewController *detailView = [segue destinationViewController];

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        theList = [app.listArray objectAtIndex:indexPath.row];

        detailView.theList = theList;

        // String to pass to DetailViewController
        detailView.string2pass = @"this is a passing string";
    }
}


DetailViewController.h

NSString *string2pass;

@property (retain, nonatomic) NSString *string2pass;


DetailViewController.m

NSLog(@"%@", string2pass);

Output: (null)


What I am doing wrong?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lorenz
  • 11
  • 4

4 Answers4

4

Unless you have this in your implementation, it won't work as you expected.

@synthesize string2pass = string2pass;

..or you can fix it by deleting the line:

NSString *string2pass;

Your log is logging the value of string2pass variable you declared. But there is another variable _string2pass.

NSLog(@"%@", string2pass);

The @property you declared, is backed by a variable name _string2pass if you don't explicitly write a @synthesize statement. Not writing an @sythesize statement is the same as declaring one like so:

@synthesize string2pass = _string2pass;
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
  • Thanks for helping me, but even when I remove `NSString *string2pass;` and I add `@synthesize string2pass = _string2pass;` I´m still getting (null) :( – Lorenz Sep 16 '13 at 13:11
  • You don't actually need to explicitly write that synthesize statement if you don't want, it's no harm it being there - but if you don't write it, it is created for you. If you did that, then you probably won't compile now without changing your NSLog statement to read NSLog(@"%@", self.string2pass).... have you done this? Also, where are you calling the NSLog statement from? – bandejapaisa Sep 16 '13 at 13:15
  • Called from viewDidLoad. Yes, I also tried it w/o @sythesize and self.string2pass, but also not working... – Lorenz Sep 16 '13 at 13:19
  • I don't use story boards, so I'm not too sure on the lifecycle - is viewDidLoad called before you have set the variable? What if you log from viewWillAppear? – bandejapaisa Sep 16 '13 at 13:22
0

Use self.string2pass in NSLog. When we use self. to access a property then the getter/setter is called.

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
  • please give the reason for downvoting? – Puneet Sharma Sep 16 '13 at 12:53
  • ...it wasn't me... however, I think you were down voted because your answer hides the real problem that there are 2 variables, which could lead to more bugs/confusion later down the line. – bandejapaisa Sep 16 '13 at 12:56
  • Yeah I agree that it does not include the whole scenario but it is not a wrong answer. Anyways you have answered it completely above so it does not make any sense to repeat those things again. – Puneet Sharma Sep 16 '13 at 12:58
  • 1
    Technically, you are correct. I don't think it deserves the down vote. – bandejapaisa Sep 16 '13 at 12:59
0

First you need to add NSString property to your SecondViewController.h file:

@property (nonatomic, copy) NSString *myString;

and then in FirstViewController.m file create SecondViewController object and pass to it whatever string you want:

SecondViewController *secondViewcontroller = [[SecondViewController alloc] initWithNibName:@"ACEViewController" bundle:nil];
secondViewcontroller.myString = @"WhateverYouNeedToPass";

Log string in SecondViewController

NSLog(@"%@", self.myString);
Arun
  • 3,406
  • 4
  • 30
  • 55
Jitendra
  • 5,055
  • 2
  • 22
  • 42
0

In your DetailViewController.h only you need to define this -

@property (strong, nonatomic) NSString *string2pass;

In your DetailViewController.m

@synthesize string2pass 

This will work for you.

Devel
  • 449
  • 3
  • 9