【问题标题】:Changing UI from AppDelegate从 AppDelegate 更改 UI
【发布时间】:2012-01-27 12:50:41
【问题描述】:

当应用程序完成启动时,我尝试从我的 AppDelegate 设置 UITextView。 其实我只是想打开一个文件并将其内容传递给 UITextView。

在我的 ViewController 中,我添加了以下方法:

ViewController.h:

@interface
{
    IBOutlet UITextView *textView;
}    
- (void)openFile:(NSString *)myString;

ViewController.m:

- (void)openFile:(NSString *)myString
{
    textView.text = myString;
}

在我的 AppDelegate 中:

AppDelegate.m:

#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application: [...] didFinishLaunchingWithOptions: [...]
{
    ViewController *test = [[ViewController alloc] init];
    [test openFile:@"this is a test"];
}

当我尝试从我的 AppDelegate 调用该方法时,它实际上被调用并按预期传递了字符串。 我通过NSLog(@"%@", myString);进行了测试。

但是textView 的值没有改变。 首先我认为可能还有其他问题,所以我在加载视图等后使用 UIButton 调用了该方法。但一切正常,textView 发生了变化。

然后我想,在我从 AppDelegate 调用我的方法后,视图可能会被加载。 我又添加了一些 NSLog,结果发现视图已完全加载,然后我的 AppDelegate 调用了该方法。

所以 AppDelegate 在视图完全加载并传递字符串后调用[test openFile:(NSString *)]。但是我的 textView 的值仍然没有改变。

对此有什么建议吗? 有没有人遇到过同样的问题?

【问题讨论】:

  • 你有没有仔细检查过笔尖上的插座是否连接?
  • 是的,我做到了。但是我应该在NIB中连接什么来满足这个需求?因为我想在应用程序中完成启动。但是没关系。我解决了这个问题。谢谢
  • 请不要给标题加标签,也不要在这样的帖子上签名

标签: ios methods uiviewcontroller uitextview uiapplicationdelegate


【解决方案1】:

您没有为 ViewController 加载任何视图。所以插座什么都没有。如果您从 NIB (xib) 文件加载视图和 ViewController,那么您不必创建另一个 ViewController 实例。这就是你在分配和初始化一个新的 ViewController 时所做的,创建一个连接到任何东西的新实例。

因为有一个 IBOutlet 我想有一个 xib 文件。尝试类似

- (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;
}

【讨论】:

    【解决方案2】:

    不是我所需要的。但你给了我正确的想法。 非常感谢!

    self.viewController = [[test3ViewController alloc] initWithNibName:@"YourXibName" bundle:nil]; 
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    [self.viewController openFile:@"Der Test"];
    

    【讨论】:

      猜你喜欢
      • 2012-03-10
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 2013-06-05
      • 1970-01-01
      相关资源
      最近更新 更多