Pergunta

Please keep in mind I am new to objective c coding and do not understand many of the concepts.

I have 2 classes that must speak to one another. Class1 declares a string variable and Class2 reads it. I am not sure of the best way to go about this but after some research here is what I have.

The error is that when I access the variable in Class2 the variable is (null).

Class1.h

static NSString *var;
@interface Class1 : UIViewController {

    //other variables
}

@property(nonatomic, retain) NSString *var;
@end

Class1.m

@implementation Class1
@synthesize var;

-(void)buttonAction:(UIButton*)sender;
{
    var = @"some generated variable";
    //after setting the variable I immediately call Class2
}

Class2.h

@interface Class2 : UIViewController {
    NSString *var2;
}
@end

Class2.m

#import "Class1.h"
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    Class1 *obj = [[Class1 alloc] init];
    var2 = obj.var;
    NSLog(@"%@", var2);
    //this print statement is (null)
}

Im just not sure where I am going wrong. If my code is unclear in any way please let me know.

Foi útil?

Solução

The problem is that you are not setting the value of the property var in Class1 at the init method of this class. Yo do int only when button action method is executed. So when you create a new instance of Class1 in your initWithNibName:bundle: method in Class2 and access it (var2 = obj.var;), this instance's var property has not been set so it has a default nil value.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       Class1 *obj = [[Class1 alloc] init];
       var2 = obj.var; // <-- obj.var is not set, so it returns nil
       NSLog(@"%@", var2);
       //this print statement is (null)
    }
    return self;
}

In order to solve this, you could either initialize var in Class1at the init method or to have a reference to the same instance where you are running the buttonAction to set the var value given to Class2. For example, passing you Class1 instance as an input of your Class2 init method:

This is how you would create and initialized Class2 instance from buttonAction method in Class1.m:

-(void)buttonAction:(UIButton*)sender;
{
    self.var = @"some generated variable";
    //after setting the variable I immediately call Class2
    Class2 *obj2 = [Class2 alloc] initWithNibName:@"yourNibName"
                                           bundle:nil
                                        andClass1:self];
}

This would be your Class2 init method in Class2.m:

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil
            andClass1:(Class1 *)obj
{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        var2 = obj.var; // <-- obj.var will be set in buttonAction
        NSLog(@"%@", var2);
    }
    return self;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top