NSMutableArray 保存所有视图的所有值
NSMutableArray save all values of all views
我有 2 个 viewController,其中一个 TableView 在一行中包含一个 textView。
ViewControllerOne.h / ViewControllerSecond.h(相同代码)
UIViewController<UITableViewDataSource, UITableViewDelegate, UITextViewDelegate>
@property (nonatomic, retain) NSMutableArray *arrayFields;
ViewControllerOne.m
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *searchStr = [textView.text stringByReplacingCharactersInRange:range withString:text];
self.arrayFields[0] = searchStr;
return YES;
}
ViewControllerSecond.m
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *searchStr = [textView.text stringByReplacingCharactersInRange:range withString:text];
self.arrayFields[1] = searchStr;
return YES;
}
当应用程序 运行 时,ViewControllerOne 被打开,我在我的 textView 中写了一个文本,现在转到 ViewControllerSecond 我按下导航栏中存在的按钮:
-(void)next{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
ViewControllerSecond *lvc = [storyboard instantiateViewControllerWithIdentifier:@"profileToHome4"];
[self setAnimation];
lvc.arrayFields = self.arrayFields;
}
使用这段代码,我可以将我的数组传递到下一个视图。当下一个视图出现时,我可以在我的另一个 textView 中写一个文本。伟大的!让我们单击导航栏中存在的后退按钮返回到我的 ViewControllerOne:
- (IBAction)back:(id)sender{
[self.navigationController popViewControllerAnimated:NO];
}
在我的 ViewControllerOne 中,我的导航栏中有一个保存按钮,当用户按下此按钮时,控制台会向我显示数组中的内容...但是很奇怪...当我这样做时,我可以看到 2 个索引有两条短信!
在我看来,当我弹出我的 ViewControllerSecond 时,arrayFields 将被释放并且索引 1 从未存在过,当我在我的 ViewControllerOne 中按下保存按钮时,正确的是:
"Text 1"
不是:
"Text 1" "Text 2"
但为什么会这样? (因为我没有 return 数组到我的第一个视图)。
第二个视图控制器的 arrayFields 是对第一个视图控制器的强引用。它通过引用传递,而不是作为副本传递。在您的 ViewControllerOne 的 next
方法中,将 lvc.arrayFields = self.arrayFields;
更改为 lvc.arrayFields = [self.arrayFields copy];
我有 2 个 viewController,其中一个 TableView 在一行中包含一个 textView。
ViewControllerOne.h / ViewControllerSecond.h(相同代码)
UIViewController<UITableViewDataSource, UITableViewDelegate, UITextViewDelegate>
@property (nonatomic, retain) NSMutableArray *arrayFields;
ViewControllerOne.m
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *searchStr = [textView.text stringByReplacingCharactersInRange:range withString:text];
self.arrayFields[0] = searchStr;
return YES;
}
ViewControllerSecond.m
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *searchStr = [textView.text stringByReplacingCharactersInRange:range withString:text];
self.arrayFields[1] = searchStr;
return YES;
}
当应用程序 运行 时,ViewControllerOne 被打开,我在我的 textView 中写了一个文本,现在转到 ViewControllerSecond 我按下导航栏中存在的按钮:
-(void)next{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
ViewControllerSecond *lvc = [storyboard instantiateViewControllerWithIdentifier:@"profileToHome4"];
[self setAnimation];
lvc.arrayFields = self.arrayFields;
}
使用这段代码,我可以将我的数组传递到下一个视图。当下一个视图出现时,我可以在我的另一个 textView 中写一个文本。伟大的!让我们单击导航栏中存在的后退按钮返回到我的 ViewControllerOne:
- (IBAction)back:(id)sender{
[self.navigationController popViewControllerAnimated:NO];
}
在我的 ViewControllerOne 中,我的导航栏中有一个保存按钮,当用户按下此按钮时,控制台会向我显示数组中的内容...但是很奇怪...当我这样做时,我可以看到 2 个索引有两条短信!
在我看来,当我弹出我的 ViewControllerSecond 时,arrayFields 将被释放并且索引 1 从未存在过,当我在我的 ViewControllerOne 中按下保存按钮时,正确的是:
"Text 1"
不是:
"Text 1" "Text 2"
但为什么会这样? (因为我没有 return 数组到我的第一个视图)。
第二个视图控制器的 arrayFields 是对第一个视图控制器的强引用。它通过引用传递,而不是作为副本传递。在您的 ViewControllerOne 的 next
方法中,将 lvc.arrayFields = self.arrayFields;
更改为 lvc.arrayFields = [self.arrayFields copy];