iphone - 如何在 Table View ViewDidLoad 方法中调用自定义单元格中声明的 UISwitch?

标签 iphone ios xcode tableview uiswitch

我有一个 TableView 和一个自定义单元格,该单元格具有 UILabelUISwitch。 UISwitch 在 Custom Cell 中声明。我想在 TableView 的 ViewDidLoad 中调用这个开关。 我想使用 NSUserDefaults 来保存 UISwitch 的状态。要加载值,我必须在 ViewDidLoad 中编写代码。

@interface LabelSwitchCustomCell : UITableViewCell {
    IBOutlet UILabel  *mainLabel;
    IBOutlet UISwitch *switchButton;
}

@property (nonatomic, retain) IBOutlet UILabel  *mainLabel;
@property (nonatomic, retain) IBOutlet UISwitch *switchButton;

@end

最佳答案

您可以在代码的任何位置将 UISwitch 的状态保存在 NSUserDefaults 中。 您可以通过这种方式设置/加载开关状态:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
//Set switch states
    if (indexPath.row == 0) 
                cell.switchBtn.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"Switch1State"];
            else if(indexPath.row == 1)
                cell.switchBtn.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"Switch2State"];
            else if(indexPath.row == 2)
                cell.switchBtn.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"Switch3State"];
// Set switch states

// Set action for your switch
[cell.switchBtn addTarget:self action:@selector(switchingBtn:) forControlEvents:UIControlEventValueChanged];
    }

关于iphone - 如何在 Table View ViewDidLoad 方法中调用自定义单元格中声明的 UISwitch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664874/

相关文章:

iphone - 任何人都知道如何在 Xib 文件中插入/使用特殊字符或符号

ios - iAd 不工作

ios - Swift iOS 应用存档错误

xcode - 未找到 iOS 开发证书。

iphone - UIPanGestureRecognizer - View 在拖动时消失

iphone - 在 iOS 中显示像素矩阵的最佳方式

iphone - 从网站到 iPhone 应用程序的内容

javascript - 使用云代码和 PFFacebookUtils 覆盖电子邮件/用户名

在 macOS 上使用 Swift 和 Electron

iphone - EGO照片查看器 : Start at specific photo