How to tell wenn ein UISwitch innerhalb einer UITableViewCell erschlossen hat?

How to tell wenn ein UISwitch innerhalb eines UITableViewCell erschlossen hat?

Meine UISwitch Einrichtung innerhalb der Zelle (generic cell) wie folgt:

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

Ich versuche zu erkennen, einen Wasserhahn wie diese (aber nicht arbeiten):

- (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;
        }

    }


}

Dave DeLong schlug vor, dass ich eine Aktion für jeden switch als Lösung. Also habe ich Folgendes zum einstellen der Schalter:

        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;

Und die folgenden, zu wissen, Wann es war angezapft:

-(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;
    }



}

Drehen Sie den Schalter AUF ist nicht ein problem. Das problem ist JETZT, dass, wenn die UISwitch auf OFF festgelegt ist, für einige Grund, seine Aktion wird zweimal genannt (Und ich bekomme 2 NSLogs für 1 Tippen Sie auf).

Warum ist die Aktion immer ZWEIMAL genannt, für die man nur durch Tippen schalten Sie den Schalter AUS? Wie kann ich es beheben?

InformationsquelleAutor RexOnRoids | 2010-03-27
Schreibe einen Kommentar