0

Yes, I know that this question is very popular here and has been given a lot of answers to this question, and yes, I was here Passing Data between View Controllers. But I can't do it for a long time.

in ViewControllerB.h I create a property for the BOOL

@property(nonatomic) BOOL *someBool;

ViewControllerA.m:

#import "ViewControllerB.h"

ViewControllerB *viewControllerB = [[ViewControllerB alloc] init];
viewControllerB.someBool = YES;
[self.navigationController pushViewController:viewControllerB  animated:YES];

In ViewControllerB.m ViewDidLoad:

NSLog(@"%@", self.someBool);

But xCode give me error on this line ( NSLog(@"%@", self.someBool);) and say: Thread 1:EXC_BAD_ACCESS (code =2). What am I doing wrong?

Community
  • 1
  • 1
daleijn
  • 718
  • 13
  • 22
  • @rokjarc Why are you suggesting that this be moved to a singleton? There is no basis for such a suggestion in this case. – rmaddy Nov 14 '13 at 17:25
  • @rmaddy: ok, i admit i'm exaggerating a bit. But this particular case is just a part of the bigger app. I believe there is no place for data in views or controllers. The sooner people learn to use MVC, the less questions on passing data between viewcontrollers will be :) – Rok Jarc Nov 14 '13 at 17:40
  • @rmaddy: but yes, it was a bad suggestion. i'm removing it. – Rok Jarc Nov 14 '13 at 17:42
  • 1
    @rokjarc While I agree that proper use of the M in MVC is important, view controllers do have their own state and properties. Not everything belongs in the model. – rmaddy Nov 14 '13 at 17:48
  • True. I got carried away. – Rok Jarc Nov 14 '13 at 17:50

3 Answers3

5

Your property is a pointer. It shouldn't be. Change this:

@property(nonatomic) BOOL *someBool;

to:

@property(nonatomic) BOOL someBool;

The log should be:

NSLog(@"%d", self.someBool);

Only use %@ with objects.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • BTW - it is important to understand why and when you use a pointer and why and when you don't. You don't in this case because `BOOL` is a native type and not a class. Except for very rare conditions, when you define a property, never use a pointer for native types and of course you must use a pointer with object types. – rmaddy Nov 14 '13 at 17:51
2

Declare it as a BOOL, not a pointer to a BOOL:

@property(nonatomic) BOOL someBool;
DrummerB
  • 39,814
  • 12
  • 105
  • 142
1

You either need to declare it as a primitive and get rid of the * or store it as an object by wrapping it as an NSNumber

@property (strong, nonatomic) NSNumber *someBool

Then you'd write someBool.boolValue to grab its value

Peter Foti
  • 5,526
  • 6
  • 34
  • 47