tags:

views:

47

answers:

2

Assume I have ten lines of code,

1
2
[addressStreet setStringValue:street]
4
5
6
7
8
9
10

where addressStreet is a IBOutlet pointing to text field in UI and street is a NSString object.

Only after all the 10 lines are executed I could see the updated value in UI. My requirement is that the value should get reflected in UI immediately after line No 3 is executed and even before next line (No 4) is executed.

Tried with bindings too! Negative.

Is there a way that I can achieve this?

Thanx in advance!

+1  A: 

In general, if you have some very long-running computation that's blocking your UI, it's a good idea to either break it into chunks or run it on a second thread. The user will not be able to interact with anything anyway as long as this method is clogging the main thread.

However, the direct answer to your question is that you need to tell the field to redraw itself. Ordinarily it would do this at the next event loop, but since the method is stopping that from coming around, it's not happening. You can tell a view to redraw by calling [view display].

Chuck
Thnx for the reply. Let me try it and get back to you.
Vishnu
+2  A: 

The field redraws when you get back to the UI loop. Your problem is that you're taking too long to do that. So, don't take so long.

There are a number of ways to do that:

  1. Delayed perform (by far the easiest)
  2. Grand Central Dispatch (Snow Leopard only)
  3. NSOperation/NSOperationQueue
  4. A timer
  5. Threads

Most of these are inappropriate for your situation, but I don't know what your situation is (i.e., what all those other lines of code are doing), so I don't know which solutions are appropriate and which ones aren't. You'll have to look into all of these solutions and decide for yourself which one is most appropriate.

Simply telling the field to draw immediately is not a solution. Yes, it draws immediately, but your UI is still blocked—your user cannot do anything until you return from your method, and the only solution to that is to return sooner. Once you do that, telling the field to draw immediately becomes unnecessary.

Peter Hosey
Thnx for the reply. Let me try it and get back to you.
Vishnu