我需要在这里有一个@property吗?

发布于 2024-11-17 03:53:59 字数 487 浏览 3 评论 0原文

基本上,我希望能够通过我的类一直访问 UIApplication 委托的 window 属性,因此我想使用 iVar 引用它。

为此,我不想“拥有”它,只是引用它。

那么我应该在 .h 文件中放置一个变量引用吗?

#import <UIKit/UIKit.h>


@interface MessageView : UIView {
    UILabel *messageLabel;
    UIWindow *window;
}

@property (nonatomic, retain) UILabel *messageLabel;

@end

或者我也应该在那里设置属性?

我对此持怀疑态度,因为该属性将是非原子的,保留,但我不想保留它,除非我真的这样做,而且我只是太厚了! :p

拥有窗口对象的目的只是为了能够向其添加子视图,而不是当前视图控制器的视图。

谢谢

Basically I want to be able to access the UIApplication delegate's window property all the way through my class, so I want to reference it with an iVar.

To this end I don't want to "own" it, just reference it.

So therefore should I just put a variable reference in the .h file?

#import <UIKit/UIKit.h>


@interface MessageView : UIView {
    UILabel *messageLabel;
    UIWindow *window;
}

@property (nonatomic, retain) UILabel *messageLabel;

@end

Or should I set the property there too?

I'm sceptical because the property would be nonatomic, retain, but I don't want to retain it, unless I actually do and I'm just being thick! :p

The purpose of having the window object is just to be able to add subviews to it, rather than the current view controller's view.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七色彩虹 2024-11-24 03:53:59

为什么不使用

@property (nonatomic, assign) UIWindow *window

那你就不保留它了。

鉴于窗口应该在您的应用程序的生命周期中存在,因此没有真正需要保留它,因为它已经被您的应用程序委托保留。

在这种情况下,首先拥有一个属性无非就是语法糖,

someclass.window = self.window; // Using a property

window = [UIApplication sharedApplication].window; // Using an iVar

Why not use

@property (nonatomic, assign) UIWindow *window

Then you are not retaining it.

Given the window should exist for the lifetime of your app there is no real need to retain it as it's already being retained by your app delegate.

Having a property in the first place in this scenario is nothing more than syntactic sugar

someclass.window = self.window; // Using a property

is much more succinct than

window = [UIApplication sharedApplication].window; // Using an iVar
始终不够 2024-11-24 03:53:59

好吧,您实际上确实想保留UIWindow。新项目默认保留它,这没有任何问题。 我看到 MessageView 直接继承自 UIView 并且有一个 window 属性,一旦添加到窗口(或窗口的子视图)。另请参阅 willMoveToWindow:didMoveToWindow。现在,永远不要认为您不能仅仅因为不想保留某些内容而创建属性,因为这就是 assign 关键字的用途。

#import <UIKit/UIKit.h>


@interface MessageView : UIView {
    UILabel *messageLabel;
    UIWindow *window;
}

@property (nonatomic, retain) UILabel *messageLabel;
@property (nonatomic, assign) UIWindow *window;

@end

Well you actually do want to retain the UIWindow. New projects by default retain it and there is nothing wrong with that. I see that MessageView is inheriting directly from UIView and that has a window property that is set once it is added to a window(or a subview of a window). Also look at willMoveToWindow: and didMoveToWindow. Now never think that you can not create a property just because you do not want to retain something because that is what the assign keyword is for.

#import <UIKit/UIKit.h>


@interface MessageView : UIView {
    UILabel *messageLabel;
    UIWindow *window;
}

@property (nonatomic, retain) UILabel *messageLabel;
@property (nonatomic, assign) UIWindow *window;

@end
情徒 2024-11-24 03:53:59

事实上,不,你不知道。

每当您使用任何 UIWindow 制作关键窗口 方法(正如您可能在您的内部所做的那样) AppDelegate),例如

– makeKeyAndVisible
– makeKeyWindow

只需使用 UIApplication's keyWindow 属性。

[[UIApplication sharedApplication] keyWindow]

因此,您的 AppDelegate 或其他任何地方都不需要属性或保留,因为它将由您的应用程序类保留。

OBS:该属性通常放置在您的 AppDelegate 中,因为 XCode 中的应用程序模板使用界面生成器和 IBOutlet 来实例化 UIWindow。因此,如果您手动创建窗口,则不需要那里的属性。

Actually, no, you do not.

Whenever you use any of the UIWindow Make Key Window methods (as you probably is doing inside your AppDelegate), such as

– makeKeyAndVisible
– makeKeyWindow

The window becomes available from all your application just by using the UIApplication's keyWindow property.

[[UIApplication sharedApplication] keyWindow]

So, there is no need for a property or a retain in your AppDelegate or anywhere else, as it will be retained by your application class.

OBS: The property is commonly placed in your AppDelegate as the application template from XCode used the interface builder and an IBOutlet to instantiate the UIWindow. So, if your are creating your window by hand, there is no need for a property there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文