iDev
iDev

Reputation: 478

singleton, public, or global variable use

I have searched and tried every example regarding singleton, public, and global variables in stack overflow on this subject. I'm making a mistake some where. I have a settings variable called strIP that is part of a textField and is declared in my secondViewController.h. I want this variable to used in a class called myWSupdate.m. It's just one variable I wanna pass it to a connection string. this compiles correctly but the app crashes on run. What am I doing incorrectly?

error from complier:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[SecondViewController sharedIP]: unrecognized selector sent to class 0x6403c'

secondViewController.h

@interface SecondViewController : UIViewController
{
UITextField *ipAdd;

NSString *strIP;

}
@property (nonatomic, retain) IBOutlet UITextField *ipAdd;
@property (retain) NSString  *strIP;

+(SecondViewController*)sharedIP;

then I call it in myWSupdate.m:

#import "SecondViewController.h"

/* Implementation of the service */

@implementation myWSupdate

- (id) init
{
    if(self = [super init])
    {

        SecondViewController* IP = [[SecondViewController sharedIP]init];
NSLog(@"the test has %@", IP.strIP);
    }
}


@end

Upvotes: 0

Views: 129

Answers (1)

Phillip Mills
Phillip Mills

Reputation: 31016

Since strIP belongs to a SecondViewController, you need to reference it as part of that object.

How to do that depends on the relationship between SecondViewController and myWSupdate. (For example, if the controller creates a myWSupdate object, you could pass the variable as part of the init.)

The fact that it's marked public doesn't change the fact that it's an instance variable and therefore needs to be used in connection with an instance of its class.

Upvotes: 2

Related Questions