Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, here's my answer. This expands on the one given by Lanc.</p> <p>Firstly, you need to decide what the scope of myObj1 should be. Do you want one per instance of the class or only one for the whole application. If it's per instance of the class, make an instance variable and have a property that creates it on demand. e.g.</p> <pre><code>@interface MyViewController : UIViewController @property (nonatomic, readonly, retain) Obj1* myObj1; // other stuff @end @implementation MyViewController { @private Obj1* myObj1; } -(Obj1*) myObj1 { @synchronized(self) // if you know you are single threaded you can omit the @synchronized block { if (myObj1 == nil) { myObj1 = [[Obj1 alloc] init]; } } return myObj1; } - (IBAction)btnCreateObj1:(UIButton *)sender { [[self myObj1] increment:intVal]; [[self myObj1] restring:@"orig string 1"]; NSString * newLabel = [self.labelObject1.text stringByAppendingFormat:@"value:%d string:%@",myObj1.value,myObj1.someString]; self.labelObject1.text = newLabel; } - (IBAction)btnIncrementObj1:(UIButton *)sender { [[self myObj1] increment: [self.textField.text intValue]]; } </code></pre> <p>If you need a singleton (i.e. only one object per program), you can use a static variable as per wizH's answer but I prefer using a method to access it. So the following will work:</p> <pre><code>@interface MyViewController : UIViewController @property (nonatomic, readonly, retain) Obj1* myObj1; // other stuff @end @implementation MyViewController -(Obj1*) myObj1 { static Obj1* myObj1 = nil; // instance var moved to be a static variable @synchronized([MyViewController class) // if you know you are single threaded you can omit the @synchronized block { if (myObj1 == nil) { myObj1 = [[Obj1 alloc] init]; } } return myObj1; } - (IBAction)btnCreateObj1:(UIButton *)sender { [[self myObj1] increment:intVal]; [[self myObj1] restring:@"orig string 1"]; NSString * newLabel = [self.labelObject1.text stringByAppendingFormat:@"value:%d string:%@",myObj1.value,myObj1.someString]; self.labelObject1.text = newLabel; } - (IBAction)btnIncrementObj1:(UIButton *)sender { [[self myObj1] increment: [self.textField.text intValue]]; } </code></pre> <p>Notice how the only thing that has changed is the way the API for the class is satisfied. No code that uses the API has to change. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload