WatchKit how to check current state of WKInterfaceSwitch - ios

Im trying to figure out if my instance of WKInterfaceSwitch is currently in on or off position

You can't do that. You need to track with a variable the status of the WKInterfaceSwitch in your code.
Let's say your default value for a WKInterfaceSwitch is false.
In your awakeWithContext: method do this:
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
self.switchStatus = NO;
}
In Objective-C you would declare a property with a BOOL value.
#property (nonatomic, assign) BOOL switchStatus;
Then create an action from your Switch object to your header file.
- (IBAction)valueChanged:(BOOL)value;
And in the implementation file write.
- (IBAction)valueChanged:(BOOL)value {
self.switchStatus = value;
}
You are now able to check the status of your Switch by just using self.switchStatus for example like this:
NSLog(#"Switch is now: %#", self.switchStatus ? #"true" : #"false");
I hope this helps.

Related

get dynamic bool variable from another class iOS

I have Location Tracking class and another class with listing. I need to check if locationService is running when entering listView. In the locationService class I've added boolean to track service start and stop. But I can't track this boolean variable from listView class.
here is my code how I am doing now, but it takes only initial value of the locationService class
//listView.m
#property (strong,nonatomic) LocationTracker * trackerClass;
- (void)viewWillAppear:(BOOL)animated{
self.trackerClass = [[LocationTracker alloc] init];
if([self.trackerClass getTrackerStatus]==1)
NSLog(#"LOCATION SERVICE STATUS IS RUNNING");
else
NSLog(#"LOCATION SERVICE STATUS IS STOPPED");
}
//locationService.h
#property (nonatomic, readwrite) int trackerStatus;
- (int)getTrackerStatus;
//locationService.m
- (void)stopLocationTracking {_trackerStatus=0;}
- (void)startLocationTracking {_trackerStatus=1;}
- (int)getTrackerStatus{
return _trackerStatus;
}
thank you in advance!
Try to set this boolean variable as a local variable and create a simple static getter to get this bool. your LocationTracker will change the value of the variable and you can get it from anywhere.
#implementation LocationTracker
BOOL isStart;
//Dont forget to delclear this methode in the .h file
+(BOOL)isStart{
return isStart;
}
your code...
Any where in your app just import your calss and wirte:
BOOL isStart = [LocationTracker isStart];
//The getting vaiable should be the updated bool in you LocationTracker class
Try deleting
- (int)getTrackerStatus{
return _trackerStatus;
}

iOS Theos Hook and set ivar/property

I have the following class that I am hooking, I am trying to figure out how to hook and set m_proxyPort. I can read it without issue, but how about if I want to change it?
There is a setter for the 3 NSString ivars, but theres no setter for m_proxyPort, would like to know if there is a way to set that ivar?
#interface DDURLProtocol : NSURLProtocol <NSURLAuthenticationChallengeSender, DDURLProtocolHttpAdapterDelegate>
{
int m_proxyPort;
NSString *_proxyHost;
NSString *_proxyUsername;
NSString *_proxyPassword;
}
%hook DDURLProtocol
- (void) check
{
[self setProxyHost:#"127.0.0.1"];
int pp = MSHookIvar<int>(self, "m_proxyPort");
NSLog(#"proxyPort: %d", pp);
// How to set m_proxyPort????
}
%end
You can set it the same way you get it.
MSHookIvar<int>(self, "m_proxyPort") = 23;

Trying to implement Key-Value Observing for first time, get some error

I have trouble implementing a Key-Value Observer at my attempt to follow the MVC pattern. I have a controller class, a model class and a view class. I update my model from the controller class and I want to put a key value observer in my view class to monitor when a NSMutableArray changes in model (like through addObject) and then redraw itself automatically. I used answer in this thread to guide me: How to add observer on NSMutableArray?
Code so far:
From my Scene (using sprite kit if it matters). Setting of letters will be done from Ctrl class, this is just to test.
BarCtrl *barCtrl = [[BarCtrl alloc] init];
BarModel *barModel = [[BarModel alloc] init];
BarView *barView = [[BarView alloc] init];
barCtrl.barModel = barModel;
barCtrl.barView = barView;
barView.barModel = barModel;
ScrabbleDeck *sd = [[ScrabbleDeck alloc] init];
if([barModel addLetter:[sd getLetter] onSide:BarModelSideRight])
NSLog(#"Added letter");
BarModel.h
#import <Foundation/Foundation.h>
#import "Letter.h"
typedef NS_ENUM(int, BarModelSide) {
BarModelSideLeft,
BarModelSideRight
};
#interface BarModel : NSObject
#property (nonatomic, strong) NSMutableArray *addedLetters;
- (instancetype)init;
- (BOOL) addLetter: (Letter*) letter onSide: (BarModelSide) side;
#end
BarModel.m
#import "BarModel.h"
#interface BarModel ()
#property (nonatomic) int capacity;
#end
#implementation BarModel
- (instancetype)init
{
self = [super init];
if (self) {
self.capacity = letterCapacity;
_addedLetters = [[NSMutableArray alloc] init];
}
return self;
}
// We'll use automatic notifications for this example
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
{
if ([key isEqualToString:#"arrayLetter"]) {
return YES;
}
return [super automaticallyNotifiesObserversForKey:key];
}
- (BOOL) addLetter: (Letter*) letter onSide: (BarModelSide) side{
if([_addedLetters count] > _capacity){
return FALSE;
}
switch (side) {
case BarModelSideLeft:
[_addedLetters insertObject:letter atIndex:0];
return TRUE;
break;
case BarModelSideRight:
[_addedLetters addObject:letter];
return TRUE;
break;
default:
return FALSE;
break;
}
}
// These methods enable KVC compliance
- (void)insertObject:(id)object inDataAtIndex:(NSUInteger)index
{
self.addedLetters[index] = object;
}
- (void)removeObjectFromDataAtIndex:(NSUInteger)index
{
[self.addedLetters removeObjectAtIndex:index];
}
- (id)objectInDataAtIndex:(NSUInteger)index
{
return self.addedLetters[index];
}
- (NSArray *)dataAtIndexes:(NSIndexSet *)indexes
{
return [self.addedLetters objectsAtIndexes:indexes];
}
- (NSUInteger)countOfData
{
return [self.addedLetters count];
}
#end
BarView.h
#import <SpriteKit/SpriteKit.h>
#import "BarModel.h"
#interface BarView : SKSpriteNode
#property (nonatomic, strong) BarModel *barModel;
#end
BarView.m
#import "BarView.h"
#implementation BarView
static char MyObservationContext;
- (instancetype)init
{
self = [super init];
if (self) {
//_barModel = [[BarModel alloc] init];
}
return self;
}
-(void)setBarModel:(BarModel *)barModel{
if(_barModel != barModel)
_barModel = barModel;
[_barModel addObserver:self
forKeyPath:#"arrayLetter"
options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew)
context:&MyObservationContext];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// Check if our class, rather than superclass or someone else, added as observer
if (context == &MyObservationContext) {
// Check that the key path is what we want
if ([keyPath isEqualToString:#"arrayLetter"]) {
// Verify we're observing the correct object
if (object == self.barModel) {
[self draw:change];
}
}
}
else {
// Otherwise, call up to superclass implementation
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void) draw: (NSDictionary*) change{
NSLog(#"KVO for our container property, change dictionary is %#", change);
}
#end
When I ru this I get this "error":
2014-08-31 00:23:02.828 Testing[329:60b] Added letter
2014-08-31 00:23:02.830 Testing[329:60b] An instance 0x17803d340 of class BarModel was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x17804eb50> (
<NSKeyValueObservance 0x1780cf180: Observer: 0x178111670, Key path: arrayLetter, Options: <New: YES, Old: YES, Prior: NO> Context: 0x100101428, Property: 0x17804eb80>
I tried to follow the instructions in error but can not find where to set break point. Please help me figure this out!
The error is pretty descriptive. You add self as an observer of a BarModel object. At some point that object gets deallocated. But you never remove self as an observer by calling removeObserver:forKeyPath:context:. You need to do that.
First, in setBarModel, make sure to remove self as an observer of the previous value of _barModel.
Next, you probably need to add a dealloc method that does the same thing.
There are multiple problems with the code. In addition to what Tom Harrington said with respect to the specific error that was logged about failing to remove the observation:
You implemented the indexed collection accessors for a (non-existent) property named "data". That is, they have "Data" in their name where the property name should be.
The indexed collection property is addedLetters. So, the indexed collection mutating accessors should be:
- (void)insertObject:(id)object inAddedLettersAtIndex:(NSUInteger)index;
- (void)removeObjectFromAddedLettersAtIndex:(NSUInteger)index;
You don't really need the non-mutating accessors, since you have an array-type public property with a normal getter (i.e. -addedLetters).
By the way, that property is of type NSMutableArray which it should not be. The property should be of type NSArray, backed by an instance variable of type NSMutableArray. That is, the mutability of the type (as opposed to the property) should not be exposed through the public interface. When you do this, you have to manually declare the instance variable (since it should differ from the type of the property and auto-synthesis will get it wrong), make the property copy instead of strong, and implement the setter yourself to do a mutable copy of the passed-in immutable array:
- (void) setAddedLetters:(NSArray*)addedLetters
{
if (addedLetters != _addedLetters)
_addedLetters = [addedLetters mutableCopy];
}
Once you have implemented the indexed collection mutating accessors with the correct names, you must use only those methods to mutate the collection (after initialization). In particular, your -addLetter:onSide: method must not directly operate on the _addedLetters instance variable. This is the part that makes the class KVO-compliant for that property. The mere presence of the indexed collection mutating accessors does not help. They must be used for all actual mutations.
Your implementation of +automaticallyNotifiesObserversForKey: is redundant. Automatic notification is the default.
The BarView class is key-value observing a key path "arrayLetter" on its _barModel object, but that's not the name of the property on BarModel. I suppose you meant to use the key path "addedLetters".
Finally, for proper adherence to MVC design, your view should not have a reference to your model. It should have a reference to the controller. The controller can reflect the model to the view (or, in theory, adapt a model of a different internal design to what the view expects). Or, in a more traditional non-KVO approach, the controller would actually tell the view when something has changed and give it the updated data it should show.
So, your controller could expose its own addedLetters property:
#property (readonly, copy, nonatomic) NSArray* addedLetters;
It could be implemented as a derived property, forwarded through to the _barModel object:
+ (NSSet*)keyPathsForValuesAffectingAddedLetters
{
return [NSSet setWithObject:#"barModel.addedLetters"];
}
- (NSArray*)addedLetters
{
return self.barModel.addedLetters;
}
Then, the view would have a reference to the controller and not the model, and it would key-value observe the "addedLetters" key path on the controller.

iOS Define SEL from another class

I'm trying to have another object call a selector. I'm attempting to define this selector from another class by defining the selector property. It doesn't seem to be working like I expect.
ComboBox.h
#property (nonatomic) SEL onComboSelect;
ComboBox.m
-(void)doneClicked:(id) sender
{
[textField resignFirstResponder]; //hides the pickerView
NSLog(#"DONE CLICKED CALLED");
[self performSelector:#selector(onComboSelect)];
}
OtherClass.h
#interface OtherClass : BaseViewController
{
ComboBox *combo;
}
-(void)comboSelector;
OtherClass.m
// in viewDidLoad
combo = [[ComboBox alloc] init];
combo.onComboSelect = #selector(comboSelector);
-(void)comboSelector
{
NSLog(#"COMBO SELECTOR");
}
I see "DONE CLICK CALLED" in the logs, but not "COMBO SELECTOR". So I know doneClicked is being called, but the selector doesn't seem to be working. What am I doing wrong? Is there a better way to do this?
A #selector is just a method name - it does not include any context about the class on which it is defined. So this [self performSelector:#selector(onComboSelect)] is just invoking the method on self. In addition to the selector, you also need a reference to the object on which you want to call it.
Notice how some built-in classes (like UIControl) take both a target object and action selector.
There are a 2 major issues in your code.
1.
onComboSelect is a SEL so no need to use the #selector again.
Instead of:
[self performSelector:#selector(onComboSelect)];
Use :
[self performSelector:onComboSelect];
2.
You are calling the selector on self from ComboBox class, so it'll call the selector on ComboBox object (if defined) not on OtherClass object
Your answers were helpful. Here is what I did:
added to ComboBox.h
#property (nonatomic, weak) UIViewController *parentViewController;
added to ComboBox.m
-(void)doneClicked:(id) sender
{
[textField resignFirstResponder]; //hides the pickerView
if ([parentViewController respondsToSelector:#selector(comboSelector)])
[parentViewController performSelector:#selector(comboSelector)];
}
added to OtherClass.m
combo.parentViewController = self;
#property (nonatomic) SEL onComboSelect <-- This property is not needed in ComboBox.h.
SEL is a point of objc_seletor,and in object_seletor runtime can find the
objc_method ,that define
objc_method {
SEL method_name OBJC2_UNAVAILABLE;
char *method_types OBJC2_UNAVAILABLE;
IMP method_imp OBJC2_UNAVAILABLE;
}
and IMP is the point of method,and you can find this in "runtime.h" file.
Any noe kow how to find objc_method by object_seletor ? and I cannot find the define of objc_seletor struct.

How to set Custom Properties in iOS? [duplicate]

This question already has an answer here:
How to update an NSManagedObject whenever a specific attribute is changed?
(1 answer)
Closed 8 years ago.
I am using Core Data and would like to run some custom code when setting a property.
#interface FTRecord : NSManagedObject
#property (nonatomic) NSTimeInterval timestamp;
#implementation FTRecord
#dynamic timestamp;
-(void)setTimestamp:(NSTimeInterval)newTimestamp
{
//run custom code....
//and now how to pass the value to the actual property?
[self setTimestamp:newTimestamp];
}
In this case I have defined the setter body for the timestamp property. But how do I set the value of the property without running into a recursion loop?
There's a magical accessor generated for each property, which in your case would be called setPrimitiveTimestamp: which you can use for this. Take a look at the docs for NSManagedObject's - (void)setPrimitiveValue:(id)value forKey:(NSString *)key.
So, you want:
-(void)setTimestamp:(NSTimeInterval)newTimestamp
{
//run custom code....
//and now how to pass the value to the actual property?
[self willChangeValueForKey:#"timestamp"];
[self setPrimitiveTimestamp:newTimestamp];
[self didChangeValueForKey:#"timestamp"];
}

Resources