从另一个UIViewController重新加载UITextView(Reloading UITextView from another UIViewController)

编程入门 行业动态 更新时间:2024-10-28 09:25:53
从另一个UIViewController重新加载UITextView(Reloading UITextView from another UIViewController)

我有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.

更多推荐

本文发布于:2023-07-31 08:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   UIViewController   Reloading   UITextView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!