zzhang2006
asked on
Mac/iOS Xcode -- how to lauch a asynchrounous thread by pressing a button, but not locking up the button
Just started Xcode today and I was able to create two buttons and a UILabel and load the "app" to my iPad. When I press a button the label display a text "Hello" and when I press another button, the label text changed to "how are you?".
While that's cool, I wanted to be able to let the Label change the text every few seconds by itself, after I press a button and stop the flashing by pressing another button. How can I do this? I was trying something like this:
button_pressed_function { // excuse me, for being a C++ programmer for many years
I = 0
while(I < 8)
{
I++;
label.text = "hello";
sleep(3);
label.text = "what's up?";
sleep(3);
}
Apparently it didn't work though the while-loop did run and it freezed the button before it exits. It freezed for about 48 = 8x6 seconds and just showed "what's up" in the label, never change the text during the loop run. I know this is certainly not the right way, because 1) even if did flip the text, it locks the button (is there a method of UIlabel to
reader the text in the middle for the loop?) 2) It didn't even change the text.
I guess there should be a function to dispatch another thread for the while_loop in the background and release the button-pressed function so the button can pop back up. Please help, and sample code will be very appreciated.
Thanks
While that's cool, I wanted to be able to let the Label change the text every few seconds by itself, after I press a button and stop the flashing by pressing another button. How can I do this? I was trying something like this:
button_pressed_function { // excuse me, for being a C++ programmer for many years
I = 0
while(I < 8)
{
I++;
label.text = "hello";
sleep(3);
label.text = "what's up?";
sleep(3);
}
Apparently it didn't work though the while-loop did run and it freezed the button before it exits. It freezed for about 48 = 8x6 seconds and just showed "what's up" in the label, never change the text during the loop run. I know this is certainly not the right way, because 1) even if did flip the text, it locks the button (is there a method of UIlabel to
reader the text in the middle for the loop?) 2) It didn't even change the text.
I guess there should be a function to dispatch another thread for the while_loop in the background and release the button-pressed function so the button can pop back up. Please help, and sample code will be very appreciated.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.