如何在iOS中将字符串值从一个.m文件传递到另一个.m文件
我在一个.m文件中有多个字符串值,我想将这些文件访问到另一个.m文件以使用该文件中的值。如果要将数据从ViewControlOne传递到ViewControllert,请尝试以下两种方法 在ViewControlerOne.h中执行这些操作如何在iOS中将字符串值从一个.m文件传递到另一个.m文件,ios,objective-c,Ios,Objective C,我在一个.m文件中有多个字符串值,我想将这些文件访问到另一个.m文件以使用该文件中的值。如果要将数据从ViewControlOne传递到ViewControllert,请尝试以下两种方法 在ViewControlerOne.h中执行这些操作 @property (nonatomic, strong) NSString *str1; 在ViewControllerTwo.h中执行这些操作 @property (nonatomic, strong) NSString *str2; 在View
@property (nonatomic, strong) NSString *str1;
在ViewControllerTwo.h中执行这些操作
@property (nonatomic, strong) NSString *str2;
在ViewControllerTwo.m中合成str2
@interface ViewControllerTwo ()
@end
@implementation ViewControllerTwo
@synthesize str2;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@",str2);
}
在ViewControlerOne.m中执行这些操作
- (void)viewDidLoad
{
[super viewDidLoad];
// Data or string you wants to pass in ViewControllerTwo..
self.str1 = @"hello world";
}
在按钮上单击事件执行此操作
-(IBAction)ButtonClicked
{
//Navigation on buttons click event from ViewControlerOne to ViewControlerTwo with transferring data or string..
ViewControllerTwo *objViewTwo=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerTwo"];
objViewTwo.str2=str1;
[self.navigationController pushViewController: objViewTwo animated:YES];
}
在ViewControllerTwo.m中执行这些操作
@interface ViewControllerTwo ()
@end
@implementation ViewControllerTwo
@synthesize str2;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@",str2);
}
通过以下两种方法,您可以实现这一目标,
1) 可以在.h文件中创建公共属性
2) 也可以使用委托或通知在控制器之间传递数据。您应该参考此答案。按钮点击事件可能重复。您能否告诉我什么是“obj”?obj.str2=str1;我更新了那一行。它是ViewControllerTwo的对象。当我点击按钮时出现异常——由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“情节提要()不包含标识符为“CustomerDetailViewController”@harshrajput显示代码的视图控制器。请参考这些链接以获得情节提要Id。”。。和