I have a NSString that is taken from a UITextField in a ViewController. Every of my other ViewController will use this NSString as well. How can I pass this NSString to others ViewControllers?
3 Answers
You want to have a property in each of your controllers
@interface MyViewController : UIViewController{
NSString *title;
}
@property (retain) NSString *title;
@end;
@implementation MyViewController
@synthesize title;
@end;
Use it like:
MyViewController *myVC = [[MyViewController alloc] initWithFrame:...];
myVC.title = @"hello world";
You should be familiar with Memory Management

- 52,040
- 14
- 137
- 178
-
you're saying that each of MyViewController should have a NSString * title? – aherlambang Feb 15 '11 at 00:14
-
that is just an example. you can name that member `banana` or `penelope` too – vikingosegundo Feb 15 '11 at 00:19
-
What if MyViewController2 wants to use this title? – aherlambang Feb 15 '11 at 03:15
-
it is absolutely ok to have the title in objects of different classes. seems as if u haven't understood object-orientated programming. check this: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/OOP_ObjC/Introduction/Introduction.html – vikingosegundo Feb 15 '11 at 09:12
Create a class for sharing your common objects. Retrieve it using a static method, then read and write to its properties.
@interface Store : NSObject {
NSString* myString;
}
@property (nonatomic, retain) NSString* myString;
+ (Store *) sharedStore;
@end
and
@implementation Store
@synthesize myString;
static Store *sharedStore = nil;
// Store* myStore = [Store sharedStore];
+ (Store *) sharedStore {
@synchronized(self){
if (sharedStore == nil){
sharedStore = [[self alloc] init];
}
}
return sharedStore;
}
// your init method if you need one
@end
in other words, write:
Store* myStore = [Store sharedStore];
myStore.myString = @"myValue";
and read (in another view controller):
Store* myStore = [Store sharedStore];
myTextField.text = myStore.myString;

- 8,215
- 5
- 33
- 48
-
what if you write myStore in viewcontroller1 and you want to read in the viewcontroller2 that same value written in viewcontroller1? I guess the solution above won't work – aherlambang Feb 15 '11 at 06:35
-
EquinoX: the above will work. Read up on singleton classes. This approach is much cleaner than having the same 'title' field in every view controller. – ThomasRS Feb 15 '11 at 20:25
If the string remains the same, and never changes, you could make a file named defines.h (without the .m file) and have this line:
#define kMyString @"Some text"
Then wherever you need the string, just import the defines file and use the constant.
#import "defines.h"
Much simpler than making custom classes.
EDIT:
Didn't see you needed to grab from the text field.
In that case, you could have it stored as property of your app delegate class and get it from there. The delegate can be accessed from anywhere in your app.

- 8,710
- 10
- 47
- 72
-
the string changes only the first time when a user enters it from the UITextField – aherlambang Feb 15 '11 at 00:19