-1

I have got two view controllers say NewGroupView and GroupsTabView. I have to set bool value of second view controller from my first view controller. This is how im setting the values of the second view controllers from my first view controller.

NewGroupView *groupView = [self.storyboard instantiateViewControllerWithIdentifier:@"NewGroupViewNC"];
NewGroupView *ngv = [NewGroupView new];
ngv->isGroupViewMode=YES;
groupView.isGroupViewMode = YES;
[self presentViewController:groupView animated:YES completion:nil];

Following is the interface declaration of my second view controller.

@interface NewGroupView : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,assign)    BOOL  isGroupViewMode;
@end

in my viewDidLoad method, I tried it print it as follows,

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(isGroupViewMode ? @"Yes" : @"No");
}

But the issue is that it is always printing NO though I have set it to be YES. why is that the assigned value is not persisting?

  • 1
    @meaning-matters stop write useless comments and answers. `->` is used to access instance variables. – Vyachaslav Gerchicov Feb 14 '17 at 12:29
  • check this ans: http://stackoverflow.com/a/9736559/4831524 – Antony Raphel Feb 14 '17 at 12:31
  • @VyachaslavGerchicov Actually dot notation is used as setter/getter, and -> is not a setter . OP, why are you instantiating a NewGroupView and at the same time creating another one with new method? –  Feb 14 '17 at 12:33
  • @Sneak replace `.` with `->` and property with instance variable declaration will work. At least just tried to set and get variable in the same thread – Vyachaslav Gerchicov Feb 14 '17 at 12:41
  • @VyachaslavGerchicov I was talking about it not being the same as using dot notation, since it calls the setter and getter methods it differs from -> , I don't know if you edited the comment or if I am to tired and read your comment all wrong...however even if that isnt the problem in this case.. –  Feb 14 '17 at 12:49
  • How to set and get variable in the same thread? –  Feb 14 '17 at 12:49
  • groupView->isGroupViewMode=YES; NSLog(groupView->isGroupViewMode ? @"Yes" : @"No"); – Vyachaslav Gerchicov Feb 14 '17 at 12:52
  • the above code prints no though I have set it to be yes..why so? –  Feb 14 '17 at 12:58
  • why downvoted my question? :( –  Feb 14 '17 at 13:00
  • @user31231234124 Sneak already answered - `instantiateViewControllerWithIdentifier` calls `viewDidLoad` implicitly and so you get `NO` before you set it to `YES`. – Vyachaslav Gerchicov Feb 14 '17 at 13:00
  • any alternatives? –  Feb 14 '17 at 13:01
  • for example move this code to `viewDidAppear` (as was already mentioned) – Vyachaslav Gerchicov Feb 14 '17 at 13:02
  • @user31231234124 Dude , alternatives for what? Thats the solution to your problem in my answer. You can not NSLog it in viewDidLoad, since it has not been set yet. add **viewDidAppear** and you will get the correct NSLog value there instead –  Feb 14 '17 at 13:02
  • ok thanks... i have handled it myself. –  Feb 14 '17 at 13:10

3 Answers3

1

Create a property in NewGroupView with code:

@property (nonatomic, assign) BOOL isGroupViewMode;

And write the following code in first view controller :

NewGroupView *groupView = [self.storyboard instantiateViewControllerWithIdentifier:@"NewGroupViewNC"];

groupView.isGroupViewMode = YES;
[self presentViewController:groupView animated:YES completion:nil]; 

you can check it using log from the second view controller (viewDidLoad method):

NSLog(@"%@",isGroupViewMode);

Alternativelty you can store the boolean value into NSUserDefault from first view controller and access this value from the second view controller.

Nirbhay Singh
  • 1,204
  • 1
  • 12
  • 16
  • 1
    @user31231234124 it seems your code is correct but you have a problem in another place. 1)check which is executed first - `viewDidLoad` or setting a bool variable to `YES`; 2)you set to YES 2 different variables - which one and when do you try to check? – Vyachaslav Gerchicov Feb 14 '17 at 12:51
  • @VyachaslavGerchicov I agree with you. The viewDidLoad method will get called in instantiateViewControllerWithIdentifier, and he sets the BOOL value AFTER the viewDidLoad. Should move the NSLog to viewDidAppear or something. You should add it as an answer since that is the correct answer to this question btw. –  Feb 14 '17 at 12:52
  • `@property BOOL isGroupViewMode;` in second VC. – Antony Raphel Feb 14 '17 at 12:54
  • @AntonyRaphel why? – Vyachaslav Gerchicov Feb 14 '17 at 12:56
1
NewGroupView *groupView = [self.storyboard instantiateViewControllerWithIdentifier:@"NewGroupViewNC"];

Above line calls viewDidLoad, and this is where you print NSLog , the BOOL value is still "NO" , since it has not been set yet.

Now you call:

groupView.isGroupViewMode = YES;

But you can not see it in your NSLog since viewDidLoad has already been called from before.

EDIT:

As commented by @VyachaslavGerchicov

the initial case with using of -> will work too.

Community
  • 1
  • 1
0

Declare Your BOOL Property in .h file

@interface NewGroupView : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,assign)    BOOL  isGroupViewMode;
@end

then synthesize it in .m file

@synthesize isGroupViewMode;


NewGroupView *groupView = (NewGroupView *)[self.storyboard instantiateViewControllerWithIdentifier:@"NewGroupViewNC"];
groupView.isGroupViewMode=YES;
[self presentViewController:groupView animated:YES completion:nil];

& Hope this will help you.

Mukesh Lokare
  • 2,159
  • 26
  • 38