UIViewController具有自定义视图和弱引用

时间:2022-10-02 16:10:10

I prefer to create custom views for all my view controllers. And I define it in code by using weak references for custom views like this:

我更喜欢为我的所有视图控制器创建自定义视图。我通过使用自定义视图的弱引用在代码中定义它,如下所示:

#import "MyViewController.h"
#import "MyCustomView.h"

@interface MyViewController ()
@property (nonatomic, weak) MyCustomView *customView;
@end

@implementation MyViewController
- (void) loadView
{
    MyCustomView *view = [MyCustomView new];
    self.view = view;
    self.customView = view;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // work with custom view
    self.customView.tableView.delegate = self;
    ....
}

@end

Is this the correct use of weak references? Will the application crash or leak, or will there be other problems?

这是弱引用的正确用法吗?应用程序是崩溃还是泄漏,还是会出现其他问题?

3 个解决方案

#1


2  

In this case weak is fine. You assign your CustomView to self.view which is defined in the UIViewController header as

在这种情况下,弱是好的。您将CustomView分配给self.view,它在UIViewController标头中定义为

@property(nonatomic,retain) UIView *view;

so the view property has a retaining reference.

所以view属性有一个保留引用。

There is a possibility that your view and customView could get out of sync - so I would be tempted to define customView as readonly and implement the getter as

您的视图和customView可能会失去同步 - 所以我很想将customView定义为只读并将getter实现为

- (CustomView *)customView
{
  return (id)self.view;
}

#2


1  

As you can see in the documentation of UIViewController the view controller's view property has a strong reference to the view. So the custom view object will be retained as long as you don't set the view property to something else. In short, your method works.

正如您在UIViewController的文档中所看到的,视图控制器的视图属性具有对视图的强引用。因此,只要您不将view属性设置为其他内容,就会保留自定义视图对象。简而言之,您的方法有效。

#3


0  

As you create the instance from within this controller programatically, you should use a strong reference to set the ownership clearly to this controller.

当您以编程方式从此控制器中创建实例时,应使用强引用将清除所有权设置为此控制器。

In the event that you create the view object in IB or soryboard respectively, then a weak reference to the related IBOutlet would do.

如果您分别在IB或soryboard中创建视图对象,那么对相关IBOutlet的弱引用就可以了。

#1


2  

In this case weak is fine. You assign your CustomView to self.view which is defined in the UIViewController header as

在这种情况下,弱是好的。您将CustomView分配给self.view,它在UIViewController标头中定义为

@property(nonatomic,retain) UIView *view;

so the view property has a retaining reference.

所以view属性有一个保留引用。

There is a possibility that your view and customView could get out of sync - so I would be tempted to define customView as readonly and implement the getter as

您的视图和customView可能会失去同步 - 所以我很想将customView定义为只读并将getter实现为

- (CustomView *)customView
{
  return (id)self.view;
}

#2


1  

As you can see in the documentation of UIViewController the view controller's view property has a strong reference to the view. So the custom view object will be retained as long as you don't set the view property to something else. In short, your method works.

正如您在UIViewController的文档中所看到的,视图控制器的视图属性具有对视图的强引用。因此,只要您不将view属性设置为其他内容,就会保留自定义视图对象。简而言之,您的方法有效。

#3


0  

As you create the instance from within this controller programatically, you should use a strong reference to set the ownership clearly to this controller.

当您以编程方式从此控制器中创建实例时,应使用强引用将清除所有权设置为此控制器。

In the event that you create the view object in IB or soryboard respectively, then a weak reference to the related IBOutlet would do.

如果您分别在IB或soryboard中创建视图对象,那么对相关IBOutlet的弱引用就可以了。