Loops in Objective C?

  • so I have a .m file, here's the contents.

    #import "RotationHandler.h"

    @implementation RotationHandler
    int z=1;
    float rot;
    - (IBAction)buttonRelease:(id)sender {
    do
    {
      rot=rot-0.5;
      [sliderRotation setFrameCenterRotation: rot];
      [printRotation setFloatValue: rot];
    }
    while(z=1);
    }

    @end

    I won't bother showing you the other files in the project I know
    they're all in order. So I want the object receiving sliderRotation to
    rotate, in the above code I want it to rotate forever. If I remove the
    loop, each time I press the button it rotates -.5 degrees. But with
    the loop in there, I get the horrible spinning beachball of eternity
    when I press the button and the object that is supposed to rotate
    never moves. Do I have to use some special Objective C loop?
  • >
    > I won't bother showing you the other files in the project I know
    > they're all in order. So I want the object receiving sliderRotation
    > to rotate, in the above code I want it to rotate forever. If I
    > remove the loop, each time I press the button it rotates -.5
    > degrees. But with the loop in there, I get the horrible spinning
    > beachball of eternity when I press the button and the object that is
    > supposed to rotate never moves. Do I have to use some special
    > Objective C loop?

    No, no such thing, just your loop never modifies the value of z, so
    your program's execution never proceeds outside the loop.  The GUI
    can't redraw if all your program is doing is looping infinitely.

    Bob
  • On May 26, 2008, at 1:20 PM, Nathan wrote:

    > so I have a .m file, here's the contents.
    >
    > #import "RotationHandler.h"
    >
    > @implementation RotationHandler
    > int z=1;
    > float rot;
    > - (IBAction)buttonRelease:(id)sender {
    > do
    > {
    > rot=rot-0.5;
    > [sliderRotation setFrameCenterRotation: rot];
    > [printRotation setFloatValue: rot];
    > }
    > while(z=1);
    > }
    >
    > @end
    >
    > I won't bother showing you the other files in the project I know
    > they're all in order. So I want the object receiving sliderRotation
    > to rotate, in the above code I want it to rotate forever. If I
    > remove the loop, each time I press the button it rotates -.5
    > degrees. But with the loop in there, I get the horrible spinning
    > beachball of eternity when I press the button and the object that is
    > supposed to rotate never moves. Do I have to use some special
    > Objective C loop?

    The problem here is that looping forever will not let the rest of the
    program run. So the drawing code will never get called. Look into
    NSTimer to run a method that will do your rotation on some interval.

    --Nathan
  • Thanks, I'll look into NSTimer then.
    On May 26, 2008, at 3:31 PM, Nathan Kinsinger wrote:

    >
    > On May 26, 2008, at 1:20 PM, Nathan wrote:
    >
    >> so I have a .m file, here's the contents.
    >>
    >> #import "RotationHandler.h"
    >>
    >> @implementation RotationHandler
    >> int z=1;
    >> float rot;
    >> - (IBAction)buttonRelease:(id)sender {
    >> do
    >> {
    >> rot=rot-0.5;
    >> [sliderRotation setFrameCenterRotation: rot];
    >> [printRotation setFloatValue: rot];
    >> }
    >> while(z=1);
    >> }
    >>
    >> @end
    >>
    >> I won't bother showing you the other files in the project I know
    >> they're all in order. So I want the object receiving sliderRotation
    >> to rotate, in the above code I want it to rotate forever. If I
    >> remove the loop, each time I press the button it rotates -.5
    >> degrees. But with the loop in there, I get the horrible spinning
    >> beachball of eternity when I press the button and the object that
    >> is supposed to rotate never moves. Do I have to use some special
    >> Objective C loop?
    >
    > The problem here is that looping forever will not let the rest of
    > the program run. So the drawing code will never get called. Look
    > into NSTimer to run a method that will do your rotation on some
    > interval.
    >
    > --Nathan
  • I'm looking into NSTimer then. Thanks!

    On May 26, 2008, at 3:30 PM, Thomas Davie wrote:

    >
    > On 26 May 2008, at 21:30, Nathan wrote:
    >
    >> How do I redraw then?
    >>
    >> On May 26, 2008, at 3:26 PM, Thomas Davie wrote:
    >>
    >>>>
    >>>> I won't bother showing you the other files in the project I know
    >>>> they're all in order. So I want the object receiving
    >>>> sliderRotation to rotate, in the above code I want it to rotate
    >>>> forever. If I remove the loop, each time I press the button it
    >>>> rotates -.5 degrees. But with the loop in there, I get the
    >>>> horrible spinning beachball of eternity when I press the button
    >>>> and the object that is supposed to rotate never moves. Do I have
    >>>> to use some special Objective C loop?
    >>>
    >>> No, no such thing, just your loop never modifies the value of z,
    >>> so your program's execution never proceeds outside the loop.  The
    >>> GUI can't redraw if all your program is doing is looping infinitely.
    >>>
    >>> Bob
    >
    > You need to either spread your loop over the run loop, or run it on
    > another thread.  I'd suggest the earlier by having a timer fire a
    > method off at regular intervals to update the control.
    >
    > Bob
  • On May 26, 2008, at 12:20 PM, Nathan wrote:

    > while(z=1);

    In addition to what others said, change that from assignment "=" to
    comparison "==".

    -- Chris Page - Text Editor

    An ASCII character walks into a bar.  Bartender asks, “What’ll you
    have?”  ASCII character says, “Give me a double.”  Bartender asks,
    “Having a bad day?”  ASCII character says, “Yeah, I have a parity
    error.”  Bartender says, “Hmmm. I thought you looked a bit off.”
previous month may 2008 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Go to today