0

I'm working on a project in objective C where i have to modificate some variables which are in a view controller from uiviews.

So i've tried somethings like this :

ViewController.h :

@property (nonatomic) bool Contact;

One of the UIViews :

ViewController * View;
View.Contact = YES;

I've also tried to make a setter method like this in the ViewController :

-(void) SetterContact:(bool)boolean;

And so to modificate from a UIView like this :

[View SetterContact:YES];

But it's looking working. I've read that i have to init the object in which is containt the variable, but in memory management it's not really good to make some initializations from object who are already actives no ? So if View is already init, i'm not going to call the init method from another UIView no ?

Thanks for your help !

user2057209
  • 345
  • 1
  • 6
  • 19

3 Answers3

2

If you want bool variable to be accessible from other viewController.Then simply wirte it as :-

@property BOOL Contact;

and make an object of ViewController in which you have declared contact variable as BOOL and access this variable using like this:-

OtherViewController *otherViewController=[[OtherViewController alloc] init];
otherViewController.Contact=YES;

As it is a instance variable it has to be accessed using class object.

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
  • Returns an error : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' but i dont' have any NSarray in those two view controllers... – user2057209 Jun 10 '13 at 08:08
  • paste you viewController's code on pastebin so that I can see it deeply what you have done in your code – Prince Kumar Sharma Jun 10 '13 at 08:18
  • I just write : if (!World) { World = [[World_1_ViewController alloc] init]; } World.Contact = YES; Furthermore, that code looks like working in another viewcontroller -_-' – user2057209 Jun 10 '13 at 10:17
  • Ok, you need to check in currentViewController in which error displaying.If on other ViewController is working there is no issue in declaring bool varible. – Prince Kumar Sharma Jun 10 '13 at 10:21
  • I have checked, and all is the same as in my other view controller which is working. Really don't know how to fix it... – user2057209 Jun 10 '13 at 10:50
  • strange error.Bool variable giving NSArray error?Need to find out deeply.Run app in zombie profile.And check where it quits – Prince Kumar Sharma Jun 10 '13 at 10:52
  • No, it's only those lines which are making fail : if (!World) { World = [[World_1_ViewController alloc] init]; } And if i don't put them, world object is not init, and World.Contact = YES; couldn't work :/ – user2057209 Jun 10 '13 at 10:56
  • you mean if (!World) { World = [[World_1_ViewController alloc] init]; } line causing error – Prince Kumar Sharma Jun 10 '13 at 10:57
  • use this one :-- World_1_ViewController *world=[[World_1_ViewController alloc] init]; world.Contact=YES; – Prince Kumar Sharma Jun 10 '13 at 10:59
  • Same error when i try World_1_ViewController *world=[[World_1_ViewController alloc] init]; world.Contact=YES; Really strange... – user2057209 Jun 10 '13 at 11:39
  • I really don't know how to fix this sort of error, because it's work in another view controller with other parameters defines like here. – user2057209 Jun 10 '13 at 12:02
  • I've done the methode which is here... : http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – user2057209 Jun 10 '13 at 12:10
  • you can try initwithnibname I am not sure but may be it help you. – Prince Kumar Sharma Jun 11 '13 at 03:44
  • I don't have nib, so it didn't change anything :/ – user2057209 Jun 11 '13 at 07:54
0
  1. Respect naming conventions
    @property (nonatomic,retain) UIViewController *myController;

don't forget to synthesize
@synthesize myController = _myController;

  1. If you want to implement your own setter do this: respect the naming convention
    -(void)setMyController:(UIViewController*)controller;

  2. or if by any bizarre reason you can't respect naming convention you can point the property to the method you want
    @property (nonatomic,retain,setter=myBizarreSetterMethod:) UIViewController *myController;

this can help you out as well question in stackoverflow

Community
  • 1
  • 1
Pacu
  • 1,985
  • 20
  • 33
0

use @property (nonatomic, assign, getter = isContact) BOOL contact; in your .h file.

lukas
  • 2,300
  • 6
  • 28
  • 41