i hope you can help me solving my actual problem:
I´ve set up a new project with a navigation controller. For the table view of the project I´ve added a new class for it, called “TableViewController.h” and “TableViewController.m”.
In this class I´ve declared in .h a property for a NSString to access it from other classes, like this:
@property (strong, nonatomic) NSString *testString;
In .m I´ve synthesized it as followed:
@synthesize testString;
Now I set in – (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath-Method of the table view controller the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
testString = @"Hello, World!";
}
Now created a new UIViewController class for the next view which should appear when I´m tapping on a cell (all that works so far). In this UIViewController, called “SecondView” I imported the .h-File of TableViewController.
Further I have this code in SecondView.m:
-(void) viewDidLoad
{
[super viewDidLoad];
TableViewController *demoObject = [TableViewController alloc] init]
NSLog (@"Teststring is: %@" [demoObject.testString]);
}
Now when I start the Simulator I get this Output:
Teststring is: (null)
When I NSLog the testString in TableViewController I get “Hello, World”, but why isn´t it “transferred” to the SecondView class and generates the needed output?
You are logging the string in
viewDidLoad, which gets called long before it is set in the table views’sdidSelectRowmethod. You should also useself.testString = ...