我需要在这里有一个@property吗?
基本上,我希望能够通过我的类一直访问 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用
那你就不保留它了。
鉴于窗口应该在您的应用程序的生命周期中存在,因此没有真正需要保留它,因为它已经被您的应用程序委托保留。
在这种情况下,首先拥有一个属性无非就是语法糖,
比
Why not use
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
is much more succinct than
好吧,您实际上确实想保留我看到UIWindow
。新项目默认保留它,这没有任何问题。MessageView
直接继承自UIView
并且有一个 window 属性,一旦添加到窗口(或窗口的子视图)。另请参阅willMoveToWindow:
和didMoveToWindow
。现在,永远不要认为您不能仅仅因为不想保留某些内容而创建属性,因为这就是assign
关键字的用途。Well you actually do want to retain theI see thatUIWindow
. New projects by default retain it and there is nothing wrong with that.MessageView
is inheriting directly fromUIView
and that has a window property that is set once it is added to a window(or a subview of a window). Also look atwillMoveToWindow:
anddidMoveToWindow
. Now never think that you can not create a property just because you do not want to retain something because that is what theassign
keyword is for.事实上,不,你不知道。
每当您使用任何
UIWindow
制作关键窗口 方法(正如您可能在您的内部所做的那样)AppDelegate
),例如只需使用
UIApplication's
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 yourAppDelegate
), such asThe window becomes available from all your application just by using the
UIApplication's
keyWindow property.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 theUIWindow
. So, if your are creating your window by hand, there is no need for a property there.