UITextView没有刷新

时间:2012-12-13 16:15:08

标签: objective-c ios uitextview appdelegate

我正在尝试集成“Open with ...”功能。

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中拨打以下内容,稍后省略@synthesize

中的.m
@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给了我正确的“新”内容(通过Dropbox应用程序开始我用“打开...”文件的内容)。所以数据确实可以用于我的应用程序。

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

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

我做错了什么?我甚至尝试用setNeedsDisplay手动重绘UITextView。但那没有效果。

由于

2 个答案:

答案 0 :(得分:1)

现在可以从您的评论中推断出来。看起来你的故事板segues正在实例化你的“ViewController”类的新实例,而不是让你回到原来的。但是,原始版本仍然是应用代表的rootViewController。这会导致您向视图控制器的实例发送消息,该实例不是当前显示的实例。基本上,您正在更改您看不到的标签。有几种方法可以解决这个问题。

  • 更改您的细分,以便他们带您回到rootViewController

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

  • 使用委托:

向您的应用代表添加urlLaunchDelegate,并在适当的时间(viewDidLoadviewWillAppear:)让您的视图控制器将自己设置为urlLaunchDelegate

  • 使用NSNotificationCenter

您的视图控制器会观察到MyApplication_LaunchURLNotification之类的通知。您可以添加NSURL作为对象。

答案 1 :(得分:0)

@property (nonatomic, retain) IBOutlet UITextView

需要声明名称

@property (nonatomic, retain) IBOutlet UITextView *textView

您还需要链接IBOutlet中的textView。