Reputation: 349
I know this question is asked often, I've read so much on it but I still cant get it to work. Lets say I have two classes, FirstClass and SecondClass. FirstClass has a label and SecondClass wants to get the text of that label. Here is what I've done:
//FirstClass
@interface FirstClass : UIViewController
{
@public
UILabel *theLabel;
}
@property (strong, nonatomic) UILabel *theLabel;
@implementation FirstClass
@synthesize theLabel;
//SecondClass
#import "MainGameDisplay.h"
@interface SecondClass : UIViewController
{
MainGameDisplay *mainGame;
}
@property (strong, nonatomic) UILabel *theSecondLabel;
@implementation SecondClass
-(void) thisMethodIsCalled {
mainGame = [[FirstClass alloc] init];
self.theSecondLabel.text = mainGame.theLabel.text;
NSLog(@"%@",mainGame.theLabel.text); //Output is '(Null)'
}
theLabel.Text is not nil as it's being changed every second and is also displaying the label on the other controller which is running in the background whilst the SecondClass view is loaded. Could someone please point me to the write direction if I'm completely wrong, or show me some kind of example as to how this would be done. Thank you.
EDIT:
@Implementation FirstClass
@synthesize theLabel;
- (void)viewDidLoad {
[self superview];
[self startTickCount];
}
-(void) startTickCount {
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timeChanger) userInfo:nil repeats:YES];
}
-(void) timeChanger {
theDay++;
NSLog(@"%@",self.theLabel.text);
if (theDay <= 9)
self.theLabel.text = [NSString stringWithFormat: @"0%i", theDay];
else
self.theLabel.text = [NSString stringWithFormat: @"%i", theDay];
if (theDay > 27)
[self monthChanger];
}
That's pretty much it. The NSLog outputs the day as expected.
Upvotes: 0
Views: 83
Reputation: 34657
The method name is text, not Text. The case matters, and using text with a lower T will cause errors.
Upvotes: 0
Reputation: 1128
I assume MainGameDisplay is your FirstClass. Then in order to update theSecondLabel.text in you SecondClass object you need to pass an object of FirstClass and not to instantiate it in method call.
I guess you need to do something like this (this is a very simple example)
After that:
1) create instance of FirstClass, let it have name firstClass.
2) create instance of SecondClass. SecondClass *secondClass = [[SecondClass alloc] init];
3) set property of Second class to instance of FirstClass
secondClass.firstClass = firstClass;
4) now you have a reference to actual object of FirstClass and can access its properties.
-(void) thisMethodIsCalled {
self.theSecondLabel.text = self.firstClasss.theLabel.text;
NSLog(@"%@",mainGame.theLabel.text);
}
I hope this will help.
Upvotes: 1
Reputation: 50089
if you didnt leave out LOTS of code,
-(void) thisMethodIsCalled {
mainGame = [[MainGameDisplay alloc] init];
self.theSecondLabel.text = mainGame.theLabel.text;
NSLog(@"%@",mainGame.theLabel.text); //Output is '(Null)'
}
will not work.. nobody can modify mainGame in between the alloc init and the getting of .text variable....
[@all I know this is not an answer, but the formatting of comments sucks. Ill edit or delete it as needed]
Upvotes: 1
Reputation: 10808
If this is your code exactly, you have two problems. First, Text
is unnecessarily capitalized. And, secondly, TheLabel
is unnecessarily capitalized.
Edited Code:
-(void) thisMethodIsCalled {
mainGame = [[MainGameDisplay alloc] init];
// 'text' shouldn't be capitalized
// 'theLabel' shouldn't be capitalized
self.theSecondLabel.text = mainGame.theLabel.text;
NSLog(@"%@",mainGame.theLabel.text);
}
Upvotes: 0