English 中文(简体)
如何在用户违约的情况下拯救开关状态?
原标题:how to save switch state with user defaults?

Is there a method to store the state of a UISwitch with NSUserDefaults?
If the state is ON I d like to set some action...

Can I do this?
Thanks!

最佳回答

热帕韦2的答案是好的,也能很好地促进大片部分控制(超过2个州)。 但是,如果你只想储存2个州,那么为什么不只使用<条码>。

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   [userDefaults setBool:switchState forKey:@"mySwitchValueKey"];

并公布:

   BOOL swichState = [userDefaults boolForKey:@"mySwitchValueKey"];

仇恨更简单,甚至根本不成规律,不扼制反向和改换。

问题回答

储蓄:

- (void)mySwitchAction:(id)sender
{
  if (sender == mySwitch) {
    BOOL mySwitchValue = [ sender isOn ];
    NSString *tmpString = mySwitchValue ? @"1" : @"-1" ;
    NSUserDefaults  *myNSUD = [NSUserDefaults standardUserDefaults];
    [ myNSUD setObject:tmpString forKey: @"mySwitchValueKey" ];
    [ myNSUD synchronize ];
    // do other stuff/actions
  }
}

从挽救的国家开始:

NSUserDefaults  *myNSUD = [NSUserDefaults standardUserDefaults];
NSString *tmpString =  [ myNSUD stringForKey: @"mySwitchValueKey"];
BOOL mySwitchValue = NO;  // or DEFAULT_VALUE
if (tmpString != nil) { 
  mySwitchValue = ( [ tmpString intValue ] == 1 ); 
}
[mySwitch setOn: mySwitchValue];




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签