ios - 通过 userDefaults 在两个 View Controller 之间共享数据

标签 ios swift xcode uiswitch

我正在创建控制两个 View Controller 的 UISwitch,并且我需要在它们之间共享 UISwitch 条件的数据。我尝试使用用户默认值,但收到 SIGABRT 错误。我需要修复什么才能使其正常工作?

// Its view with UISwitch

@IBAction func SwitchThemeColor(_ sender: UISwitch) {
    if SwitchTheme.isOn || traitCollection.userInterfaceStyle == .dark {
        view.backgroundColor = UIColor.black
        UserDefaults.standard.set(true, forKey: "switchState")
        UserDefaults.standard.synchronize()
    }


// And that is the view where I want to transfer UISwitch condition

if  UserDefaults.standard.bool(forKey: "switchState") == true {
            view.backgroundColor = UIColor.black
        }

预期:数据传输和切换控制两个 View 的背景

实际:SIGABRT 错误并且不起作用

我还在输出中写了 bt 并得到了这个:

* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00007fff513782c6 libsystem_kernel.dylib`__pthread_kill + 10
    frame #1: 0x00007fff5141fbf1 libsystem_pthread.dylib`pthread_kill + 284
    frame #2: 0x00007fff51308a5c libsystem_c.dylib`abort + 120
    frame #3: 0x00007fff4f22e7f8 libc++abi.dylib`abort_message + 231
    frame #4: 0x00007fff4f22e9c7 libc++abi.dylib`demangling_terminate_handler() + 262
    frame #5: 0x00007fff503b5d7c libobjc.A.dylib`_objc_terminate() + 96
    frame #6: 0x00007fff4f23be97 libc++abi.dylib`std::__terminate(void (*)()) + 8
    frame #7: 0x00007fff4f23be39 libc++abi.dylib`std::terminate() + 41
    frame #8: 0x00007fff503b5d1c libobjc.A.dylib`objc_terminate + 9
    frame #9: 0x000000010ff1cd78 libdispatch.dylib`_dispatch_client_callout + 28
    frame #10: 0x000000010ff1fcd5 libdispatch.dylib`_dispatch_block_invoke_direct + 300
    frame #11: 0x00007fff3633108a FrontBoardServices`__FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
    frame #12: 0x00007fff36330d78 FrontBoardServices`-[FBSSerialQueue _queue_performNextIfPossible] + 441
    frame #13: 0x00007fff36331287 FrontBoardServices`-[FBSSerialQueue _performNextFromRunLoopSource] + 22
    frame #14: 0x00007fff23afbac1 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    frame #15: 0x00007fff23afb9ec CoreFoundation`__CFRunLoopDoSource0 + 76
    frame #16: 0x00007fff23afb21c CoreFoundation`__CFRunLoopDoSources0 + 268
    frame #17: 0x00007fff23af5ecf CoreFoundation`__CFRunLoopRun + 1263
    frame #18: 0x00007fff23af56b6 CoreFoundation`CFRunLoopRunSpecific + 438
    frame #19: 0x00007fff3815cbb0 GraphicsServices`GSEventRunModal + 65
    frame #20: 0x00007fff47162a67 UIKitCore`UIApplicationMain + 1621
  * frame #21: 0x000000010fa7bdab TabbedAppTest`main at AppDelegate.swift:12:7
    frame #22: 0x00007fff5123bcf5 libdyld.dylib`start + 1
    frame #23: 0x00007fff5123bcf5 libdyld.dylib`start + 1

最佳答案

问题出在其他地方 - 这段代码应该可以工作。

您在评论中提到,您收到错误消息:Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<TabbedAppTest.SecondViewController 0x7f8d84516c60> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key BarItem.

看起来你在storyboard/xib文件中有一些旧的 key 导出BarItem在你身上SecondViewController类(class)。可能您以编程方式从代码中删除了outlet,但storyboard/xib中的outlet仍然存在。

关于ios - 通过 userDefaults 在两个 View Controller 之间共享数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58134164/

相关文章:

xcode - 移动到不同的 Mac 时,Git 子模块文件在 XCode 中显示为红色

iphone - CoreData 关系延迟加载?

iphone - 以编程方式将内容添加到音乐库

Swift 3 UIImageView 顺序

ios - 添加为另一个 View 的 subview 时未调用 touchesBegan 方法

swift - 在 Swift 中如何测试索引是否超出范围?

ios - SKLabelNode 倒置可能吗?

c# - IOS项目如何生成IPA

ios - 字符串存储.plist不保存/加载并且不填充UITableView

ios - 获取 CollectionView IOS 中的列数