2010-03-27 7 views
10

UITableViewCell 안에 UISwitch의 도청 방법을 알려주는 방법은 무엇입니까?UITableViewCell 내부의 UISwitch가 두드려 졌는지 확인하는 방법은 무엇입니까?

UISwitch과 같이 셀 (일반 세포)의 내부에 설정됩니다

UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease]; 
[cell addSubview:mySwitch]; 
cell.accessoryView = mySwitch; 

그리고이 같은 탭을 감지하는 것을 시도하고있다 (그러나 작동하지의) :

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { 

    NSUserDefaults *prefs; 


    if(indexPath.section == 1){ 

     switch(indexPath.row) 
     { 
      case 0: 

       NSLog(@"Tapped Login Switch");    

       break; 
      default: 
       break; 
     } 

    } 


} 

데이브 DeLong 나는 해결책으로 각 스위치에 대한 작업을 설정하는 것이 좋습니다. 문제

-(IBAction)switchToggled1:(id)sender { 


    NSUserDefaults *prefs; 

    NSLog(@"Tapped Login Switch"); 

    prefs = [NSUserDefaults standardUserDefaults]; 

    if(at_login){ 
     [prefs setObject:@"NO" forKey:@"autotweet_login"]; 
     at_login = NO; 
    }else{ 
     [prefs setObject:@"YES" forKey:@"autotweet_login"]; 
     at_login = YES; 
    } 



} 

스위치를 ON되지 않습니다

 UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease]; 
     [mySwitch addTarget:self action:@selector(switchToggled2:) forControlEvents: UIControlEventTouchUpInside]; 
     if(at_songs){ 

      [mySwitch setOn:YES animated:NO]; 

     } 
     [cell addSubview:mySwitch]; 
     cell.accessoryView = mySwitch; 



그리고이 도청 때 알고 다음 : 그래서 스위치를 설정하려면 다음을했다. 이제 문제는 UISwitch가 OFF로 설정된 경우 어떤 이유로 그 동작이 두 번 호출된다는 것입니다 (그리고 1 탭에 대해 2 개의 NSLog를 얻음).



작업이 스위치를 OFF 설정하는 하나 개의 탭에 대해 두 번 호출하기 왜 ? 어떻게 수정해야합니까?

답변

12

스위치에게 목표와 행동을 보내기

[mySwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside]; 

그런 다음 switchToggled 구현 : 방법 :

- (void) switchToggled:(id)sender { 
    //a switch was toggled. 
    //maybe use it's tag property to figure out which one 
} 
+0

감사합니다. tag 속성을 사용하는 대신 각 스위치에 대해 특정 작업 (switchToggled1, switchToggled2)을 만들었습니다. 하지만 한 가지 문제가 있습니다. 스위치를 한 번 탭하면 어떤 이유로 액션이 두 번 호출됩니다. 왜 그것이 두 번 불리는거야? (업데이트 : 스위치를 끄면 액션이 두 번 호출되고 스위치를 켜면 액션이 한 번만 호출됩니다.) – RexOnRoids

+0

위의 샘플 코드에서 @selector가 아닌 @selection이어야합니다. 그게 내 시간의 약 30 분을 알아 내야했다. –

+0

@ Chuck whoops! 미안합니다. 브라우저에서 코드를 입력 할 때 단점이 있습니다 ... –

1

가 switchToggled가 두 번 호출 이유 해결인가요을? 나에게도 일어나고있어. NSLog를 두 번 로깅합니다. 하지만 내 경우에는 무작위로. 때로는 OFF에서 두 번 호출되기도하고 때로는 켜기도합니다. 사람들이 다수의 접촉에 문제가에 대한 로그

2010-08-17 18:12:30.264 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:12:33.032 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:12:33.032 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:12:33.760 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:12:46.223 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:12:47.383 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:12:48.000 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:12:48.623 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:12:49.176 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:12:59.687 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:12:59.688 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:00.246 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:13:00.759 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:00.759 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:05.638 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:13:06.391 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:06.391 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:07.078 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:13:07.830 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:07.830 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:08.622 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:13:09.261 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:09.262 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:15.565 SimplyPersonnelV1[3190:207] Auto Login turned off 
2010-08-17 18:13:16.485 SimplyPersonnelV1[3190:207] Auto Login turned on 
2010-08-17 18:13:16.486 SimplyPersonnelV1[3190:207] Auto Login turned on 
+0

이것이 해결 되었습니까? 나는'switchChanged :'메소드의 끝 부분에'[table reloadData]'가 있으면'switchChanged :'메소드가 두 번 실행된다는 것을 알았다. –

11

를 부착하면 UIControlEventValueChanged

[catSwitch addTarget:self action:@selector(catSwitched:) forControlEvents: UIControlEventValueChanged]; 

내가 고민이 방법을 가지고 있지 않다으로 제어 변경 이벤트 노력했다.

+0

'@ selector'에서'valueChanged :'를 사용할 수도 있습니다. –

관련 문제