UITextView.text 属性第一次不更新,然后剪辑或不显示

发布于 2024-09-10 15:02:56 字数 1311 浏览 5 评论 0原文

我正在尝试将字典中的内容获取到 UITextView 中。字典包含与百分比配对的分子质量,例如:

24 -> 98
25 -> 1.9
26 -> 0.1

我有一个 NSArray,其中包含字典中的键,按升序排序。因此,这是我的代码,用于生成要设置为 textField.text 属性的字符串:

-(void)detailIsotopes:(NSMutableDictionary *)isotopes withOrder:(NSMutableArray *)order{
NSMutableString *detailString = [[NSMutableString alloc] init];

for (NSNumber *mass in order){
    [detailString appendString:[NSString stringWithFormat:@"%d: %f\n", [mass integerValue], [[isotopes valueForKey:[NSString stringWithFormat:@"%@", mass]] floatValue]]];
}

NSLog(@"%@", detailString);
textField.text = detailString;
[detailString release];
}

这应该创建一个如下所示的字符串:

24: 89
25: 1.9
26: 0.1

由于某种原因,此方法在第一次运行时从不执行任何操作。我看到 NSLog 输出,它输出正确的字符串。但是,UITextView 的内容不会改变:它们在 Interface Builder 中保持为“Lorem ipsum dolor sat amet...”。如果我再次运行该方法,它会起作用。

UITextView 显示一些文本,然后只截断一行的一半,只留下字符的顶部。如果我删除半行上方的内容,其他行就会从分界线下方拉起:内容就在那里,它们只是停止显示,如果您明白我的意思的话。如果我在视图中启用分页,这种情况似乎就会消失。如果我这样做,那么该行不会被截断,但 UITextView 只是在某个点后停止显示任何内容,尽管滚动条表明还有更多内容(确实有)。

设置内容后,包含 UITextView 的视图不可见(如果这有影响的话)。一个单独的视图控制器生成 NSMutableDictionary 和 NSMutableArray 并将它们发送到其委托,然后委托将它们发送到应显示 UITextField 并具有DetailIsotopes: withOrder: 方法的视图。可以通过信息按钮在两者之间进行交换。

有谁明白为什么会发生这些事情? 感谢您提供的任何建议!

I'm trying to get the contents from a dictionary into a UITextView. The dictionary contains molecular masses paired with percentages, for example:

24 -> 98
25 -> 1.9
26 -> 0.1

I have an NSArray containing the keys from the dictionary, sorted in ascending order. So, here is my code to generate the string to set as the textField.text property:

-(void)detailIsotopes:(NSMutableDictionary *)isotopes withOrder:(NSMutableArray *)order{
NSMutableString *detailString = [[NSMutableString alloc] init];

for (NSNumber *mass in order){
    [detailString appendString:[NSString stringWithFormat:@"%d: %f\n", [mass integerValue], [[isotopes valueForKey:[NSString stringWithFormat:@"%@", mass]] floatValue]]];
}

NSLog(@"%@", detailString);
textField.text = detailString;
[detailString release];
}

This should create a string looking like this:

24: 89
25: 1.9
26: 0.1

For some reason, this method never does anything the first time it runs. I see the NSLog output, which outputs the correct string. However, the contents of the UITextView don't change: they stay as 'Lorem ipsum dolor sit amet...' from Interface Builder. If I run the method again, it works, sort of.

The UITextView displays some of the text, and then just cuts off half way through a line, leaving only the tops of the characters. If I delete the contents above the half line, the other lines pull up from under the divide: the contents are there, they just stop being shown, if you understand what I mean. This appears to go away if I enable paging in the view. If I do that, then the line isn't truncated, but the UITextView just stops showing any content after some point, although the scroll bar indicates that there is more to go (which there is).

The view containing the UITextView is not visible when the contents is set, if that makes a difference. A separate view controller generates the NSMutableDictionary and NSMutableArray and sends them to its delegate, which then sends them to the view which should display the UITextField and has the detailIsotopes: withOrder: method. The two can be swapped between with an info button.

Does anyone understand why these things are happening?
Thanks for any advice you can give!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2024-09-17 15:02:56

首先,我认为您不需要在这里分配和释放 NSMutableString 。只需使用 [NSMutableString string] 初始化一个,它会创建一个可以修改的空字符串,无需显式释放。

第二件事,您似乎将质量存储在 NSDictionnary 中的 NSStrings 中,为什么您将其 integerValue 方法用于 stringWithFormat (带有 %d 修饰符) ,而不是按原样使用它们? (NSStrings 的 stringWithFormat 修饰符是 %@

另外,你一开始就谈论 UITextView,然后谈论 UITextField,你确定你没有在某个地方犯错误吗?我猜你格式化的 NSString 的接收对象应该是 UITextView (如果你同时有 textField 和 textView)。

如果与此无关,则可能您太早调用 detailIsotopes 并且 textView 尚未创建。尝试 NSLog 其地址并查看它是否是 nil 第一次。如果您使用 Interface Builder 并且您的 UITextField 是 ib 插座,则可能会出现这种情况。如果这样做,那么您可以将字典和数组存储在 viewController 中,并在 viewController 的 viewDidLoad 方法中设置 textField。或者在显示视图后调用 detailIsotopes ,我想这取决于您。

关于截断的文本,我认为这是因为 UITextView 不会自动调整自身大小,因此它保留了您最初设置的高度。我通常做的是这样的:

CGRect frame = textView.frame;
frame.size.height = textView.contentSize; // you can adjust this to leave some space at the end
textView.frame = frame;

这会将 textView 高度设置为内容(文本)高度。
另请注意,如果您的 textView 应该显示整个文本,您可以将其 scrollingEnabled 属性设置为 FALSE,这样它就永远不允许滚动。

希望有帮助。

First of all, I don't think you need to allocate and release your NSMutableString here. Simply initialize one using [NSMutableString string] which creates an empty string you can modify and don't need to explicitly release.

Second thing, you seem to store masses in NSStrings in your NSDictionnary, why do you use their integerValue method for stringWithFormat (with %d modifier), instead of using them as is? (the stringWithFormat modifier for NSStrings is %@)

Also, you talk about a UITextView at start, then about a UITextField, are you sure you did not make a mistake somewhere? I guess the receiving object for your formatted NSString should rather be the UITextView (if you have both a textField and textView).

If it's not about this, maybe you are calling detailIsotopes too early and the textView it's created yet. Try to NSLog its address and see if it's nil the first time. It could be the case if you use Interface Builder and your UITextField is an ib outlet. If you do, then you could store your dictionary and array in the viewController, and set the textField in the viewController's viewDidLoad method. Or call detailIsotopes after you've displayed the view, I guess that's up to you.

About the truncated text, I think that's because UITextView doesn't resize itself automatically, so it keeps the height you originally set. What I usually do is this:

CGRect frame = textView.frame;
frame.size.height = textView.contentSize; // you can adjust this to leave some space at the end
textView.frame = frame;

This will set the textView height to the content (the text) height.
Also note that if your textView is supposed to display the whole text, you can set its scrollingEnabled property to FALSE so it never allows scrolling.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文