gpt4 book ai didi

objective-c - UITextView 不刷新

转载 作者:行者123 更新时间:2023-11-29 13:23:06 25 4
gpt4 key购买 nike

我正在尝试集成“打开方式...”功能。

在我的 AppDelegate.m 中有

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
...
_fileContent = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&error];
...
ViewController *vc = (ViewController*)self.window.rootViewController;
[vc refreshUI:nil];
}

我正在使用 ARC,所以我只在我的 ViewController.h 中调用以下内容,然后在 .m 中省略了 @synthesize

@property (nonatomic, retain) IBOutlet UITextView *textView;

在我的 ViewController.m 中有以下内容

-(void)refreshUI:(id)sender
{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

if ([appDelegate openedFromURL])
{
NSLog(@"[refreshUI] appDelegate openFromURL (Length: %d)", [appDelegate.fileContent length]);
NSLog(@"Contents before: %@", [_textView text]);
[_textView setText:appDelegate.fileContent];
NSLog(@"Contents after: %@", [_textView text]);
}
...
}

当我从另一个应用程序 (Dropbox) 打开我的文件时,第一个 NSLog 为我提供了正确的文件长度。第二个 NSLog 为我提供了 UITextView 的正确“旧”内容,第三个 NSLog 为我提供了正确的"new"内容(从我通过保管箱应用程序“打开...”的文件的内容开始)。所以数据确实进入了我的应用程序。

问题是 UITextView 没有得到更新。如果我按下 UIViewController 中的任何其他按钮(这会导致我切换到不同的 UIViewController),然后返回到“ViewController”,UITextView 会更新为正确的数据。即使我添加一个 UIButton 并将其操作设置为仅调用 [self refreshUI] UITextView 也会更新。

它只是不会从 AppDelegate 调用的方法自行刷新。

我做错了什么?我什至尝试使用 setNeedsDisplay 手动重绘 UITextView。但这没有任何效果。

谢谢

最佳答案

现在可以从您的评论中推断出来。似乎您的 Storyboard segues 正在实例化您的“ViewController”类的新实例,而不是将您返回到原始实例。然而,原来的仍然是应用委托(delegate)的 rootViewController。这会导致您向不是当前显示的 View Controller 的实例发送消息。本质上,您正在更改您看不到的标签。有几种方法可以解决这个问题。

  • 更改您的 segue,以便它们将您带回 rootViewController:

这可能是最简单的,并且可能会提高您的效率。在导致返回的方法中,使用带有 pop...dismiss 等词的方法。

  • 使用委托(delegate):

urlLaunchDelegate 添加到您的应用委托(delegate),并在适当的时候(viewDidLoadviewWillAppear:)将您的 View Controller 设置为urlLaunchDelegate

  • 使用NSNotificationCenter

有一个像 MyApplication_LaunchURLNotification 这样的通知,你的 View Controller 会观察到。您可以添加 NSURL 作为对象。

关于objective-c - UITextView 不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13863940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com