gpt4 book ai didi

ios - 在 View Controller 的自定义属性中设置 Bool

转载 作者:行者123 更新时间:2023-12-01 18:19:16 26 4
gpt4 key购买 nike

在 vc 中分配自定义类。然后我设置一个 boolean 值(设置或 . 表示法)。
该值永远不会到达自定义类 - 总是报告 NO。

谷歌搜索并尝试了许多不同的变化 - 没有工作。下面的代码还有什么问题?

自定义 View .h

    @interface CustomView : UIScrollView <UIScrollViewDelegate> {
BOOL myLocalProperty;
}

@property (assign) BOOL myProperty;

自定义 View .m
     @implementation CustomView

@synthesize myProperty =_myProperty;

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

myLocalProperty = _myProperty;

if (myLocalProperty==YES) {
NSLog(@"YES");
} else if (myLocalProperty==NO) {
NSLog(@"NO");
}

return self;
}

View Controller .m
(在某些被定义为被调用的方法中)
CustomView *myView = [[CustomView alloc] initWithFrame:CGRectZero];

myView.myProperty=YES;

YES 的值永远不会到达属性。这里有什么明显的错误吗?

最佳答案

YES 的值确实到达了那里,但是在您打印了默认值 NO 之后会发生这种情况.
_myProperty 的当前值在初始化程序中打印;在您分配属性时 YES ,初始化完成!

您可以通过添加一个显示属性当前值的方法来检查该值是否到达那里:

- (id)showMyProperty {
myLocalProperty = _myProperty;
if (myLocalProperty==YES) {
NSLog(@"YES");
} else if (myLocalProperty==NO) {
NSLog(@"NO");
}
}

现在更改创建 CustomView 的代码如下:
CustomView *myView = [[CustomView alloc] initWithFrame:CGRectZero];
myView.myProperty=YES;
[myView showMyProperty]; // This should produce a "YES" in NSLog

关于ios - 在 View Controller 的自定义属性中设置 Bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18624792/

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