小编use*_*876的帖子

自定义UIView子视图的最佳实践强与弱

所以我正在编写一个继承自UIView的自定义类.我有一堆子视图,我添加.

所以遇到2个问题.如果我将superview和子视图引用强大,那么视图就会泄漏.如果我让他们变弱,他们根本就不会出现.我究竟做错了什么?

CustomUIView

@interface CustomUIView : UIView
@property(nonatomic, strong) AnotherCustomUIView *mySubView;
@end

@implementation CustomUIView {

- (void)initCommon {
    self.mySubView = [self createSubView]
}

- (AnotherCustomUIView *) createSubView {
    return [[AnotherCustomUIView alloc] init:self];
}

@end
Run Code Online (Sandbox Code Playgroud)

AnotherCustomUIView

@interface AnotherCustomUIView : UIScrollView
@property (nonatomic, strong) CustomUIView *ownerView;
@end


@implementation AnotherCustomUIView

- (id)init:(CustomUIView *) ownerView {
    self = [super init];
    if (self) {
        self.ownerView = ownerView;
        self.delegate = self;
    }
    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

memory-management objective-c ios

9
推荐指数
1
解决办法
5867
查看次数

NSInvocation和内存问题

所以我来自Java世界,我们对内存管理问题一无所知.在大多数情况下,ARC已经救了我的屁股,但这里有些让我难过的东西.基本上我使用NSInvocations来做一些事情,在进行下面的代码修改之前,我遇到了一些讨厌的内存问题.由于我做了这些修改,内存崩溃已经消失,但我通常非常害怕我不理解的代码.我这样做了吗?

之前:各种内存问题:

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[target class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:target];
[invocation setArgument:&data atIndex:2];
[invocation setArgument:&arg atIndex:3];
[invocation invoke];

NSString *returnValue;
[invocation getReturnValue:&returnValue];
Run Code Online (Sandbox Code Playgroud)

之后:没有内存问题,但我不确定我是否正确:

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[target class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:target];
[invocation setArgument:&data atIndex:2];
[invocation setArgument:&arg atIndex:3];
[invocation invoke];

CFTypeRef result;
[invocation getReturnValue:&result];

if (result)
    CFRetain(result);

NSString *returnValue = (__bridge_transfer NSString *)result;
Run Code Online (Sandbox Code Playgroud)

编辑:

我只想根据下面的答案添加,我使用了objc_msgSend,因此:

NSString * returnValue = objc_msgSend(target, selector, data, arg);
Run Code Online (Sandbox Code Playgroud)

它解决了所有内存问题,而且看起来更简单.如果您发现此问题,请发表评论.

cocoa-touch memory-management objective-c nsinvocation ios

8
推荐指数
1
解决办法
1900
查看次数

Touch Up Inside的全局事件处理程序

我有一个触摸操作,它是调整大小的场景的触发器,其中操作在触摸移动时开始,并且需要以touchesEnded结束.问题是,touchesEnd可能位于不同的UI视图上,因为用户正在拖动手指.如何注册*global"touchesEnded listener?

cocoa-touch ios

1
推荐指数
1
解决办法
264
查看次数