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 !

3 Answers 3

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.

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

14 Comments

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...
paste you viewController's code on pastebin so that I can see it deeply what you have done in your code
I just write : if (!World) { World = [[World_1_ViewController alloc] init]; } World.Contact = YES; Furthermore, that code looks like working in another viewcontroller -_-'
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.
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...
|
0

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

7 Comments

in my project i use NSSString instead @"YES"
so @property (nonatomic, retain) NSString isContact;
ViewController *view = [ViewController alloc] init]; view.isContact = @"YES";
you dont need to create your own get/setter and no need to init the NSString isContact nor synthesize.
How could i do a setter ? Easily
|
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

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.