BOOL value

I am working on an application and I am stuck at the point where it seems I cannot get the BOOL value set in the class. I spent too much time on this, through all the questions I found that seem to cover this question.

It is bad that I get something, but not what I need (I get 0, which means, I think the value was not restored correctly, since it should be 1).

What I tried:

  • pass a pointer to my first class and access my BOOL as follows:

    //in some method
    self.pointerFirstClass.myBOOL;
    NSLog(@"%d", firstClass.myBOOL); => This gives 0!
    

declaring it (talking about a pointer) as a property in my second class (and importing the h. file from my first class, where my BOOL is declared as a property too):

    @property FirstClass *pointerFirstClass;

But I got 0 using this.

  • , , BOOL

    //in some method
    FirstClass *firstClass = [[FirstClass alloc] init];
    if (firstClass.myBOOL){
          //Do something
    }
    NSLog(@"%d", firstClass.myBOOL); => This gives 0!
    

    0.

Booleans , C, , - , , getter , .

getBOOLValue - (BOOL) BOOL . .

  • - ?

  • , ?

, , , , - , .

EDIT:

. , 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
       }
    }
  • , BOOL AppDelegate.

.

+4
3

:

self.pointerFirstClass.myBOOL;
NSLog(@"%d", firstClass.myBOOL); => This gives 0!

. , . , , , . :

self.pointerFirstClass = [[FirstClass alloc] init];
self.pointerFirstClass.myBOOL = YES;
NSLog(@"myBOOL = %d", self.pointerFirstClass.myBOOL);

, , self.pointerFirstClass . , , myBOOL .

: , . :

- (IBAction)onClick:(id)sender {
       AppDelegate *appDelegate = [[AppDelegate alloc] init];

, , , . - - , , , . -, AppDelegate. , , AppDelegate, , . , , .

, , , :

[[UIApplication sharedApplication] delegate];

, :

- (IBAction)onClick:(id)sender {
   AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];;

   if (appDelegate.inRegion) {
      [self performSegueWithIdentifier:@"WelcomeToDetection" sender:self];
   }
   // note: you don't need an else clause if it doesn't do anything
}

, , , inRegion, .

+4

UPDATE. , , appDelegate, ...

- (IBAction)onClick:(id)sender {
       AppDelegate *appDelegate = [[AppDelegate alloc] init];

....

- (IBAction)onClick:(id)sender {
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]

-

, ? , ...

self.pointerFirstClass.myBOOL;
NSLog(@"%d", firstClass.myBOOL); => This gives 0!

NSLog(@"%d", self.pointerFirstClass.myBOOL);

...

@property FirstClass *pointerFirstClass;

, ,

@property (nonatomic,strong) FirstClass *pointerFirstClass;

...

FirstClass *firstClass = [[FirstClass alloc] init];
if (firstClass.myBOOL){
      //Do something
}
NSLog(@"%d", firstClass.myBOOL); => This gives 0!

FirstClass, , YES init, false

, , ?

+2

, myBOOL 1. , -

@implement FirstClass
- (id)init
{
    self = [super init];
    if(self) {
        _myBOOL = 1;
    }
    return self;
}
// Other methods
@end

EDIT:

- 0.

- (IBAction)onClick:(id)sender {
   AppDelegate *appDelegate = [[AppDelegate alloc] init]; // this is the problem.
                                                          // you create a new appdelegate,
                                                          // and never call locationManager:didDetermineState:forRegion:

  if (appDelegate.inRegion) {

     [self performSegueWithIdentifier:@"WelcomeToDetection" sender:self];

   }
   else
 {
      //Do Nothing
   }
}

rewrite your code as follows:

- (IBAction)onClick:(id)sender {
   AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

  if (appDelegate.inRegion) {
     [self performSegueWithIdentifier:@"WelcomeToDetection" sender:self];

   }
   else
 {
      //Do Nothing
   }
}
+1
source

Source: https://habr.com/ru/post/1532851/


All Articles