问题
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
回答1:
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!
回答2:
first view controller:
nsstring *firstString=@"your string"; to pass this string into second view controllert create nsstring into 2nd view controller in 2nd view controller
nsstring *sencondstring;
@property(nonatomic,retain)nsstring *sencondstring; @synthesize sencondstring;
now back to first view controller create object of second view controller
firstviewcontroller *firstviewObj=[[firstviewcontroller alloc]init;
firstviewObj.sencondstring=firststring; OR firstviewObj.sencondstring=[nsstring stringwithformat:@"%@",firststring];
回答3:
maybe this would help you
write this in Firstclass.m file
SecondView *ObjSecondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
objSecondView.secondClassString=Firstclassstring;
[self.navigationController pushViewController:ObjSecondView animated:YES];
[ObjSecondView release];
in SecondView.h
@property(nonatomic,retain)NSString *secondClassString;
//also synthesize this
then print on secondview's viewdidload method
-(void)viewDidLoad
{
NSLog(@"your string = %@",secondClassString);
}
回答4:
Let us use the names of the two viewControllers as FirstViewController and SecondViewController.
Now suppose you push SecondViewController from FirstViewController on a button click then you need to write this code under your button click event:
// In the FirstViewController
- (void)buttonClicked:(id)sender{
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle: nil];
second.secString = firstString;
[self.navigationController pushViewController:second animated:YES];
[second release];
}
Here you need to declare in SecondViewController.h file:
NSString *secString;
and then create a @property
@property (non atomic,strong) NSString *secString;
In SecondViewController.m you need to @synthesize the secString:
@synthesize secString;
Hence this way you also create a getter and setter for secString by creating property and synthesizing it.
Now you can easily access the secString and you can use it anywhere in SecondViewController.
Just to check Try and check if value of firstString is passed to secString, write following code on viewWillAppear: of SecondViewController
NSLog(@"secString: %@",secString);
Let me know if you need more help.
Hope this helps.
来源:https://stackoverflow.com/questions/9954155/passing-nsstring-value-between-classes