0

I have AViewController which is a rootViewController in it's navigationController. In AViewController, i have a objectA, i declare it like this:

@interface AViewController : ParentVC 
@property (strong, nonatomic) ObjectA *objectA;
@end 

I also have a BViewController, declared similar like this:

@interface BViewController : ParentVC 
@property (strong, nonatomic) ObjectA *objectA;
@end 

What i did is, in AViewController:

if (!_objectA)
    {
        _objectA = [ObjectA new];
    }
BViewController *bViewController = [[BViewController alloc] init];
bViewController.ojectA = _objectA;
[self.navigationController pushViewController:bViewController animated:YES];

While in BViewController, when i do this:

[self.navigationController popToRootViewControllerAnimated:NO];
        self.objectA = nil;

But the problem is, when i back into AViewController, the objectA is still not nil. It seems that i niled the objectA just in BViewController. These two objectA are separated. What i really want to do is, in BViewController just keep a point to the objectA in AViewController. How can i reach this? Thanks in advance.

7
  • when you pop to AViewController you want to set objectA = nil? Commented Feb 27, 2014 at 7:16
  • Yes, but the one in AViewController, not the one in BViewController Commented Feb 27, 2014 at 7:17
  • Basically what i want is not a individual instance of objectA in BViewController, but just a pointer to the objectA in AViewController Commented Feb 27, 2014 at 7:19
  • Of course they are separate variables. If you want to clear instance variable of AViewController from outside, you must export public method that does that and call it when needed. Commented Feb 27, 2014 at 7:21
  • please check my answer if it has any issues let me know. we can fix it Commented Feb 27, 2014 at 7:22

7 Answers 7

2

You should set it to weak rather than strong in BViewController, BViewController does not own the object, AViewController does...

Is there a good reason to nil out AViewcontrollers pointer to this object? This is not safe object management IMHO

edit addition.. ok after reading your comments i really think you should write a protocol, your design can be improved {or, as this is subjective, lets say more 'apple-like' :) }

i think you should add a protocol in BViewController.h

@protocol <NSObject> BViewControllerDelegate

-(void)viewControllerDidLogout:(BViewController *)controller;

@end

and inside BViewController's interface

@property id <BViewControllerDelegate> delegate

AViewController can adopt this protocol, set BViewControllers delegate to self before pushing it, then BViewController can use this to communicate back. Alternatively you could post a notification..

Sign up to request clarification or add additional context in comments.

8 Comments

I totally agree with Jeff. What you're trying to do is a sign of bad design. Anyway, the only way you can do what you're asking is having in BController a pointer to AController... or let ARC do the job with weak property, but this is very dirty.
I make it weak, but it doesn't work. The reason why i was doing this is: AViewController is a Login View, In BViewController when i do the logout, i need set something to nil in the login View
@triplegg Make the clearLogin method that cleans objectA in AVC and use it when popping BVC. Man, do you even read the comments?
@user3125367 Sorry mate, i missed your first comment, You said they are separate variables, but in the second comment you said i had the pointer already. I'm getting confused, are they individual instance? or objectA in BController is just a pointer, not a instance.
:) Look, an instance (or an object) is in heap. Both VCs hold [strong] pointer to it. Clearing one pointer does not clear another. If you want it your way, then at least make objectA public property and pass entire AVC instead of just it's objectA to BVC, so you can use self.avc.objectA there and say self.avc.objectA = nil and self.avc = nil later.
|
1

You better to try this below approach, where we will have AViewController weak reference then manipulate its contents

@interface BViewController : ParentVC
@property (weak, nonatomic) AViewController *AViewControllerObject;
@end


BViewController *bViewController = [[BViewController alloc] init];
bViewController.AViewControllerObject = self;
[self.navigationController pushViewController:bViewController animated:YES];


self.AViewControllerObject.objectA = nil;
[self.navigationController popToRootViewControllerAnimated:NO];

Comments

0
According your code 
[self.navigationController popToRootViewControllerAnimated:NO];
 self.objectA = nil; // this means you are making BViewController object as nil
If you want to nil AViewController object then

In AViewController 
-(void)viewWillAppear:(BOOL)animated
{
self.objectA = nil;
}


Hope this will help you

Comments

0

try this: AViewController 's objectA is weak. and refer to B's objectA;

@interface AViewController : ParentVC 
@property (weak, nonatomic) ObjectA *objectA;
@end

BViewController *bViewController = [[BViewController alloc] init];
bViewController.objectA = [ObjectA new];
self.objectA = bViewController.objectA;
[self.navigationController pushViewController:bViewController animated:YES];

Comments

0

If you want to override ARC keeping the object reference, you could retain it and release in B controller, this way when you get back to A the objectA is already destroyed. You can do it like :

bViewController.objectA = [_objectA retain];

And in B

[_objectA release];

Comments

0

You have @property (strong, nonatomic) ObjectA *objectA; in both of your view controllers,

You are expecting to _objectA of AViewController to become nil, while you are actually setting self.objectA of BViewController to nil.

bViewController.ojectA = _objectA;
_objectA = nil; 
[self.navigationController pushViewController:bViewController animated:YES];

Comments

0

You need to do in AViewController - (void)viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.objectA = bViewController.ojectA;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.