gpt4 book ai didi

iphone - View Controller 的 UI 元素的所有者是什么?带ARC的iOS5

转载 作者:行者123 更新时间:2023-11-29 13:36:17 25 4
gpt4 key购买 nike

我是 iOS 5 的新手。从 Apple 的文档中我知道 ARC 是什么以及“对象的所有者应该使用 strong 表示法。”看完《Hello World》后,我注意到一件奇怪的事。(我的意思是那让我很困惑)

HelloWorldAppDelegate:

@interface HelloWorldAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

HelloWorldViewController:

#import <UIKit/UIKit.h>

@interface HelloWorldViewController : UIViewController <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)changeGreeting:(id)sender;

@property (copy, nonatomic) NSString *userName;
@end

这里:

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;

UI 元素具有 weak 符号,并且没有文件具有对它们的 strong 引用。所以我很困惑什么/谁持有它们?

最佳答案

在我看来,您可以将 NIB/XIB 视为这些对象的所有者。你的类(class)都不拥有它们。参见 "Managing the Lifetimes of Objects from Nib Files"在资源编程指南中:

From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File's Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create should will therefore typically be weak by default, because:

  • Outlets that you create to, for example, subviews of a view controller's view or a window controller's window, are arbitrary references between objects that do not imply ownership.
  • The strong outlets are frequently specified by framework classes (for example, UIViewController's view outlet, or NSWindowController's window outlet).

关于iphone - View Controller 的 UI 元素的所有者是什么?带ARC的iOS5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10499080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com