0

I'm trying to test some ideas on a Tabbed Application preset - what I want to do is have the main view 90% container for subsequent views (which is working fine so far) and have a persistent status bar at the top showing a UILabel that can be updated from the subsequent views, however am having trouble updating the label.

In seeking a solution I have attempted both a global variable and protocol approach.

Whilst I can set the label text to be that of the global variable when the main view is loaded, I cannot figure out how to refresh the label once the global variable has been changed in a subsequent view. Similarly with the protocol approach, trying to create a global function in the main view that will update the instance UILabel's properties when called from a subsequent view is not allowed.

If someone could point me in the right direction I would be very grateful.

EDIT

I have tried creating a public function that can be called from subsequent views:

GlobalContainerViewController.h

@interface GlobalContainerViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

+ (void) updateLabel;

@end

GlobalContainerViewController.m

...
+ (void) updateLabel
{
    _statusLabel.text = [NSString stringWithFormat:@"updated"];
}

However get the error "Instance variable '_statusLabel' accessed in class method.

I have also tried using a global variable to store the status text:

AppDelegate.h

NSString * statusVar;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    statusVar = [NSString stringWithFormat:@"initialStatus"];
    return YES;
}

GlobalContainerViewController.m (with AppDelegate.h imported into .h)

- (void)viewDidLoad
{
    [super viewDidLoad];
    _statusLabel.text = statusVar;
}

SecondViewController.m (with AppDelegate.h imported into .h)

- (IBAction)updateStatusPressed:(id)sender {
    statusVar = [NSString stringWithFormat:@"Update"];
}

However am not sure how to get the label to refresh with this updated data.

Aethon
  • 1
  • 2
  • `"I have attempted both a global variable and protocol approach"` - it usually helps when you provide code showing what you have tried. Others maybe able to gleam something from it. – Mike Jul 29 '14 at 18:27
  • *Similarly with the protocol approach, trying to create a global function in the main view that will update the instance UILabel's properties when called from a subsequent view is not allowed.* Who's stopping you? – duci9y Jul 29 '14 at 18:53

1 Answers1

0

To anyone else that comes across the same problem, I've solved using notifications (Send and receive messages through NSNotificationCenter in Objective-C?) - not sure if that's the best way but is working for my needs at this stage:

GlobalContainerViewController.m

- (void)viewDidLoad
...
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveTestNotification:)
                                             name:@"TestNotification"
                                           object:nil];

}

- (void) receiveTestNotification:(NSNotification *) notification
{

if ([[notification name] isEqualToString:@"TestNotification"])
    NSLog (@"Successfully received the test notification!");
NSDictionary * info = notification.userInfo;
NSString *statusString=info[@"status"];
NSLog(@"Name = '%@;",statusString);
_statusLabel.text = statusString;

}

SecondViewController.m

- (IBAction)updateStatusPressed:(id)sender {

statusVar = [NSString stringWithFormat:@"Update from 2nd view"];
NSDictionary * dict =[NSDictionary dictionaryWithObject:statusVar forKey:@"status"];
[[NSNotificationCenter defaultCenter]
 postNotificationName:@"TestNotification"
 object:self userInfo:dict];

}
Community
  • 1
  • 1
Aethon
  • 1
  • 2