我有2个视图控制器MainDetailViewController (MD)和MainEditViewController (ME)
MD中有一个textView,当视图加载时已有一些文本。 然后我像这样打电话给我
MainEditViewController *editVC = [[MainEditViewController alloc] init]; editVC.theTextView.text = self.theTextView.text; UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:editVC]; [self.navigationController presentViewController:navCon animated:YES completion:nil];我像上面一样将TextView的文本值传递给ME并使用presentViewController方法调用它。
在ME中,我编辑文本并单击“保存”按钮,该按钮应更新MD的textView中的文本值
MainDetailViewController *mainDetailVC = [[MainDetailViewController alloc] init]; mainDetailVC.theTextView.text = self.theTextView.text; [self dismissViewControllerAnimated:YES completion:nil];这并不反映医学博士的变化
我究竟做错了什么?
I have 2 view controllers MainDetailViewController (MD) and MainEditViewController (ME)
There is a textView in MD with some text already there when view loads. Then I call ME like this
MainEditViewController *editVC = [[MainEditViewController alloc] init]; editVC.theTextView.text = self.theTextView.text; UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:editVC]; [self.navigationController presentViewController:navCon animated:YES completion:nil];I am passing the TextView's text value to ME like above and calling it with presentViewController method.
In ME I edit the text and click on save button which should update the text value in MD's textView
MainDetailViewController *mainDetailVC = [[MainDetailViewController alloc] init]; mainDetailVC.theTextView.text = self.theTextView.text; [self dismissViewControllerAnimated:YES completion:nil];This is not reflecting change in MD
What am I doing wrong?
最满意答案
最简单的方法是使用块。 在导入之后但在@interface之前将新的block typedef添加到MainDetailViewController.h文件中:
typedef void (^ReturnBlock)(NSString *arg);在@interface和@end之间的MainEditViewController.h文件中添加公共属性:
@property(copy) ReturnBlock returnBlock;这是您关闭ME视图控制器时将调用的块。 接下来转到MainEditViewController.m文件并替换保存按钮中的代码:
MainDetailViewController *mainDetailVC = [[MainDetailViewController alloc] init]; mainDetailVC.theTextView.text = self.theTextView.text; [self dismissViewControllerAnimated:YES completion:nil];至:
if (self.returnBlock) self.returnBlock(self.theTextView.text); [self.navigationController popViewControllerAnimated:YES];您应该使用popViewControllerAnimated:方法而不是dismissViewControllerAnimated。
当你创建editVC时,剩下要做的最后一个更改是在你的MainDetailViewController.m文件中,之后添加:
editVC.theTextView.text = self.theTextView.text;这个:
editVC.returnBlock = ^(NSString *returnText) { NSLog(@"Returned text: %@", returnText); self.theTextView.text = returnText; };快乐的编码。
The easiest way to do this is by using block. Add new block typedef to your MainDetailViewController.h file just after imports but before @interface:
typedef void (^ReturnBlock)(NSString *arg);In MainEditViewController.h file between @interface and @end add public property:
@property(copy) ReturnBlock returnBlock;This is your block which will be called when you dismiss your ME view controller. Next move to MainEditViewController.m file and replace code in save button from:
MainDetailViewController *mainDetailVC = [[MainDetailViewController alloc] init]; mainDetailVC.theTextView.text = self.theTextView.text; [self dismissViewControllerAnimated:YES completion:nil];to:
if (self.returnBlock) self.returnBlock(self.theTextView.text); [self.navigationController popViewControllerAnimated:YES];You should use popViewControllerAnimated: method instead of dismissViewControllerAnimated.
The last change left to do is in your MainDetailViewController.m file when you create editVC, add after:
editVC.theTextView.text = self.theTextView.text;this:
editVC.returnBlock = ^(NSString *returnText) { NSLog(@"Returned text: %@", returnText); self.theTextView.text = returnText; };Happy coding.
更多推荐
发布评论