Sunday, 15 July 2012

XCode View Controller Not Updating in Do Loop -



XCode View Controller Not Updating in Do Loop -

i have loop want execute command every 1 sec while switch on.

the code works fine once, when don't have loop.

however, add together loop, none of labels in view controller updated, button storyboard doesn't work, , switch not toggle off. essentially, loop keeps looping, nil on screen work, nor can out.

i know i'm doing wrong. but, don't what. thoughts appreciated.

i attached code gets me in trouble.

thanks,

- (ibaction)roaming:(id)sender { uiswitch *roamingswitch = (uiswitch *)sender; bool ison = roamingswitch.ison; if (ison) { last=[nsdate date]; while (ison) { current = [nsdate date]; interval = [current timeintervalsincedate:last]; if (interval>10) { thecommand.text=@"on"; [self combo:sendcommand]; last=current; } } } else { thecommand.text=@"off"; }

}

ios , osx event based systems , cannot utilize loops in main (ui) thread want do, otherwise don't allow run loop run , events stop beingness processed.

see: mac app programming guide section "the app’s main event loop drives interactions".

what need set-up timer (nstimer) fire every second:

.h file:

@interface myclass : nsview // or whatever base of operations class { nstimer *_timer; } @end

.m file:

@implementation myclass - (id)initwithframe:(nsrect)frame // or whatever designated initializier class { self = [super initinitwithframe:frame]; if (self != nil) { _timer = [nstimer timerwithtimeinterval:1.0 target:self selector:@selector(timerfired:) userinfo:nil repeats:yes]; } homecoming self; } - (void)dealloc { [_timer invalidate]; // if using mrr only! [super dealloc]; } - (void)timerfired:(nstimer*)timer { if (roamingswitch.ison) { thecommand.text=@"on"; [self combo:sendcommand]; } } @end

xcode loops

No comments:

Post a Comment