[英]Keep button pressed in IOS when App reopens
我正在制作一個Iphone應用程序,我讓用戶單擊一個按鈕,它會更改獲取的結果控制器中的排序。 但是,在我關閉應用程序后,它又變回了原來的排序方式。 我只是想知道在重新打開應用程序時是否仍然需要按下該按鈕。
- (IBAction)btnValue:(id)sender {
self.model.frc_Work.delegate = self;
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Value" ascending:YES];
self.model.frc_Work.fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
}
是。 只需使用NSUserDefaults
來存儲信息:
// Set this to the correct value when the button is pressed
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ButtonPressedKey"];
然后,當您布置按鈕時:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ButtonPressedKey"]){
// Do whatever you're currently doing to keep the button pressed during runtime.
}
NSUserDefaults
基本上是一個NSDictionary
,可在應用程序啟動后持續存在。 它僅存儲屬性列表值( NSNumber, NSString, NSData, NSArray, NSDictionary, and NSDate
)以及諸如BOOL
的基元。
這是一個示例項目,使用UISwitch
進行了演示: https : //github.com/MaxGabriel/ButtonPersistence
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.