views:

52

answers:

3

In my app I update a textView progamatically. I actually loop through a dataset from a SQLITE3 DB and show a specific text for a given time. Then I would like to show the text from the next dataset record and so forth. I have been browsing through all kind of forums and the apple doc as well, but could not find anything like a repaint, refresh, updateTextView or so command. I am dealing with long strings, why I thought the UITextView would be the best way to display. However, if another UI Class, like the UITextField or UILabel would be better and easier to achieve what I am looking for, I would also use that. No problem.

Cheers, René

A: 

Does [myTextView setNeedsDisplay]; work? That is the traditional way to tell an iOS UIView that it needs to see about redrawing itself. The UIView documentation contains some good information on that.

samkass
A: 

What exactly is your question?

Are you asking how to set text in a UITextView? Just use its text property:

someTextView.text = @"Some Text";

Are you asking if it's "better" to use a UILabel or UITextField? That's a subjective question. It depends on how your app is designed and what makes the most sense for presenting textual information. (Presenting formatted HTML in a UIWebView is another option.)

Once you set the text of any of these built-in classes, it will know to update/redraw itself. You don't need to set text and then trigger the redrawing or "repainting" manually.

Shaggy Frog
Sorry, seem I was not clear on my question. samkass got the point though, as I tried the setNeedsDisplay method on my UITextView. I need to do that as I am in a loop reading all the records, which contain my textblocks to display iteratively, here the code snippet: while(sqlite3_step(selectstmt) == SQLITE_ROW) { self.myText1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)]; self.myTextView1.text = self.question; [self.myTextView1 setNeedsDisplay]; }
renesteg
BTW: The setNeedsDisplay did not work, to answer samkass question. Only the very first record gets displayed and the the very last. Even though I send the setNeedsDisplay message to UITextView it does not update the display within the loop.
renesteg
Like I said, as soon as you have `self.myTextView1.text = self.question;`, `myTextView1` is already flagged itself as needing to update its display, so `[self.myTextView1 setNeedsDisplay];` will do nothing. I think what you are asking is "When I set the `text` property of a `UITextField` in a loop, why isn't the display update?" The answer is "The UI runs on a single thread, so until you're done your loop, it won't have a chance to update the UI at all"
Shaggy Frog
Ok, understood and actually makes perfectly sense. Will go back and check for another solution in that case. I am actually thinking if I may solve that with the selectors as I also need the app to wait a certain time before it displays the next text in the UITextView. Thanks anyway. Cheers, René
renesteg
A: 

Ok I got the solution now. As always, it is pretty simple as soon as you know how :-) As it seems, that I was not very clear on my initial problem description, here the feature description again: I needed my app to display text from my SQLITE3 database in a UITextView and update that text every 2 seconds with the text from the next record in the DB. So my initial thought was to do it within the Select loop. With the hint of shaggy, that it the view will not be updated, before finishing the loop as it is running in a single thread, I started to look for another solution and came up with the following selector based solution, which works fantastic.

The solution to such a programatic and iterative update of any object in the user interface is a method like this

- (IBAction)displayTextFromArray:(UIButton *)sender{
timer = [NSTimer scheduledTimerWithTimeInterval:(2.0) target:self selector:@selector(displayText) userInfo:nil repeats:YES];    

}

This method in my case is invoked when the user touches a UIButton. To make the schedule stop, it is required to send the following message to the timer instance:

        [timer invalidate];

Of course I had to change my code a bit to run with the scheduled timer, but from an iteration perspective, the above code is everything you need to trigger and stop a scheduler. Hope that this helps others with a similar issue too.

Cheers, René

renesteg