Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am building an app where the value of an integer can change randomly and I want to be able to hide or show a UIButton depending on the value of this integer.
However, I don't know how to trigger the status change when the value of the integer changes.
Is there any easy way to do it on iOS?
Many thanks for your help
Use observer pattern for this
[self.myVC addObserver:self forKeyPath:#"IntegerProperty" options:0 context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == self.myVC && [keyPath isEqualToString:#"IntegerProperty"]) {
//do work
}
}
Your integer should be a property of a class and you should access it properly via the provided accessors (so use self.xxx). Then you can use KVO.
you can use key value observer concepts, it works similar to notification centre...
Related
I am trying to implement an answer to an old question.
Here is that answer https://stackoverflow.com/a/26725721/1984167
And here are the code sections suggested in that answer:
self.name = self.peripheral.name;
[self.peripheral addObserver:self forKeyPath:#"name" options:NSKeyValueObservingOptionNew context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:#"name"]) {
[self peripheralDidUpdateName:self.peripheral];
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral {
if([peripheral.name isEqualToString:self.name]) return;
if([self.delegate respondsToSelector:#selector(peripheralDidUpdateName:)]) {
[self.delegate peripheralDidUpdateName:self];
}
}
I am using xCode 13.2.1 and my project has been operational for years but recently Bluetooth peripheral renaming has been become an issue which I need to solve.
First, I do not know for sure where I need to place the two lines that adds an observer.
I have tried adding the 2 observer creation lines in my ViewContoller.c code:
The code section where its global variables are defined
In its #interface section
In its viewDidLoad
Unfortunately all 3 locations had different compiling errors, none of which I could solve.
After I have help placing those two lines of code it also looks like I will need help concerning errors in the use of the 'self' keyword.
I am a novice objective-c programmer and I do not understand how to implement the 'Self' Keyword as used by this sample code into my project.
I have searched and tried to understand 'Self' Keyword usage so that I can modify the code sample for use in my project but I get lost in the subtleties of when/where and for what purpose 'self' serves.
I think that if someone helps me with 'where to put the two lines that add an observer' and with the 'self' usage issue, I may be able to actually understand how this code can get compiled into my project so I can test it.
Thanks
Dale
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I am new to programming, but I can't seem to get this to work.
When I try to run the method from another method every thing stops
-(void)rotateMmovment {
}
-(void)stickMove {
[self rotateMmovment];
stick.center = CGPointMake(stick.center.x + x, stick.center.y);
}
First you should check either your method is running or not via using NSLog.its does't seems that you are facing problem due to calling method which have empty body
-(void)rotateMmovment {
NSLog(#"My method is running");
}
-(void)stickMove {
[self rotateMmovment];
stick.center = CGPointMake(stick.center.x + x, stick.center.y);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a location app that already successfully asks the user to enable location services, and then can show them their coordinates on a button press as well. So I decided to play around with everything that is available in the CLLocationManager reference provided by xcode.
I decided to setup a bool method called "locationServicesEnabled". It returns a value of YES(1) or NO(0). I declared the method and then went to implement it. I am trying to have NSLog print the bool result out to the console when you open the app.
Here is how I declared the BOOL method in my ViewController.m file:
#interface ViewController () <CLLocationManagerDelegate>
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
+ (BOOL)locationServicesEnabled;
#end
And here is how I implemented the BOOL method in ViewController.m:
+ (BOOL)locationServicesEnabled{
[self locationServicesEnabled];
NSLog(#"%hhd", self.locationServicesEnabled);
return 0;
}
Why do you want to create an extra method? You could minimize the risk of errors by using already available ones: NSLog(#"Location services enabled: %d",[CLLocationManager locationServicesEnabled]);
I think your issue might be the fact that you are calling your function inside its self:
+ (BOOL)locationServicesEnabled
{
[self locationServicesEnabled]; <- should this be here
NSLog(#"%hhd", self.locationServicesEnabled);
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a UIButton which when pressed passes an integer to a simple method I have set up. However I keep on getting this error:
Implicit conversion of 'int' to 'id' is disallowed with ARC
Here is my code:
[self performSelector:#selector(show:) withObject:prev_image afterDelay:2.0];
The reason I'm not just doing [self show:prev_image] is because I want a delay before the method is called.
Thanks for your time, Dan.
You have two choices:
Change the show: method to take an NSNumber and then wrap prev_image in an NSNumber or
Use dispatch_after.
Code:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self show:prev_image];
});
prev_image is an int where an 'object' must be passed into the performSelector method.
I would advise that you do this:
[self performSelector:#selector(show:) withObject:#(prev_image) afterDelay:2.0f];
I would also recommend you change prev_image to prevImage whilst programming in Objective-C simply for style.
Using dispatch_after is not necessary here and you almost certainly want to stay as high level as possible when tackling problems in iOS development.
I apologise for being unclear.
You will also want to change the method signature and implementation of show:
- (void)show:(NSNumber *)number
{
NSInteger integerNumber = [number integerValue];
}
One solution is to use [NSNumber numberWithInt:prev_image] instead of prev_image. Also you'll need to change the show: method to to take an NSNumber instead of an int.
I have an entity which is a subclass of NSManagedObject called Event. I have also registered a few modeled attributes of this entity for KVO change notifications:
[self.event addObserver:self
forKeyPath:#"numGuests"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:&numGuestsContext];
[self.event addObserver:self
forKeyPath:#"checkedIn"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:&checkedInContext];
[self.event addObserver:self
forKeyPath:#"seatedCount"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:&seatedContext];
However it seems the observeValueForKeyPath method notification is getting triggered even though the value of NSKeyValueChangeOldKey and NSKeyValueChangeNewKey in the change dictionary are equal???
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSNumber *oldValue = [change valueForKey:NSKeyValueChangeOldKey];
NSNumber *newValue = [change valueForKey:NSKeyValueChangeNewKey];
if ([oldValue isEqualToNumber:newValue])
{
return;
}
For now i have resorted to just doing a quick sanity check to see if they are equal but i would like to understand why this notification is getting fired to begin with?
UPDATE: #jszumski mentioned in the comments that this most likely is happening because the objects are different although logically equal. The Event entity object always has the same address however the object i am observing, which is an attribute within the entity, keeps changing addresses although i am not sure why??
I am wondering if accessing this value in a bg query thread could cause Core Data to create new internal objects within the entity with the same value?
Simply, you are assuming the functionality of the NSKeyValueObservingOptions. They do not work as you are expecting them to. The flags you send only decide what the contents of the change dictionary are. The observer is always triggered whenever the setters are called.
Besides, when you are sending flags old and new, that really just says "I would like the old value, and new value. I don't care if they are equal or not".
Please read the reference to learn more:
NSKeyValueObservingOptions
These constants are passed to addObserver:forKeyPath:options:context:
and determine the values that are returned as part of the change
dictionary passed to an
observeValueForKeyPath:ofObject:change:context:. You can pass 0 if you
require no change dictionary values.