Change UILabel in View Controller from Singleton - ios

I am very new to iOS development and I'm struggling to make an app which connects to BLE devices. As I have many view controllers I need to keep the peripheral always connected in all of them.
To achieve this, I implemented all the BLE connection methods in a Singleton. This works just great, I call the connect method from View Controller and the Singleton connects to the peripheral.
Now, the problem is I have a UILabel in my view controller which I would like to update with the connection state (scanning, connecting, connected, disconnected) from the Singleton.
So I tried to get instance from the View Controller and change the label directly like:
MainViewController *controller = [[MainViewController alloc] init];
controller.myLabel.text = #"TEST";
I also instantiated the view controller class like:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle: nil];
MainViewController *controller = (MainViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"MainVC"];
Then I tried to create a method in the main View Controller:
- (void) updateLabel:(NSString *) labelText{
NSLog(#"CALLED IN MAIN");
self.myLabel.text = labelText;
}
And call it from Singleton like:
MainViewController *controller = [[MainViewController alloc] init];
[controller updateLabel:#"TEST"]
Which was called properly (NSLog was shown) but the label was not updated.
I don't really know how to update my View Controller label from the Singleton. Don't know neither if the way I'm trying to do it is the right one or not.
Any advice or help would be much appreciated. Thanks.
----- UPDATE: -----
Thanks to Mundi and Nikita, I got a better way to implement what I need through NSNotification. For all those who need it here is how I do it:
In my View Controller in viewDidLoad I call:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateConnectionLabel:) name:#"connectionLabelNotification" object:nil];
Then in the same class I implement the notification observer method like:
- (void)updateConnectionLabel:(NSNotification *) notification {
if ([[notification name] isEqualToString:#"connectionLabelNotification"]) {
self.connectionLabel.text = notification.object; //The object is a NSString
}
}
Then in my Singleton, when I need I call:
[[NSNotificationCenter defaultCenter] postNotificationName:#"connectionLabelNotification" object:[NSString stringWithFormat:#"CONNECTED"]];
When the View Controller receives the notification from the Singleton it updates the label with the text I add on the notification object (in this case #"CONNECTED").

You need to use NSNotification.
Here is sample code:
in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(mySelector:)
name:DeviceStateChanged
object:nil];
in dealloc:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:DeviceStateChanged
object:nil];
also add a method in ViewController:
- (void) mySelector:(NSNotification *) notification {
// action performed
}
in Sigleton
- (void) foo {
/// some actions
// device connected
[[NSNotificationCenter defaultCenter] postNotificationName:DeviceStateChanged object:self];
///
}
Recommendation: move notification name to your constants and use constant name. For naming convention look at Apple guidelines

The proper way to do this is via NSNotification. This communication device is meant for exactly this kind of situation. It broadcast a message without caring whether the potential receiver is available.
In your view controllers, you call NSNotificationCenter's addObserver / removeObserver when they appear / disappear. You post the notification via postNotification:.

Related

NSNotification is not calling #selector method

I am trying to pass NSString from one class to another. Let's say I have ViewController A and ViewController B. I want to pass NSString from A to B.
In ViewController A, I have following code :
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificationMessageEvent" object:userType];
//Here user type is a string I get using delegate and I need to pass this userType to ViewController B
In ViewController B, I have following code :
In viewDidLoad , I have following code :
[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(notificationAction:) name:#"NotificationMessageEvent" object:nil];
//This NSNotificationCenter method is called
I have registered the following selector method.
-(void) notificationAction:(NSNotification *) notification
{
if ([notification.object isKindOfClass:[NSString class]])
{
NSString *message = [notification object];
// do stuff here with your message data
NSLog(#"%# is message",message);
}
else
{
NSLog(#"Error, object not recognised.");
}
}
//The above selector method is never called.
I have read other similar stackoverflow answers but I have not been able to find any solutions regarding this.
Your code and syntax is clearly correct. I'm guessing that it's a matter of object lifecycle. I would hypothesize that either of the following are true:
ViewController B doesn't actually exist as an object when the notification is posted
or
ViewController A is posting the notification before ViewController B has had a chance to register for it.
One way you can verify either of these is to add two breakpoints, one where the notification is posted, and one where the notification listener is registered. The breakpoint where the listener is registered should be hit before the notification is posted. If that happens, then verify that ViewController B is in fact an object in existence when the notification is posted (like it's not popped off the navigation stack or something).
This is what you are looking for
NSNotification not being sent when postNotificationName: called
You must addObserver before postNotificationName
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationAction:) name:#"NotificationMessageEvent" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificationMessageEvent" object:nil];

Access View Controller's properties from AppDelegate

I currently have an Xcode project in which I am using Storyboards. In my AppDelegate, I need to set some properties that are contained in the .h files of other View Controllers in response to notifications that the app receives.
How do I instantiate a object of those view controllers in the AppDelegate so that I can access and modify their properties?
There are ways for the app delegate to get a handle to the right vc and communicate with it, but the better design is to have the information flow the other way around, letting the view controllers ask for information and update their own properties.
To do this, when the app delegate receives a notification, have it post a corresponding NSNotification (via NSNotificationCenter). The view controllers who care about the change can add themselves as observers for this notification and get the information. How can they get it? A few ways:
The textbook way is to have a model on the application, probably a singleton that has properties relevant to the view controllers. Idea two is to effectively make your app delegate a model by giving it properties that the vcs can interrogate. Last idea, the userInfo param on postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo can convey information to the observer.
EDIT - NSNotificationCenter is pretty easy to use. It goes like this:
In AppDelegate.m, when you get an external notification:
// say you want a view controller to change a label text and its
// view's background color
NSDictionary *info = #{ #"text": #"hello", #"color": [UIColor redColor] };
[[NSNotificationCenter defaultCenter] postNotificationName:#"HiEverybody" object:self userInfo:info];
In SomeViewController.m, subscribe to the message:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(observedHi:)
name:#"HiEverybody"
object:nil];
}
// unsubscribe when we go away
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// this method gets run when the notification is posted
// the notification's userInfo property contains the data that the app delegate provided
- (void)observedHi:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
self.myLabel.text = userInfo[#"text"];
self.view.backgroundColor = userInfo[#"color"];
}

Multiple NSNotificationCenter Issue

In my app in which I'm using storyboards I have mainViewController with MapView inside. At the top of the mainViewController I'm displaying UIContainerView from the bottom of the screen with UITableViewController inside. When user click on row I'm sending notification to the mainViewController in purpose of changing map region with animation. The similar notifications are sending when user is selecting specific tableViewCell and also when user tapping on the DONE button located in navigationViewController. Everything works fine for me except method called in DONE button method.
MAIN VIEW CONTROLLER -(void)viewDidLoad method...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(hideContainerView:)
name:HideContainerView object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changeRegionForUser:)
name:ChangeRegionForUser
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(hideContainerView:)
name:ChangeRegionToInitial
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changeRegionToFitWearerAnnotations:)
name:ChangeRegionToFitWearerAnnotations
object:nil];
MAIN VIEW CONTROLLER OBSERVERS METHODS:
#pragma mark NSNotificationCenter methods
-(void)changeRegionForUser:(NSNotification*)notification
{
NSLog(#"%#",notification.description);
[self zoomToFitUserLocationWithLatitude:-33.861858 longitude:151.210546 andUserInfo:nil];
}
-(void)hideContainerView:(NSNotification*)notification
{
NSLog(#"%#",notification.description);
[self zoomToFitMapAnnotations:_mainMapView];
[UIView beginAnimations:#"HideContainerAnimation" context:nil];
[UIView setAnimationDuration:0.5];
[_containerView setFrame:CGRectMake(0, 600, 320, 284)];
[UIView commitAnimations];
}
-(void)changeRegionToFitWearerAnnotations:(NSNotification*)notification
{
NSLog(#"%#",notification.description);
[self zoomAnnotationsOnMapView:_mainMapView toFrame:CGRectMake(0, 0, 320, 200) animated:YES];
}
TABLE VIEW CONTROLLER (DID-SELECT-ROW-AT-INDEX-PATH METHOD)//It work properly
[[NSNotificationCenter defaultCenter] postNotificationName:#"ChangeRegionForUser" object:self];
TABLE VIEW CONTROLLER (DONE BUTTON METHOD) //IT DOESN'T WORK
-(void)doneButtonAction:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"HideContainerView" object:self];
}
MY STORYBOARD
IN MAIN VIEW CONTROLLER.H
#interface MainScreenViewController : UIViewController<MKMapViewDelegate>
extern NSString * const HideContainerView;
extern NSString * const ChangeRegionForUser;
extern NSString * const ChangeRegionToInitial;
extern NSString * const ChangeRegionToFitWearerAnnotations;
IN MAIN VIEW CONTROLLER.M
#import "MainScreenViewController.h"
NSString* const HideContainerView = #"HideContainerView";
NSString* const ChangeRegionForUser = #"ChangeRegionForUser";
NSString* const ChangeRegionToInitial = #"ChangeRegionToInitial";
NSString* const ChangeRegionToFitWearerAnnotations =#"ChangeRegionToFitWearerAnnotations";
You are using a string constant HideContainerView for the notification name when you add yourself as observer. But when you post the notification you are using a literal string #"HideContainerView". Maybe you are using two different strings there?
Also, I think that you are using self as the object when you add yourself as an observer. It seems this precludes receiving the notification. From the documentation:
notificationSender
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
Use nil instead.
That being said, would it not be better to couple these two controllers more tightly. I think that would be a much more appropriate design. You should send notifications when the sender does not know if the receiver exists or not. But in this case, you know that the receiver exists.
Thus, you should implement a simple delegate protocol, retain a reference to the map view controller and tell it directly what to do.

NSNotificationCenter , prints memory address

I have in my app a UITableview Controller, a View Controller and I'm trying to pass NSDictionary from UITableview Controller to my ViewController, using NSNotificationCenter. So, I push a notification at my UITableview Controller and then I add an observer ,using a selector at my ViewController.The selector is called,but I have an NSLog and get memory results ,like :
ViewController: 0x8a0bcc0
I have tried to pass NSString instead of NSDictionary , but I get again memory results , and not the value of the string.
My code :
UITableView Controller
NSString *string=#"This is a test string";
[[NSNotificationCenter defaultCenter] postNotificationName: #"Update" object: string];
ViewController
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(incomingNotification:) name:#"Update" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"Update" object:self];
And here is the incomingNotification selector method:
-(void) incomingNotification : (NSNotification *)notification{
NSLog(#"Trying to print : %#",[notification object]);
}
All Notifications take place at ViewDidLoad method.Thank you!
UPDATE
Finally , I quit using NSNotificationCenter and used properties to pass data ,changing a bit the inheretence from my TableViewController. No idea why Notifications did not work ,as they were supposed to. Thank you all ,very much for your suggestions and ideas :)
[[NSNotificationCenter defaultCenter] postNotificationName:#"Update" object:self]
Object means the object that generates a notification. To post parameters use another method
[[NSNotificationCenter defaultCenter] postNotificationName:#"Update" object:self userInfo:string]
If I understand correctly, UIViewController is shown after you tap a button on UITableViewController. And you if you are adding a ViewController as observer in its -viewDidLoad:, then it will be able to receive notifications only when it is loaded.
What do you need:
1) override -init or -initWithNibName: method of ViewController like this:
-(id) init
{
self = [super init];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(incomingNotification:) name:#"Update" object:nil];
}
return self;
}
so you can be sure ViewController is observing for notifications from the beginning (well, this might be unnecessary step for your case)
2) when you push ViewController you need to send a notification after it was created, like this:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *nextController = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nextController animated:YES];
NSString *string=#"This is a test string";
[[NSNotificationCenter defaultCenter] postNotificationName: #"Update" object: string];
}
However, if you're trying just to send some parameters from one view controller to another, this is the wrong way. Just create a property in ViewController and in method -tableView:didSelectRowAtIndex: of UITableViewController set this property

sending a NSNotification between views

This is my first attempt with NSNotification, tried several tutorials but somehow it's not working.
Basically I am sending a dictionary to class B which is popup subview (UIViewController) and testing to whether is has been received.
Could anyone please tell me what am I doing wrong?
Class A
- (IBAction)selectRoutine:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:#"Right"
forKey:#"Orientation"];
[[NSNotificationCenter defaultCenter]
postNotificationName:#"PassData"
object:nil
userInfo:dictionary];
createExercisePopupViewController* popupController = [storyboard instantiateViewControllerWithIdentifier:#"createExercisePopupView"];
//Tell the operating system the CreateRoutine view controller
//is becoming a child:
[self addChildViewController:popupController];
//add the target frame to self's view:
[self.view addSubview:popupController.view];
//Tell the operating system the view controller has moved:
[popupController didMoveToParentViewController:self];
}
Class B
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(receiveData:)
name:#"PassData"
object:nil];
}
- (void)receiveData:(NSNotification *)notification {
NSLog(#"Data received: %#", [[notification userInfo] valueForKey:#"Orientation"]);
}
If it hasn't registered to receive that notification yet - it will never receive it. Notifications don't persist. If there isn't a registered listener, the posted notification will be lost.
Specific to your problem, the receiver hasn't started observing before the notification is sent so the notification just gets lost.
More generally: What you're doing wrong is using notifications for this use case. It's fine if you're just playing around and experimenting but the kind of relationship you're modelling here is best actioned by retaining a reference to the view and calling methods on it directly. It's usually best if experimentation is realistic of the situation in which it would actually be used.
You should be aware of 3 basic communication mechanisms and when to use them:
Notifications
Use them to notify other unknown objects that something has happened. Use them when you don't know who wants to respond to the event. Use them when multiple different objects want to respond to the event.
Usually the observer is registered for most of their lifetime. It's important to ensure the observer removes itself from NSNotificationCenter before it is destroyed.
Delegation
Use delegation when one object wants to get data from an unknown source or pass responsibility for some decision to an unknown 'advisor'.
Methods
Use direct calls when you know who the destination object is, what they need and when they need it.

Resources