我有一個 UIView 子類,它被添加為 UIStackView 的排列子視圖。根據模型中的資料,我想隱藏或顯示排列的子視圖(稱為myView),但問題是當我去隱藏它時,即使我設定了myView.hidden = NO,它仍然顯示myView.hidden = YES.
例如,以下是我擁有的代碼。它從隱藏的視圖開始,根據是否myModel.someProperty設定,它將顯示 myView。或者這就是應該發生的事情。
我已經設定了一個斷點并逐步執行此代碼并self.myView.hidden == YES在第 4 行執行之前使用 LLDB 進行驗證。然后我在跨過第 4 行后立即檢查該值,它仍然是 YES。但是第 4 行明確將其設定為 NO,并且 myView 的實作中沒有任何內容覆寫甚至設定或檢查自身的隱藏屬性。所以設定隱藏在這個只是去標準的 UIViewsetHidden:方法。那么它怎么可能仍然是 YES?
1. //currently, self.myView.hidden is YES
2.
3. if (self->_myModel.someProperty) {
4. self.myView.hidden = NO;
5.
6. //for some reason, self.myView.hidden is still YES
7.
8. while (self.myView.isHidden) {
9. NSLog(@"myView is hidden, but it should not be");
10. self.myView.hidden = NO;
11. }
12. NSLog(@"myView is no longer hidden");
13. }
我在第 8 行添加了一個回圈,這將導致視圖再次隱藏。這一次它起作用了。所以如果我設定myView.hidden = NO兩次,那么它實際上會被設定為 NO。但是如果我只設定一次,那么它就停留在 YES 上。我不明白發生了什么。
有誰知道這里可能有什么問題或如何進一步解決這個問題?我已經使用 LLDB 的po命令來查看myView.isHidden每組屬性之前和之后的值。所以在第 4 行之前,它被設定為 YES,這是正確的。然后,在第 4 行之后,我檢查了它,它仍然設定為 YES,即使它在前一行中明確設定為 NO。然后,我檢查了一下,它進入了第 8 行的回圈(即使它不應該像它應該那樣被隱藏)。然后我在第 10 行之前再次檢查,myView.hidden仍然是 YES,我在第 10 行之后檢查,最終正確設定為 NO。
但我只是不確定發生了什么。這是非常違反直覺的,因為我明確將其設定為 NO,但直到我將其設定為 NO 兩次后才開始設定。
是否有解決此問題或找出問題所在的好方法,或者是否有人對可能出現的問題有任何建議?
更新
我已經更新了代碼以添加一些額外的日志陳述句。我p self.myView.hidden在 LLDB 中檢查該屬性時也使用過。
1. // at this point, self.myView.hidden = YES
2.
3. if (self->_myModel.someProperty) {
4. NSLog(@"Before setting hidden=NO: %@", self->_myView);
5. self.myView.hidden = NO;
6. NSLog(@"After setting hidden=NO: %@", self->_myView);
7.
8. while ([self.myView isHidden]) {
9. NSLog(@"SHOULD NOT BE HERE - Before setting hidden=NO again: %@", self->_myView);
10. self.myView.hidden = NO;
11. NSLog(@"SHOULD NOT BE HERE - After setting hidden=NO again: %@", self->_myView);
12. }
13.
14. NSLog(@"Finally, no longer hidden: %@", self->_myView);
15. }
以下是此代碼中的日志陳述句。第一個日志陳述句是正確的,因為它顯示 myView.hidden == YES。然而,第二個日志陳述句對我來說似乎是錯誤的,因為它仍然顯示 myView.hidden == YES 即使在上一行它只是設定為 NO。
設定 hidden=NO 之前:<MyView: 0x117ef6eb0; 框架 = (0 49.6667; 123.667 20.3333); 隱藏 = 是; layer = <CALayer: 0x280ddaa20>>
設定 hidden=NO 后:<MyView: 0x117ef6eb0; 框架 = (0 49.6667; 123.667 20.3333); 隱藏 = 是; layer = <CALayer: 0x280ddaa20>>
The next set of log statements are inside the loop, which it should not even enter anyway since I am setting myView.hidden to NO, but it goes in anyway because the value is still YES. And here it looks like it works correctly. The first log statement shows it is visible and then the next log statement shows it is hidden.
SHOULD NOT BE HERE - Before setting hidden=NO again: <MyView: 0x117ef6eb0; frame = (0 49.6667; 123.667 20.3333); hidden = YES; layer = <CALayer: 0x280ddaa20>>
SHOULD NOT BE HERE - After setting hidden=NO again: <MyView: 0x117ef6eb0; frame = (0 49.6667; 123.667 20.3333); layer = <CALayer: 0x280ddaa20>>
Finally, no longer hidden: <MyView: 0x117ef6eb0; frame = (0 49.6667; 123.667 20.3333); layer = <CALayer: 0x280ddaa20>>
Update 2
I know this code seems to be working on its own, but it is not working for me in my project. I will show the code for my view class here and also the output from a debug session showing the same behavior observed in the code.
And I know it might be in my code, but at the same time, I just do not see how. All my code consists of here is a call to setHidden:. Nothing extra. Before calling setHidden, the value of hidden is YES. After calling setHidden:NO, the value is still YES. I do not understand that. I am wondering if this is maybe a compiler issue. I know these compilers are very well tested, but at the same time I also do not understand how it is my code. I am simply setting hidden = NO, but it is not working unless I do it twice.
Debug Session
Here is the output from LLDB. I set a breakpoint right before the view was about to be unhidden (line 3 in the previous code snippets). At this point, myView.hidden = YES.
So all I did was to print the value of hidden for that view, and it correctly showed YES. After this, I ran call self.myView.hidden = NO to try to update it, but that doesn't work as can be seen in the debug statement that is printed out right below the call statement. It still shows hidden = YES;. I also went ahead and printed the value again just to be sure, and it still shows hidden = YES.
(lldb) p self.myView.hidden
(BOOL) $12 = YES
(lldb) call self.myView.hidden = NO
<MyView: 0x12b138980; frame = (0 49.6667; 123.667 20.3333); hidden = YES; layer = <CALayer: 0x283addfe0>> MyView::setHidden:NO
(BOOL) $13 = NO
(lldb) p self.myView.hidden
(BOOL) $15 = YES
Next, I just set the value to NO again and this time it works as can be seen by the debug statement and I also printed the value again for good measure.
(lldb) call self.myView.hidden = NO
<MyView: 0x12b138980; frame = (0 49.6667; 123.667 20.3333); layer = <CALayer: 0x283addfe0>> MyView::setHidden:NO
(BOOL) $16 = NO
(lldb) p self.myView.hidden
(BOOL) $17 = NO
Here is the code for my view class that gets shown and hidden. I am not overriding or doing anything with the hidden property, so any call to setHidden: goes straight to the method on UIView.
MyView.h
#import <UIKit/UIKit.h>
#import "MyModel.h"
@interface MyView : UIView
@property (strong, nonatomic, nullable) MyModel *myModel;
@end
MyView.m
#import "MyView.h"
@interface MyView ()
@property (strong, nonatomic) UILabel *label;
//other UI components are here, but they are just more labels and an image view
@end
@implementation MyView
- (instancetype)init {
return [self initWithFrame:CGRectZero];
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
[self initialize];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initialize];
}
return self;
}
- (void)initialize {
[self addSubview:self.label];
//add other labels and the image view
[NSLayoutConstraint activateConstraints:@[
[self.label.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.leadingAnchor],
[self.label.topAnchor constraintGreaterThanOrEqualToAnchor:self.topAnchor],
[self.label.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
//more constraints for the other labels and the image
]];
}
- (void)setMyModel:(MyModel *)myModel {
self->_myModel = myModel;
[self updateDisplay];
}
- (void)updateDisplay {
//set the text of all the labels based on the model
}
- (UILabel *)label {
if (!self->_label) {
self->_label = [[UILabel alloc] init];
self->_label.translatesAutoresizingMaskIntoConstraints = NO;
self->_label.numberOfLines = 0;
self->_label.text = @"My Text:";
[self->_label setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
[self->_label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}
return self->_label;
}
@end
Please let me know if there is anything else that I should post that would help or if there is anything I could try. I can just write the value twice in my code, but without understanding why I have to do it, I feel that is sort of dangerous because how do I know that two times will always be sufficient? Plus, it is just weird to have to set a variable to the same value twice in a row for it to work.
Thank you to everyone for your help with this.
uj5u.com熱心網友回復:
是的,在UIStackView.
您應該能夠通過將其添加到您的自定義視圖類來糾正該問題:
- (void)setHidden:(BOOL)hidden {
if (self.isHidden != hidden) {
[super setHidden:hidden];
}
}
這是一個顯示問題的完整示例,并顯示“修復”:https : //github.com/DonMag/StackViewBug
uj5u.com熱心網友回復:
看起來這是由于 UIStackView 中的一個錯誤,如果您多次隱藏一個視圖,它會累積該視圖的隱藏計數。因此,例如,想象一個這樣的視圖層次結構:
- 界面堆疊視圖
- 我的看法
那么,如果設定了MyView3次hidden=YES,則需要設定3次hidden=NO才能真正設定為NO。反過來,這似乎不是問題。因此,如果您將它設定為 hidden=NO 三次,則只需將其設定為 hidden=YES 一次,它將被隱藏。
此 StackOverflow 答案中有更多資訊:https ://stackoverflow.com/a/45599835/5140550
我不確定此錯誤是否已報告給 Apple,但它似乎是 UIStackView 中的錯誤。現在我只需要在我的代碼中找出一種干凈的方法來處理這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317364.html
