开发者

Do I need to have an @property here?

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


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜