我正在开发一款应用程序,遇到了一个问题:无法检索设置在类中的BOOL值。我已经花费了很多时间,并查看了所有相关问题,但仍无法解决。
不幸的是,我得到了一些信息,但并非我需要的内容(我得到的是0,这意味着该值没有正确地被检索出来,应该是1)。
我尝试了以下方法:
但是我使用这个方法得到的结果是0。
我尝试在我的第一个类中编写了一个名为getBOOLValue的方法,然后在我的第二个类中调用此方法并将其分配给该类中的BOOL。但结果并不理想。
我已经尽力想到了各种方法来解决这个问题,但似乎并不是很难,希望这只是一个简单的错误。
编辑:
以下是一些实际代码。我正在两个文件之间工作,一个是AppDelegate(确实是这个文件),另一个是WelcomeViewController(也就是一个VC)。
AppDelegate.h
不幸的是,我得到了一些信息,但并非我需要的内容(我得到的是0,这意味着该值没有正确地被检索出来,应该是1)。
我尝试了以下方法:
pass a pointer to my first class and access to my BOOL like this:
//in some method self.pointerFirstClass.myBOOL; NSLog(@"%d", firstClass.myBOOL); => This gives 0!
@property FirstClass *pointerFirstClass;
但是我使用这个方法得到的结果是0。
The other shot I gave was add my BOOL in the first class and create an instance of the class in my second class
//in some method FirstClass *firstClass = [[FirstClass alloc] init]; if (firstClass.myBOOL){ //Do something } NSLog(@"%d", firstClass.myBOOL); => This gives 0!
But I got 0 too.
我尝试在我的第一个类中编写了一个名为getBOOLValue的方法,然后在我的第二个类中调用此方法并将其分配给该类中的BOOL。但结果并不理想。
我已经尽力想到了各种方法来解决这个问题,但似乎并不是很难,希望这只是一个简单的错误。
编辑:
以下是一些实际代码。我正在两个文件之间工作,一个是AppDelegate(确实是这个文件),另一个是WelcomeViewController(也就是一个VC)。
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
BOOL inRegion; //thought of this making my BOOL as a property of AppDelegate
}
@property (strong, nonatomic) UIWindow *window;
@property BOOL inRegion; //Declaring my BOOL here to make it accessible for another class
- (BOOL)getBOOLValue; //An attempt to pass my BOOL value
AppDelegate.m
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if (state == CLRegionStateInside)
{
self.inRegion = YES; //Set my BOOL to TRUE
}
else if (state == CLRegionStateOutside)
{
self.inRegion = NO; //Else set it to False
}
- (BOOL)getBOOLValue
{
return inRegion; //Tried to create a custome "getter"
}
WelcomeViewControler.m(我在.h文件中没有作任何更改) 我试过很多方法,现在这是我的代码的最新版本。
//Simply trying to do a Segue on a condition...
- (IBAction)onClick:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init];
if (appDelegate.inRegion) {
[self performSegueWithIdentifier:@"WelcomeToDetection" sender:self];
}
else
{
//Do Nothing
}
}
- 如上所述,我想获取AppDelegate的BOOL值。
谢谢。
NSLog(@"Bool is: %@", myBool ? @"YES" : @"NO");
- AbizernfirstClass
为空,则对其上的myBOOL
的引用将返回零。(当然,零值也表示“false”值,因此从打印的零值无法得出任何真实证据。) - Hot Licks