I have a singleton here is the header file:
#import <Foundation/Foundation.h>
@interface Shared : NSObject
{
NSString *messages;
}
@property (nonatomic, retain) NSString *messages;
+ (Shared*)sharedInstance;
@end
Here is the implementation:
#import "Shared.h"
static Shared* sharedInstance;
@implementation Shared
@synthesize messages;
+ (Shared*)sharedInstance
{
if ( !sharedInstance)
{
sharedInstance = [[Shared alloc] init];
}
return sharedInstance;
}
- (id)init
{
self = [super init];
if ( self )
{
messages = [[NSString alloc] init];
}
return self;
}
@end
The problem is when the I use
[Shared sharedInstance].messages = someVariable;
I can use
NSLog([Shared sharedInstance].messages);
and it shows the right output, but when i check from another class, NSLog doesn’t show any output. I have the NSLog in the viewDidLoad method of another class, so when I click a button to go to the next view, it should output the value of the string, but it only works the second time. If the variable is set to dog, first it outputs nothing, then when I close the view and try again, it outputs dog. however, if I then change the variable to cat, it will output dog, and on the next attempt, output cat. I want it to update immediately, rather than remain one behind all the time.
EDIT: Here’s the code from the other classes
This particular section is from a view controller class in the method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Omitted, just preparing the DB, and emptying the array.
if ([db open])
{
FMResultSet *s = [db executeQueryWithFormat:@"SELECT ShabadID FROM Shabad WHERE Gurmukhi LIKE %@", currentLine];
while ([s next])
{
lineID = [s intForColumn:@"ShabadID"];
}
s = [db executeQueryWithFormat:@"SELECT Gurmukhi, ShabadID FROM Shabad WHERE ShabadID LIKE %i", lineID];
while ([s next])
{
//NSLog([s stringForColumn:@"Gurmukhi"]);
[paragraphArray addObject:[s stringForColumn:@"Gurmukhi"]];
}
Text = @"";
for (int i = 0; i<[paragraphArray count]; i++)
{
Text = [Text stringByAppendingFormat:@"%@\n", [paragraphArray objectAtIndex:i]];
}
[Shared sharedInstance].messages = Text;
}
Then in the another class, where I want the text to appear, in the viewDidLoad method,
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog([Shared sharedInstance].messages);
UITextView *myUITextView = [[UITextView alloc] initWithFrame:CGRectMake(0,30,310,450)];
myUITextView.text = [Shared sharedInstance].messages;
myUITextView.textAlignment = NSTextAlignmentCenter;
myUITextView.textColor = [UIColor blackColor];
myUITextView.font = [UIFont fontWithName:@"GurbaniLipiLight" size:24];
[myUITextView setBackgroundColor:[UIColor clearColor]];
myUITextView.editable = NO;
myUITextView.scrollEnabled = YES;
[ScrollerView addSubview:myUITextView];
}
Sure the NSLog doesn’t show up right, but neither does the text in the textview, it does the same thing the NSLog does.
There is an assumption here about what order things happen in that’s not quite right. Assuming there’s a segue involved in this,
didSelectRowAtIndexPath:is called after the new view controller is prepared but before it’s displayed. Moving code toviewWillAppear:orviewDidAppear:delays execution until after the calling controller has set new data.The other approach for communication between controllers that use a segue, is to use
prepareForSegue:in the first controller to set data that the second controller needs. That way it should be available when the view is loaded.