Reputation: 7560
I try to set a UITextView from my AppDelegate, when the application did finished launching. Actually I just want to open a file and pass it's contents to a UITextView.
In my ViewController I added the following method:
ViewController.h:
@interface
{
IBOutlet UITextView *textView;
}
- (void)openFile:(NSString *)myString;
ViewController.m:
- (void)openFile:(NSString *)myString
{
textView.text = myString;
}
In my AppDelegate the following:
AppDelegate.m:
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application: [...] didFinishLaunchingWithOptions: [...]
{
ViewController *test = [[ViewController alloc] init];
[test openFile:@"this is a test"];
}
When I attempt call the method from my AppDelegate, it actually gets called and passes the string as expected.
I tested via NSLog(@"%@", myString);
.
But the value of textView
doesn't change.
First I thought there could be an other problem, so I called the method with a UIButton after loading the view etc. But it was everything okay and the textView changed.
Then I thought, the view might be loaded after I calling my method from the AppDelegate. I put in a few NSLogs more and it turned out, that the view is fully loaded, then my AppDelegate calls the method.
So the AppDelegate calls [test openFile:(NSString *)]
after the view is fully loaded and it's passing the String. But the value of my textView is still not changing.
Any suggestions on this? Did anybody of you run into the same problem?
Upvotes: 0
Views: 1913
Reputation: 7560
Wasn't exactly what I needed. But you gave me the right idea. Thanks a lot!
self.viewController = [[test3ViewController alloc] initWithNibName:@"YourXibName" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[self.viewController openFile:@"Der Test"];
Upvotes: 0
Reputation: 3359
You are not loading any view for ViewController. So the outlet is connected to nothing. if you are loading views and ViewController from NIB (xib) file, then you don't have to create another instance of ViewController. That is what you are doing when alloc and init a new ViewController, create a new instance connecting to nothing.
As there is a IBOutlet I suppose there is a xib file. Try something like
- (BOOL)application: [...] didFinishLaunchingWithOptions: [...]
{
ViewController *test = [[ViewController alloc] initWithNibName:@"YourXibName"
boundle:nil ];
[test openFile:@"this is a test"];
self.window.rootViewController = test.view ;
[self.window makeKeyAndVisible];
return YES;
}
Upvotes: 1