2013-08-23 26 views
0

在viewDidLoad中UISwitch在IOS

{ 
    NSLog(@"Switch Value %d",delegate.switchvalue); 

    if(delegate.switchvalue==1) 
    { 
     [onoffswitch setOn:YES]; 
    } 
    else 
    { 
     [onoffswitch setOn:NO]; 
    } 

} 

在設計,

onoffswitch=[[UISwitch alloc]initWithFrame:(CGRectMake(200, 8, 50, 33))]; 
    [onoffswitch addTarget:self action: @selector(flip:) forControlEvents:UIControlEventValueChanged]; 
    // [onoffswitch setOn:YES]; 

在交換機的方法,

-(IBAction)flip:(id)sender 


{ 

    if([sender isOn]) 
    { 
     [onoffswitch setTag:1]; 
     NSLog(@"%d",[sender tag]); 
     delegate.switchvalue=[sender tag]; 
     NSLog(@"%d",delegate.switchvalue); 


     NSLog(@"ON"); 

     NSTimer *BirthTimer=[NSTimer scheduledTimerWithTimeInterval:86400.0 target:self selector:@selector(TimerTapped:) userInfo:nil repeats:YES]; 

     [BirthTimer fire]; 
    } 
    else 
    { 
     NSLog(@"OFF"); 

    } 
} 

我的問題是,我將開關模式ON.If我轉到另一個視圖,然後再次關閉模式。 任何想法請幫助我。

感謝您的幫助。

使用NSUserDefaults的

- (IBAction爲)翻轉:(ID)發送方

{

[onoffswitch setTag:1]; 
delegate.switchvalue=[sender tag]; 
NSLog(@"%d",delegate.switchvalue); 

if([sender isOn]) 
{ 

    NSLog(@"ON"); 
    isAlarm=1; 
    NSLog(@"%d",isAlarm); 

    NSUserDefaults *switchdefault=[NSUserDefaults standardUserDefaults]; 
    [[NSUserDefaults standardUserDefaults]synchronize]; 
    [switchdefault setInteger:delegate.switchvalue forKey:@"Switchcontrol"]; 
    [switchdefault setBool:YES forKey:@"SwitchBool"]; 
    [switchdefault synchronize]; 


    NSTimer *BirthTimer=[NSTimer scheduledTimerWithTimeInterval:86400.0 target:self selector:@selector(TimerTapped:) userInfo:nil repeats:YES]; 

    [BirthTimer fire]; 
} 
else 
{ 
    NSLog(@"OFF"); 
    isAlarm=0; 
    NSLog(@"%d",isAlarm); 


} 

}

在viewWillAppear中

NSUserDefaults的* sw​​itchdefault = [NSUserDefaults的standardUserDefaults ]。

if([switchdefault boolForKey:@"SwitchBool"]) 
{ 
    [onoffswitch setOn:YES animated:YES]; 
} 
else 
{ 
    [onoffswitch setOn:YES animated:YES]; 
} 

在viewDidLoad中AnotherView的

NSUserDefaults *switchdefault=[NSUserDefaults standardUserDefaults]; 

if ([switchdefault boolForKey:@"SwitchBool"]) 
{ 
    delegate.switchvalue=1; 
} 
[switchdefault synchronize]; 

回答

0

如果你想開關被設置爲每一個出現在您的瀏覽時間delegate.switchValue,嘗試從viewDidLoad中這個代碼遷移到viewDidAppear:

-(void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated]; 
    if(delegate.switchvalue==1) 
    { 
     [onoffswitch setOn:YES]; 
    } 
    else 
    { 
     [onoffswitch setOn:NO]; 
    } 
} 
+0

我試過了,但再次關閉模式 – rajesh

+0

@rajesh - 好的,這裏有什麼問題?你的NSLog爲switchValue打印出正確的值嗎? – katzenhut

+0

我用斷點,但它不能去if語句 – rajesh

2

使用NSSUserDefault來存儲您的開關按鈕的狀態,並再次將其添加到您的視圖檢索它從相同的...

存儲1一旦你切換它ON和0爲OFF模式。

[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"values"]; 

,並拿回來這樣,

NSInteger mode = [[NSUserDefaults standardUserDefaults] integerForKey:@"values"]; 

注意:我建議你在ViewWillAppear/ViewDidAppear添加您的開關按鈕。

+0

我在表視圖中附加了開關按鈕。 – rajesh

+0

你把它添加到單元格作爲contentview相當......對吧? –

+0

yes.correct.It是任務。 – rajesh