开发者

iPhone and Application Settings

开发者 https://www.devze.com 2023-02-21 11:25 出处:网络
I want to have a setting in an iphone app that uses a toggle switch to allow something 开发者_StackOverflowto be turned on or off.I have seen tutorials, but they only show how to do this in the iPhone

I want to have a setting in an iphone app that uses a toggle switch to allow something 开发者_StackOverflowto be turned on or off. I have seen tutorials, but they only show how to do this in the iPhone's settings place. I want this done inside the application. Any guides, help advice. I'm going for something similar to the picture below.

iPhone and Application Settings


You can use the UISwitch as accessoryView. This will look (almost?) exactly like in your picture.

Something like this:

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UISwitch *mySwitch = [[[UISwitch alloc] init] autorelease];
        [mySwitch addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = mySwitch;
    }
    // configure cell
    UISwitch *mySwitch = (UISwitch *)cell.accessoryView;
    mySwitch.on = YES; // or NO
    cell.textLabel.text = @"Auto Connect";

    return cell;
}

- (IBAction)switchToggled:(UISwitch *)sender {
    UITableViewCell *cell = (UITableViewCell *)[sender superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSLog(@"Switch %i,%i toggled", indexPath.section, indexPath.row);
}


You can use a UISwitch. Here's the very simple class reference guide.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UISwitch_Class/Reference/Reference.html

Basically you can check its state by checking its "on" property.

if(mySwitch.on) {
   //do something here
}


First, make sure you're UITableView style is set to "Grouped"

Then, in your cellForRowAtIndexPath method, do something along these lines:

if (indexPath.section == kSwitchSection) {


    if (!randomControl) {
        randomControl = [ [ UISwitch alloc ] initWithFrame: CGRectMake(200, 10, 0, 0) ];
        [randomControl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        randomLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,180,30)];
        [randomLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [randomLabel setText:@"My Label"];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell addSubview:randomControl];
    [cell addSubview:randomLabel];
}

Remember to release the UISwitch object later and to include code for setting it to on or off depending on what state it should be in.

0

精彩评论

暂无评论...
验证码 换一张
取 消