Reputation: 9351
I am trying to integrate the "Open with..." functionality.
In my AppDelegate.m
I have
-(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];
}
I am using ARC, so I only call the following in my ViewController.h
and later omit the @synthesize
in the .m
@property (nonatomic, retain) IBOutlet UITextView *textView;
In my ViewController.m
I have the following
-(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]);
}
...
}
When I go to open my file form another application (Dropbox), the first NSLog gives me the right length of my file. The second NSLog gives me the correct "old" contents of the UITextView and the third NSLog gives me the correct "new" contents (begin the contents of the file I "opened with..." through the dropbox app). So the data does make it to my app.
THe problem is the UITextView does not get updated. If I press any other button in the UIViewController (which causes me to switch to a different UIViewController) and then go back to "ViewController" the UITextView is updated with the correct data. Even if I add an UIButton to and set its action to only call [self refreshUI]
the UITextView updates.
It just doesn't refresh on its own from the method being called from AppDelegate.
What am I doing wrong? I even tried manually redrawing the UITextView with setNeedsDisplay
. But that had no effect.
Thanks
Upvotes: 1
Views: 636
Reputation: 27147
As can now be deduced from your comments. It seems that your storyboard segues are instantiating a new instance of your "ViewController" class rather than returning you to your original. The original, however, is still the app delegate's rootViewController
. This leads to you messaging an instance of your view controller that is not the one currently presented. Essentially, you are changing a label that you cannot see. There are a few ways to fix this.
rootViewController
:It's probably the easiest, and will likely improve your efficiency. In your methods that cause a return use methods with words like pop...
or dismiss
.
Add a urlLaunchDelegate
to your app delegate, and at appropriate times (viewDidLoad
or viewWillAppear:
) have your view controller set itself as the urlLaunchDelegate
.
NSNotificationCenter
Have a notification like MyApplication_LaunchURLNotification
that your view controller observes. You could add the NSURL
as the object.
Upvotes: 1
Reputation: 2205
@property (nonatomic, retain) IBOutlet UITextView
Needs to declare a name
@property (nonatomic, retain) IBOutlet UITextView *textView
You also need to link the textView in your IBOutlet as well.
Upvotes: 0