2011-05-25 52 views
1

當我在iphone模擬器中輸入一個值並按下保存按鈕它只保留值,直到模擬器運行的值的問題。當我重新啓動模擬器並按下加載按鈕時,它會顯示me.i.e輸入的較早值,但它不能保留新值並只保留舊值。保存iphone

我正在使用以下循環來保存文件和加載文件。

-(IBAction) save{ 

    NSUserDefaults *sinner=[NSUserDefaults standardUserDefaults]; 

    [sinner setObject:serverIP.text forKey:@"load"]; 


    NSUserDefaults *king=[NSUserDefaults standardUserDefaults]; 

    [king setObject:noc.text forKey:@"save"]; 

    } 


-(IBAction) load { 

    NSUserDefaults *sinner=[NSUserDefaults standardUserDefaults]; 

    NSString *tempstring =[sinner stringForKey:@"load"]; 


     serverIP.text = [NSString stringWithFormat:tempstring]; 



    NSUserDefaults *king=[NSUserDefaults standardUserDefaults]; 

    NSString *tempstring1 =[king stringForKey:@"save"]; 


    noc.text = [NSString stringWithFormat:tempstring1]; 




    } 

回答

1
// Your code  
NSUserDefaults *king= [NSUserDefaults standardUserDefaults]; 
[king setObject:bookmarks forKey:@"Bookmarks"];  
// saving it all 
[king synchronize]; 
+0

@thanks哥們它worked.canü提供socket編程索姆幫助。 – 2011-05-25 06:35:57

+0

看到這個:[Socket programming](http://stackoverflow.com/questions/1083017/iphone-socket-program)。我對此沒有太多的想法:) – Nitish 2011-05-25 06:40:55

+0

@ nitish感謝buddy – 2011-05-25 06:42:02

1
-(IBAction) save{ 
NSUserDefaults *sinner=[NSUserDefaults standardUserDefaults]; 
[sinner setObject:serverIP.text forKey:@"load"]; 
[sinner setObject:noc.text forKey:@"save"]; 
[sinner synchronize]; 
} 

這應該保存的內容。你不需要兩個單獨的userdefaults。加載它們,你可以嘗試

-(IBAction) load { 
NSUserDefaults *sinner=[NSUserDefaults standardUserDefaults]; 
NSString *tempstring =[sinner stringForKey:@"load"]; 
serverIP.text = tempstring; 
NSString *tempstring1 =[sinner stringForKey:@"save"]; 
noc.text = tempstring1; 
} 

希望這有助於