2011-02-14 159 views

回答

4

您想在每个控制器的property

@interface MyViewController : UIViewController{ 
    NSString *title; 
} 
@property (retain) NSString *title; 
@end; 


@implementation MyViewController 
@synthesize title; 
@end; 

这样使用它:

MyViewController *myVC = [[MyViewController alloc] initWithFrame:...]; 
myVC.title = @"hello world"; 

你应该熟悉Memory Management

+0

你是说每个MyViewController应该有一个NSString *标题? – aherlambang 2011-02-15 00:14:22

+0

这只是一个例子。你可以命名该成员`banana`或`penelope` – vikingosegundo 2011-02-15 00:19:28

+0

如果MyViewController2想要使用这个标题怎么办? – aherlambang 2011-02-15 03:15:29

1

分享您共同创建一个类对象。使用静态方法检索它,然后读取和写入其属性。

@interface Store : NSObject { 
    NSString* myString; 
} 

@property (nonatomic, retain) NSString* myString; 

+ (Store *) sharedStore; 

@end 

@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 
换句话说

,写:

Store* myStore = [Store sharedStore]; 
myStore.myString = @"myValue"; 

和读取(在另一视图中控制器):

Store* myStore = [Store sharedStore]; 
myTextField.text = myStore.myString; 
0

如果字符串保持相同,而且从不改变,你可以创建一个文件命名defines.h(不包括.m文件),并有这一行:

#define kMyString @"Some text" 

那么无论你需要的字符串,就导入定义文件,并使用常数。

#import "defines.h" 

比自定义类更简单。

编辑:

没有看到你需要从文本字段抓取。

在这种情况下,您可以将它存储为应用程序委托类的属性并从那里获取它。代表可以从应用程序的任何位置访问。

相关问题