views:

143

answers:

2

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!

A: 

Hi,

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.

Jukurrpa
Thanks, that fixed it. I was trying to set the value of the textview while it was still nil. As for the truncating issue, it seems to have gone away once I fixed the textview code. So, thanks :)
Ben Ward
A: 

Hello, I have a similar problem - I have a UITextView property of my viewcontroller - marked as IBOutlet and linked to the actual control in IB. This property stays nil after the view controller has been initialized with initWithNibName and after the UI message loop has been executed after that (but the view controller with the UITextView is not shown). Why does this happen???

avasilev