如何从另一个对象访问我的Application Delegate的窗口访问器方法? [英] How do I access my Application Delegate's window accessor method from another object?

查看:131
本文介绍了如何从另一个对象访问我的Application Delegate的窗口访问器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如前所述 - 我是第一个订单的Objective-C新手,但是已经阅读了4本关于这个主题的实用书籍和一堆电子书和文档,我仍然无法找到我正在寻找的东西。

As mentioned before - I'm an Objective-C newbie of the first order but having read 4 physical books on the subject and a bucket-load of ebooks and documentation I still can't find what I'm looking for.

我有一个顶层内容视图控制器,想要从应用程序委托的window属性的物理维度配置其视图属性。这是几个人已经问过的问题。 ( [UIScreen mainScreen] 不会因为此论坛之前已经多次播出的原因而删除它)。因此,逻辑方法是内容视图控制器读取应用程序委托窗口的框架。现在,我发现接近这个的唯一答案是使用 [[[UIApplication sharedApplication] window] frame] - 但是,这仅适用于窗口属性已经成为keyAndVisible。内容视图控制器需要在获取makeKeyAndVisible之前读取应用委托的窗口属性。代码按此顺序排列....

I have a top-level content view controller that wants to configure its view property from the physical dimensions of the window property of the application delegate. This is something that several people have already asked questions on. ([UIScreen mainScreen] doesn't cut it for reasons already aired many times before on this forum). Therefore, the logical approach would be for the content view controller to read the frame of the application delegate's window. Now, the only answer I've found that comes close to this is to use [[[UIApplication sharedApplication] window] frame] - however, this only works once the window property has been made keyAndVisible. The content view controller needs to read the app delegate's window property before it gets to makeKeyAndVisible. The code goes in this order....

App代表:

- (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {
// This next line is a test window frame for R&D purposes....
    [self setWindow: [[UIWindow alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 320.0f)]];

    if ([self window]) {
        contentViewController = [[ContentViewControl alloc] initWithNibName: nil bundle: nil]; // Content view controller instantiated here

        if (contentViewController) {
            [[self window] addSubview: [contentViewController view]];
            [[self window] layoutSubviews];
            [[self window] makeKeyAndVisible];  // Window made key and visible here
            return YES;
        }
    }

    return NO;
}

在我的内容视图中,控制器的initWithNibName:nil包:nil方法我有以下内容测试代码...

In my content view controller's initWithNibName: nil bundle: nil method I have the following test code...

- (id) initWithNibName: (NSString *) nibNameOrNil bundle: (NSBundle *) nibBundleOrNil {
    self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];

    if (self) {
        NSLog(@"%@", NSStringFromCGRect([[[UIApplication sharedApplication] keyWindow] frame]));
        // This does not work.
    }

    return self;
}

这不起作用,因为App Delegate的窗口还没有关键并且可见。
所以,我的问题是这样的;我的App Delegate Class'实例的名称是什么?我知道App Delegate的 Class 名称默认为 myApplicationNameAppDelegate 但我在实例名称之后。我希望用以下内容替换对 [[UIApplication sharedApplication] keyWindow] 的调用;

This does not work due to the fact that App Delegate's window is not yet key and visible. So, my question is thus; What is the name of my App Delegate Class' instance? I know the App Delegate's Class name defaults to myApplicationNameAppDelegate but I'm after the instance name. I want to replace the call to [[UIApplication sharedApplication] keyWindow] by something like;

[myAppDelegatesInstanceName window].

稍微扩展一下,如何在其他目标对象中访问不是范围后代的方法对象进行查询?

Expanding this a little, how does one access methods in other target objects that are not scope descendants of the object doing the querying?

就像我说的那样,我对所有这些都是一个完全的菜鸟,这可能是另一个愚蠢的noobie问题,但它是一个没有人出现的问题已经以一种简单的方式回答了。

Like I said, I'm a total noob to all of this and this is probably another dumb noobie question but it's one that nobody yet appears to have answered in a simple way.

(程序上 - 我的家庭地盘 - 有很多方法可以将窗口内容放到其他范围内,范围从制作整个程序套件全局可访问的窗口,通过各种函数层次结构将其作为特定参数传递 - 但这个目标似乎与已建立的程序实践不同。

(Procedurally - my home turf - there are a plethora of ways to get the window stuff down into other levels of scope ranging from making the window globally accessible to the whole program suite to passing it down as a specific parameter through the various function hierarchies - but this Objective stuff seems to depart from established, procedural practice).

如果有人可以提供帮助,我会非常感激。这个东西绝对不直观!
VV

If anyone can help, I'd be really grateful. This stuff is definitely not intuitive! V.V.

推荐答案

您可以通过单身 UIApplication 实例:

[[[UIApplication sharedApplication] delegate] window];

这是一个特例,因为你可以访问一个对象( UIApplication instance)您知道的委托是您要访问的对象。一般情况:

This is a special case, though, because you can access an object (the UIApplication instance) whose delegate you know is the object you want to access. The general case:


稍微扩展一下,如何在其他目标对象中访问不属于该对象的范围后代的方法查询?

Expanding this a little, how does one access methods in other target objects that are not scope descendants of the object doing the querying?

您需要将目标对象的引用传递给要从中访问它的对象。这有时是必要的,但它也意味着您在这两个对象之间引入紧密耦合,如果可以的话,通常要避免这种耦合。因此,请考虑每个案例,看看是否还有其他可能的设计。

You need to pass a reference to the target object to the object from which you want to access it. This is sometimes necessary but it also means that you introduce a tight coupling between these two objects, which you generally want to avoid if you can. So think about each case and see if there are other possible designs.

这篇关于如何从另一个对象访问我的Application Delegate的窗口访问器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆