English 中文(简体)
如何拯救申请中的开关状态
原标题:How to save the state of switch in the application

我正与转换控制合作,只要我从网页上航行或重新开始申请,其价值套就会违约。 怎么去做,这样,如果施加压力,它就应当中止,或者如果停止,就应该中止,直到我们手工改变为止?

Here is my code where I m allocating and setting ffram for scrollview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell new";
     listData =[self.tableContents objectForKey:[self.sortedgroups objectAtIndex:[indexPath section]]];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];


    if(indexPath.section==0)
    {  
        switch (indexPath.row) 
        {
            case 0:
            {
                switcher = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
                [switcher addTarget:self action:@selector(switchAction:)
                   forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = switcher;
                switcher.tag = indexPath.row;


                //cell.textLabel.text=@"switcher";

            } 
                break;
            case 1: 
            {


                slide = [[[UISlider alloc] initWithFrame:CGRectMake(0,0, 150, 15)] autorelease];
                [slide addTarget:self action:@selector(slideact:)
                forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = slide;
                // slide.tag = indexPath.row;                
            } 
                break;

}
}
}

这是我转换行动的方法:

- (void)switchAction:(UISwitch*)sender
{
    NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]);

    if(sender.on)
    {

[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switcher"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    else
    {
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switcher"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
}

-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switcher"]isEqualToString:@"on"])
 {

switcher.on=YES;

    }
else
{
switcher.on=NO;
}

}
最佳回答

Well sameer then you need to save the state of switch. For this you can even use the database or NSUserDefaults. Using NSUserDefaults would be pretty much better.

- (void)switchAction:(UISwitch*)sender
{
    DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));

    if(sender.on)
    {

[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    else
    {
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
}

然后,你认为, 预留方法如下:

-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switch"]isEqualToString:@"on"]) {

switch.Ison=YES;

    }
else
{
switch.Ison=NO;
}
}
问题回答

你们应当有价值来挽救转换控制地位可能取决于用户选择。 当你提出这一看法时,你应当检查这一价值。

Boolean status=YES;   // You should read this value from User Preference for example it represent the last status set to the switch control.
[switchView setOn:status animated:NO];

Refer to the following question for reading and writing to User Preferences: iphone-reading-and-saving-preferences

You simply use the method to set the switch on and off like this

- (void)switchAction:(UISwitch*)sender
{
    DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));

    if(sender.on)
    {

    }
    else
    {

    }
}

如果你想要在违约时确定,那么,你就在申请书中要求你在申请中说明此事,以防在执行情况档案中消失,或在执行后将文件放在顶上。

switch.on=YES;




相关问题
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风格的解决办法,只是一个简单的袖珍流程......

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

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

热门标签