Using isOn gives you whether the switch is on of off. I'm not sure if this is what you are looking for. In my case I wanted to know whether the user is manipulating (touching) the switch, regardless of its state. I found a way to do it. The code is as follows:

Add this upon creation of the switch.

[theSwitch addTarget:self action:@selector(controlTouched:forEvent:) forControlEvents:UIControlEventAllTouchEvents] ;

Add the following to the class referred by self in the above sentence.

- (void)controlTouched:(UIControl *)sender forEvent:(UIEvent*)event
   {
    UITouch *touch = [[event allTouches] anyObject] ;
    BOOL tag ;
    if ( touch == nil ) tag = NO ;
    else
       {
        UITouchPhase phase = [touch phase] ;
tag = (phase != UITouchPhaseEnded && phase != UITouchPhaseCancelled) ;
       }
    [sender setTag:tag] ;
   }

Now you have 1 in the tag property if the switch is being touched, or 0 otherwise.

Hope that helps.

Joan Lluch-Zorrilla


El 01/04/2009, a las 16:03, Brian Slick escribió:

We may be able to combine forces here. What I was doing gets the correct answer from the switch, but what I was finding is that certain circumstances do not result in a message being sent from the switch. I forget just now which circumstance it was, but I believe it was a direct tap on the "button" of the switch. Swiping the switch worked fine, tapping on the text of the switch worked fine, but tapping on the button did not send a message. I finally just punted and asked each switch for their current status in viewWillDisappear, but this means I cannot react to changes while the user is still in the view.

So, if you use your method, but change it to [sender isOn], does that work? I hope so, because that opens up some possibilities for me.

Brian

On Mar 31, 2009, at 9:22 AM, Joan Lluch-Zorrilla wrote:

I need to know whether the user is touching inside a UISwitch control. The UIControlEvent(s) do fire, but then isTouchInside always gives NO. My code is:

- (void)switchTouched:(UIControl *)sender
 {
  NSLog( @"switchtouched:%d", [sender isTouchInside]) ;
 }

The switchTouched method was added as a target to touch events upon creation of the switch like this

[switchv addTarget:self action:@selector(switchTouched:) forControlEvents:UIControlEventAllTouchEvents] ;

Whatever the user does on the switch the result is always Zero. What am I missing?

Joan Lluch-Zorrilla









_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to