daleijn
daleijn

Reputation: 738

Passing Data between View Controllers (yes, I know... )

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?

Upvotes: 0

Views: 84

Answers (3)

Peter Foti
Peter Foti

Reputation: 5654

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

Upvotes: 1

rmaddy
rmaddy

Reputation: 318774

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.

Upvotes: 5

DrummerB
DrummerB

Reputation: 40201

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

@property(nonatomic) BOOL someBool;

Upvotes: 2

Related Questions