Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - Access Data Variable from one class in another class

Before anyone says its a similar or duplicate question of xxxx question, I have checked 27 questions till now and none of them is able to clear myquery and help me, most possibly coz no one posted the question in its most simplest form.

So I have a NS String in Class 1 in which I store some data.

##Class 1 
.h
@property ..... NSString *str; 

.m
-(void) someMethod { 

self.str = @"Blah blah"; 

} 

Now I want to access this "Blah Blah" data from the property "str" in Class 2 without passing it via any methods or such. I just need to access it somewhere (for example lets say viewDidLoad). What would be the most simplest way ?

PS:I was thinking to create a *tempString variable in appdelagate.m and access it in w/e class I want by creating a AppleDelegate *apdg object and accessing it. Any other ideas ?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

In SecondViewController.h

@property (nonatomic, copy) NSString *textblah;

In FirstViewController.m

#import "SecondViewController.h"

// where you want to store value

SecondCiewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.textblah = stringToStore;

// and 
[self.navigationController push:secondViewController animated:YES];

// You can log the set value with to check whether it was successful or not.
NSLog(@"textblah: %@", textblah);

For complete understanding of the code and process you can watch the video tutorial like this and this

You will find them informative and easy to understand specially beginners.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...