在 VStack 内的 HStack 内的 SwiftUI 文本字段中,在 macOS 中生成运行时错误

问题描述

我正在使用 Xcode 12.3 和 MacOS 11.1。

Textfield 嵌入在 HStack 中,而这个文本字段嵌入在 vstack 中。

struct ContentView: View {
@State var username: String = ""
 var body: some View {
    vstack {
        HStack() {
            Text("Enter Username:")
            TextField("",text: $username)
        }
        Text("Your username: \(username)")
    }
 }

}

代码作为 iOS 应用程序运行没有任何问题。 但是,将代码编译为 MacOS-App 会导致运行时错误

2021-01-23 23:18:29.835048+0100 TestMacSwiftUI[6349:280606] [General] 错误:设置 <_ttc7swiftuip33_c58093e7172b0a541a997680e343d0d516_systemtextfield:> 作为窗口的第一响应者 ,但它在不同的窗口中((空))!当视图被释放时,这最终会崩溃。 第一响应者将设置为 nil。

0   AppKit                              0x00007fff22c5a499 -[NSWindow _validateFirstResponder:] + 449
1   AppKit                              0x00007fff22c5a294 -[NSWindow _setFirstResponder:] + 31
2   AppKit                              0x00007fff22d7e72b -[NSWindow _realMakeFirstResponder:] + 338
3   SwiftUI                             0x00007fff42d8627b $s7SwiftUI11FocusBridgeC04moveC02toyAA0C4ItemV_tF + 155
4   SwiftUI                             0x00007fff42d86fad $s7SwiftUI11FocusBridgeC27hostDidBecomeFirstResponder33_74086170859BF7C5D78394D4DEE7E0F9LLyyF + 861
5   SwiftUI                             0x00007fff42d85b97 $s7SwiftUI11FocusBridgeC23firstResponderDidChange2to04rootF0ySo11NSResponderCSg_AA0F4Node_pSgtF + 583
6   SwiftUI                             0x00007fff42c7102b $s7SwiftUI13NSHostingViewC12observeValue10forKeyPath2of6change7contextySSSg_ypSgSDySo05NSKeyf6ChangeH0aypGSgSvSgtFyycfU_ + 299
7   SwiftUI                             0x00007fff42632c8c $sIeg_ytIegr_TR + 12
8   SwiftUI                             0x00007fff42c79631 $sIeg_ytIegr_TRTA + 17
9   SwiftUI                             0x00007fff42b61374 $s7SwiftUI6UpdateO3endyyFZ + 436
10  SwiftUI                             0x00007fff42c70e85 $s7SwiftUI13NSHostingViewC12observeValue10forKeyPath2of6change7contextySSSg_ypSgSDySo05NSKeyf6ChangeH0aypGSgSvSgtF + 805
11  SwiftUI                             0x00007fff42c71159 $s7SwiftUI13NSHostingViewC12observeValue10forKeyPath2of6change7contextySSSg_ypSgSDySo05NSKeyf6ChangeH0aypGSgSvSgtFTo + 249
12  Foundation                          0x00007fff21188ab8 NSkeyvalueNotifyObserver + 327
13  Foundation                          0x00007fff2124fe45 NSkeyvalueDidChange + 431
14  Foundation                          0x00007fff212f0539 NSkeyvalueDidChangeWithPerThreadPendingNotifications + 146
15  AppKit                              0x00007fff22d7e72b -[NSWindow _realMakeFirstResponder:] + 338
16  AppKit                              0x00007fff22d93adf -[NSWindow _selectFirstkeyview] + 758
17  AppKit                              0x00007fff22d9310f -[NSWindow _setUpFirstResponder] + 189
18  AppKit                              0x00007fff22d90577 -[NSWindow _doWindowWillBeVisibleAsSheet:] + 153
19  AppKit                              0x00007fff22d8e4bd -[NSWindow _reallyDoOrderWindowAboveOrBelow:relativeto:findKey:forCounter:force:isModal:] + 1317
20  AppKit                              0x00007fff22d8dc42 -[NSWindow _reallyDoOrderWindow:relativeto:findKey:forCounter:force:isModal:] + 135
21  AppKit                              0x00007fff22d8cc52 -[NSWindow _doOrderWindow:relativeto:findKey:forCounter:force:isModal:] + 289
22  AppKit                              0x00007fff22d8cad0 -[NSWindow orderWindow:relativeto:] + 155
23  AppKit                              0x00007fff22d806f1 -[NSWindow makeKeyAndOrderFront:] + 60
24  TestMacSwiftUI                      0x000000010e887d21 $s14TestMacSwiftUI11AppDelegateC29applicationDidFinishLaunchingyy10Foundation12NotificationVF + 3169
25  TestMacSwiftUI                      0x000000010e887f62 $s14TestMacSwiftUI11AppDelegateC29applicationDidFinishLaunchingyy10Foundation12NotificationVFTo + 146
26  CoreFoundation                      0x00007fff2041dfec __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
27  CoreFoundation                      0x00007fff204b989b ___CFXRegistrationPost_block_invoke + 49
28  CoreFoundation                      0x00007fff204b980f _CFXRegistrationPost + 454
29  CoreFoundation                      0x00007fff203eebde _CFXNotificationPost + 723
30  Foundation                          0x00007fff2115dabe -[NSNotificationCenter postNotificationName:object:userInfo:] + 59
31  AppKit                              0x00007fff22c4cf6d -[NSApplication _postDidFinishNotification] + 305
32  AppKit                              0x00007fff22c4ccbb -[NSApplication _sendFinishLaunchingNotification] + 208
33  AppKit                              0x00007fff22c49eb2 -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:] + 541
34  AppKit                              0x00007fff22c49b07 -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] + 665
35  Foundation                          0x00007fff21189056 -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 308
36  Foundation                          0x00007fff21188ec6 _NSAppleEventManagerGenericHandler + 80
37  AE                                  0x00007fff26201ed9 _AppleEventsCheckInAppWithBlock + 15850
38  AE                                  0x00007fff262015f4 _AppleEventsCheckInAppWithBlock + 13573
39  AE                                  0x00007fff261fa260 aeProcessAppleEvent + 452
40  HIToolBox                           0x00007fff286bd612 AEProcessAppleEvent + 54
41  AppKit                              0x00007fff22c44276 _DPSNextEvent + 2048
42  AppKit                              0x00007fff22c425af -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1366
43  AppKit                              0x00007fff22c34b0a -[NSApplication run] + 586
44  AppKit                              0x00007fff22c08df2 NSApplicationMain + 816
45  TestMacSwiftUI                      0x000000010e8882d4 $sSo21NSApplicationDelegateP6AppKitE4mainyyFZ + 36
46  TestMacSwiftUI                      0x000000010e88829e $s14TestMacSwiftUI11AppDelegateC5$mainyyFZ + 46
47  TestMacSwiftUI                      0x000000010e888309 main + 41
48  libdyld.dylib                       0x00007fff2034b621 start + 1
49  ???                                 0x0000000000000003 0x0 + 3

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)