Iphone 在xcode中保存/共享字符串
我需要在我的应用程序中保存一个字符串,但我不想只为保存一个字符串而创建plist。还有其他方法吗?我在用故事板。我试过segue但没用Iphone 在xcode中保存/共享字符串,iphone,ios,xcode,ipad,storyboard,Iphone,Ios,Xcode,Ipad,Storyboard,我需要在我的应用程序中保存一个字符串,但我不想只为保存一个字符串而创建plist。还有其他方法吗?我在用故事板。我试过segue但没用 -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString: @"modal"]) { ViewController *vc = (ViewController *)[se
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: @"modal"])
{
ViewController *vc = (ViewController *)[segue destinationViewController];
vc.subject = self.subject;
}
}
“我需要在应用程序中保存字符串”是什么意思?是否只想将字符串传递到目标ViewController
如果是这种情况,您可以在目标ViewController中定义一个新属性(假设您知道它是哪一个)
然后
-(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"];
如果可以在项目中的任何位置使用字符串,只需像这样创建一个外部变量 在AppDelegate.h中
extern NSString *stringObject;
在AppDelegate.m中
NSString *stringObject;
您可以在项目中的任何位置使用stringObject。具体是哪个字符串,需要在哪里使用该字符串?我看到您正在
ViewController
上设置主题(我假设它是UIViewController的子类)。如果您只希望它在控制器之间保持而不传递它,那么可以将它存储在NSUserDefaults
中,假设它很小(大约几千字节)事实上,它只是一个单词字符串哈哈哈是的,然后在NSUserDefaults
中抚摸它,就像我和答案建议的那样会工作我尝试了但没有工作我得到了这个错误架构i386未定义的符号:“\u主题”,引用自:我需要它在应用程序中是通用的,以便任何视图控制器都可以使用该字符串,然后我建议的NSUserDefaults解决方案将正常工作。
NSString *stringObject;