简体   繁体   English

从AppDelegate更改UI

[英]Changing UI from AppDelegate

I try to set a UITextView from my AppDelegate, when the application did finished launching. 当应用程序完成启动时,我尝试从AppDelegate设置UITextView。 Actually I just want to open a file and pass it's contents to a UITextView. 实际上,我只想打开一个文件并将其内容传递给UITextView。

In my ViewController I added the following method: 在我的ViewController中,我添加了以下方法:

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中:

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. 当我尝试从AppDelegate调用该方法时,它实际上会被调用并按预期方式传递字符串。 I tested via NSLog(@"%@", myString); 我通过NSLog(@"%@", myString);进行了测试NSLog(@"%@", myString); .

But the value of textView doesn't change. 但是textView的值没有改变。 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. 首先,我认为可能还有其他问题,因此在加载视图等之后,我使用UIButton调用了该方法。但这一切都很好,并且textView发生了变化。

Then I thought, the view might be loaded after I calling my method from the AppDelegate. 然后我想,从AppDelegate调用我的方法后,可能会加载视图。 I put in a few NSLogs more and it turned out, that the view is fully loaded, then my AppDelegate calls the method. 我添加了一些NSLogs,结果是视图已完全加载,然后我的AppDelegate调用了该方法。

So the AppDelegate calls [test openFile:(NSString *)] after the view is fully loaded and it's passing the String. 因此,在视图完全加载并传递String后,AppDelegate调用[test openFile:(NSString *)] But the value of my textView is still not changing. 但是我的textView的值仍然没有改变。

Any suggestions on this? 有什么建议吗? Did anybody of you run into the same problem? 你们中有人遇到过同样的问题吗?

You are not loading any view for ViewController. 您没有为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. 如果要从NIB(xib)文件加载视图和ViewController,则不必创建另一个ViewController实例。 That is what you are doing when alloc and init a new ViewController, create a new instance connecting to nothing. 这就是您在分配并初始化一个新的ViewController并创建一个不连接任何内容的新实例时所做的事情。

As there is a IBOutlet I suppose there is a xib file. 由于有一个IBOutlet,我想有一个xib文件。 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;
}

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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM