我需要在我的应用程序中保存一个字符串,但我不想创建一个plist只是为了保存1字符串有没有其他方法这样做?即时通讯使用故事板。我试过segue但它没有工作
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: @"modal"])
{
ViewController *vc = (ViewController *)[segue destinationViewController];
vc.subject = self.subject;
}
}
答案 0 :(得分:1)
“我需要在我的应用程序中保存字符串”是什么意思?您是否只想将字符串传递给目标ViewController?
如果是这种情况,您可以在目标ViewController中定义一个新属性(假设您知道它是哪一个)
@property (nonatomic, strong) NSString *myProperty;
然后
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: @"modal"])
{
[segue.destinationViewController setMyProperty:self.myProperty];
}
}
如果您希望可以从应用程序的任何位置访问您的字符串,您可以很容易地将其保存到用户默认值:
[[NSUserDefaults standardUserDefaults]setObject:myProperty forKey:@"myPropertyKeyName"];
你可以稍后再回来:
NSString *mySavedProperty= [[NSUserDefaults standardUserDefaults]stringForKey:@"myPropertyKeyName"];
答案 1 :(得分:0)
如果您可以在项目中的任何位置使用字符串,只需像这样创建一个外部变量。
在AppDelegate.h中
extern NSString *stringObject;
在AppDelegate.m中
NSString *stringObject;
你可以在项目的任何地方使用stringObject。