使用NSUserDefaults保存按钮的状态

时间:2012-05-16 07:55:09

标签: iphone

我是iphone的新手。我正在做一个项目,因为我被击中了。我的问题是当我点击时我在我的应用程序中有一个按钮,我放置一个代码来更改按钮的标题,如下所示代码

- (IBAction)syncOffClickedInRegisterUserScreen:(id)sender {
     if ([syncOnorOff.titleLabel.text isEqualToString:@"Sync off"]) {
        [syncOnorOff setTitle:@"Sync on" forState:UIControlStateNormal];
    } 
    else{
        [syncOnorOff setTitle:@"Sync off" forState:UIControlStateNormal];
    }
  }

工作正常 这是应用程序输入后台的代码

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"1");
   registerUserScreen = [[RegisterUserScreen alloc]init];
    //if([[[registerUserScreen syncOnorOff]currentTitle]isEqualToString:@"Sync off"]){
    // NSLog(@"title is %@",[[registerUserScreen syncOnorOff]currentTitle]);
    NSTimeInterval interval = 1;
    NSDate *alertTime = [NSDate dateWithTimeIntervalSinceNow:interval];
    UIApplication *app = [UIApplication sharedApplication];
    UILocalNotification *notifyAlarm = [[UILocalNotification alloc]init];
    if(notifyAlarm){
        notifyAlarm.fireDate = alertTime;
        notifyAlarm.alertBody = @"Sync Events";
        notifyAlarm.alertAction = @"Sync";
        notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
        [app scheduleLocalNotification:notifyAlarm];
    }
 }

我的问题只要按钮具有标题在用户屏幕中同步并进入后台状态,它将执行后台的数据,如果按钮在用户屏幕上具有标题同步,则进入后台状态不会执行背景中的数据。如果有可能的话。如果有人知道请帮帮我.....

1 个答案:

答案 0 :(得分:0)

我真的不明白你的问题,但根据你的标题,这里是如何保存/读取NSUserDefaults的按钮标题:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//read
[defaults stringForKey:@"buttontitle"];
//write
[defaults setValue:[[registerUserScreen syncOnorOff]currentTitle] forKey:@"buttontitle"];
相关问题