objective-c - using a boolean value from one class

2019-06-14 18:29发布

问题:

I've looked around and couldn't find a distinct answer to this question. So I'm asking here. I have two classes. ClassA and ClassB. I have a bool value in ClassB. I have a method in ClassA where it is looking for that value in ClassB to be True in order for the method to fire off. I'm not really sure how to get ClassA to see that value. Any help would be great. Thanks in advance!

回答1:

be careful with the "global definition". if your class must save the user settings, you can use: for save:

 NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
 [pref setBool:YES forKey:@"AudioIsON"];
 [pref synchronize];

for reading:

BOOL myBooleanSetting = [[NSUserDefaults standardUserDefaults] boolForKey:@"AudioIsON"];

instead of, is better to learn the delegate and the property.

hope this help you.