如何使用swift从故事板中的一个viewcontroller移动到另一个viewcontroller

时间:2018-04-27 13:38:39

标签: ios swift xcode storyboard

我试图在点击按钮时从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 enter image description here enter image description here 这是按下注册新帐户按钮时出现的错误。

   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

1 个答案:

答案 0 :(得分:0)

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

 performSegue(withIdentifier: String, sender: self)

你试过这个吗?