Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

iphone - Passing NSString value between classes

I have been looking long for a solution how to pass the value from one class to another. Basically I create a NSString via @property in a Settings class, which is displayed modally. I then set the NSString to a certain value depending on the settings chosen, and I want to have the value shown also in a class where the settings are supposed to make change. I also declare a string via @property in the second class, and I use the code

myController *controller = [[myController alloc] init];
secondClassString = controller.firstClassString

If I NSLog the string, it shows (null) in the second class... Any ideas how to make it pass the value? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

suppose you've two viewcontrollers say A & B

Your A.h

{
  NSString *strData;
  int cId;
}

@property (nonatomic, retain) NSString *strData;
@property (readwrite) int cId;

Now In your A.m

@synthesize strData,cId;

Your B.h

@class A

{
   A *aObj;
}

Now In your B.m

#import "A.h"

- (void) viewDidLoad
{
  aObj=[A alloc] init]; //alloc object of A
  [aObj setCId:10]; //set value for cId
  [aObj setStrData:@"Hello from B"]; //set value for strData
  //do what ever
  [aObj release]; //don't forget
}

Hope this helps you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...