如何使用 swift 在情节提要中从一个视图控制器移动到另一个视图控制器 [重复]

Posted

技术标签:

【中文标题】如何使用 swift 在情节提要中从一个视图控制器移动到另一个视图控制器 [重复]【英文标题】:How to move from one viewcontroller to another in a storyboard using swift [duplicate] 【发布时间】:2018-04-27 13:38:39 【问题描述】:

我正在尝试在单击按钮时从 SignInViewController 移动到 RegisterUserViewController。这里是代码

   @IBAction func RegisterNewAccountButtonTapped(_ sender: Any) 
         print("Register account button tapped")
        //RegisterUserViewController


       // let storyboard = UIStoryboard(name: "SignInViewController", bundle: nil)
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "RegisterUserViewController") as! RegisterUserViewController
        self.present(vc, animated: true)
    

在这张图片中是班级和故事板ID 这是按下注册新帐户按钮时出现的错误。

   2018-04-27 06:09:24.720143-0700 UserRegistrationExample[10193:1788611] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UserRegistrationExample.RegisterUserViewController 0x7f84b4f171b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key repeatPasswordTextField.'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x000000010db1826b __exceptionPreprocess + 171
        1   libobjc.A.dylib                     0x000000010a1fbf41 objc_exception_throw + 48
        2   CoreFoundation                      0x000000010db181b9 -[NSException raise] + 9
        3   Foundation                          0x0000000109c20883 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
        4   UIKit                               0x000000010a95dd56 -[UIViewController setValue:forKey:] + 87
        5   UIKit                               0x000000010ac3ac94 -[UIRuntimeOutletConnection connect] + 109
        6   CoreFoundation                      0x000000010dabb61d -[NSArray makeObjectsPerformSelector:] + 317
        7   UIKit                               0x000000010ac3964a -[UINib instantiateWithOwner:options:] + 1856
        8   UIKit                               0x000000010a964d49 -[UIViewController _loadViewFromNibNamed:bundle:] + 383
        9   UIKit                               0x000000010a965652 -[UIViewController loadView] + 177
        10  UIKit                               0x000000010a965983 -[UIViewController loadViewIfRequired] + 195
        11  UIKit                               0x000000010a9661e0 -[UIViewController view] + 27
        12  UIKit                               0x000000010b3bd39d -[_UIFullscreenPresentationController _setPresentedViewController:] + 89
        13  UIKit                               0x000000010a936a8f -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 133
        14  UIKit                               0x000000010a979338 -[UIViewController _presentViewController:withAnimationController:completion:] + 3808
        15  UIKit                               0x000000010a97c14a __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 134
        16  UIKit                               0x000000010a97c5ea -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 532
        17  UIKit                               0x000000010a97c086 -[UIViewController presentViewController:animated:completion:] + 181
        18  UserRegistrationExample             0x00000001098e0ae5 _T023UserRegistrationExample20SignInViewControllerC30RegisterNewAccountButtonTappedyypF + 1077
        19  UserRegistrationExample             0x00000001098e0c58 _T023UserRegistrationExample20SignInViewControllerC30RegisterNewAccountButtonTappedyypFTo + 72
        20  UIKit                               0x000000010a7cd631 -[UIApplication sendAction:to:from:forEvent:] + 83
        21  UIKit                               0x000000010a942000 -[UIControl sendAction:to:forEvent:] + 67
        22  UIKit                               0x000000010a94231d -[UIControl _sendActionsForEvents:withEvent:] + 450
        23  UIKit                               0x000000010a94124a -[UIControl touchesEnded:withEvent:] + 618
        24  UIKit                               0x000000010a840bf1 -[UIWindow _sendTouchesForEvent:] + 2807
        25  UIKit                               0x000000010a842314 -[UIWindow sendEvent:] + 4124
        26  UIKit                               0x000000010a7e82da -[UIApplication sendEvent:] + 352
        27  UIKit                               0x000000010b0f6f18 __dispatchPreprocessedEventFromEventQueue + 2809
        28  UIKit                               0x000000010b0f9a7f __handleEventQueueInternal + 5957
        29  CoreFoundation                      0x000000010dabb351 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
        30  CoreFoundation                      0x000000010db5ad71 __CFRunLoopDoSource0 + 81
        31  CoreFoundation                      0x000000010da9fcb9 __CFRunLoopDoSources0 + 185
        32  CoreFoundation                      0x000000010da9f29f __CFRunLoopRun + 1279
        33  CoreFoundation                      0x000000010da9eb29 CFRunLoopRunSpecific + 409
        34  GraphicsServices                    0x00000001101af9c6 GSEventRunModal + 62
        35  UIKit                               0x000000010a7cb9a4 UIApplicationMain + 159
        36  UserRegistrationExample             0x00000001098deee7 main + 55
        37  libdyld.dylib                       0x000000010ec3a621 start + 1
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException

我访问过以下链接,但没有一个能解决问题

https://coderwall.com/p/cjuzng/swift-instantiate-a-view-controller-using-its-storyboard-name-in-xcode

instantiateViewControllerWithIdentifier - Storyboard id set but still not working

Present View Controller in Storyboard with a Navigation Controller - Swift

Swift 3, Xcode 8 Instantiate View Controller is not working

Instantiate and Present a viewController in Swift

如何消除此错误。

您可以从此链接下载代码 .https://drive.google.com/open?id=1yUaKeI6ZQphN7CsoiaeF2p4TmW_B5-9u

【问题讨论】:

您链接的问题没有回答崩溃问题。这就是你应该寻找的问题 = ***.com/questions/3088059/… 我猜你之前有一个名为 repeatPasswordTextField 的 UITextField IBOutlet,你要么删除它,要么重命名它。但是您没有重做/撤消 Storyboard/Interface Builder 中的链接 该错误可能意味着RegisterUserViewController 有一个名为repeatPasswordTextField 的出口,该出口设置在情节提要中,但未设置在代码中。 【参考方案1】:

尝试 CTRL 并从您的按钮拖动到下一个 ViewController。然后选择“显示”。这就是你设置segue的方式。然后点击 segue 并给他一个标识符。然后你需要设置

 performSegue(withIdentifier: String, sender: self)

你试过了吗?

【讨论】:

以上是关于如何使用 swift 在情节提要中从一个视图控制器移动到另一个视图控制器 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何通过单击按钮在 Swift UI 中从一个视图导航到另一个视图

如何在情节提要 XCode6 (Swift) 中将所有视图控制器设置为风景

以编程方式设置 rootViewController 不起作用,仅在 Xcode 11 中从情节提要中选择初始视图控制器

如何在情节提要中从一个视图转到另一个视图

在 XCTest 测试中从情节提要加载的视图控制器中清空 UIView 快照(间歇性)

如何在 ios swift 中以编程方式将情节提要视图控制器加载到标签栏控制器中?