简体   繁体   English

iPhone和应用程序设置

[英]iPhone and Application Settings

I want to have a setting in an iphone app that uses a toggle switch to allow something to be turned on or off. 我想在iphone应用程序中设置一个使用切换开关的设置,以允许打开或关闭某些内容。 I have seen tutorials, but they only show how to do this in the iPhone's settings place. 我见过教程,但他们只展示了如何在iPhone的设置位置执行此操作。 I want this done inside the application. 我希望在应用程序中完成此操作。 Any guides, help advice. 任何指南,帮助建议。 I'm going for something similar to the picture below. 我想找一些类似下图的东西。

在此输入图像描述

You can use the UISwitch as accessoryView. 您可以将UISwitch用作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. 您可以使用UISwitch。 Here's the very simple class reference guide. 这是非常简单的课程参考指南。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UISwitch_Class/Reference/Reference.html 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. 基本上你可以通过检查它的“on”属性来检查它的状态。

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

First, make sure you're UITableView style is set to "Grouped" 首先,确保你的UITableView样式设置为“Grouped”

Then, in your cellForRowAtIndexPath method, do something along these lines: 然后,在您的cellForRowAtIndexPath方法中,沿着以下行执行某些操作:

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. 请记住稍后释放UISwitch对象并包含用于将其设置为打开或关闭的代码,具体取决于它应处于什么状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM