通过userDeafults在两个视图控制器之间共享数据

问题描述 投票:0回答:1

我正在创建控制两个视图控制器的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
        }

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

实际: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
ios swift xcode uiswitch
1个回答
0
投票

问题在其他地方-该代码应该起作用。

您在评论中提到,您收到错误消息: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.

似乎您的故事板/ xib文件中已有BarItem类中键SecondViewController的某些旧插座。可能您已以编程方式从代码中删除了插座,但故事板/ xib中的插座仍然存在。

© www.soinside.com 2019 - 2024. All rights reserved.