ios - Swift - 将字符串从 VC1 传递到 VC2 - 崩溃

标签 ios uiviewcontroller swift

我有一个 swift 应用程序,我只是想将一个字符串从一个 ViewController 传递到另一个。在 segue 之后,我看到第二个 viewController 在第一个之上,数据被传递,我得到以下异常:

2014-07-11 10:58:17.078 Flipper[8967:377257] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtC7Flipper19FirstViewController textField:]: unrecognized selector sent to instance 0x7fba2351eda0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010c805055 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010e528a1c objc_exception_throw + 45
    2   CoreFoundation                      0x000000010c80bf1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010c764dbc ___forwarding___ + 988
    4   CoreFoundation                      0x000000010c764958 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010d2ed196 -[UIApplication sendAction:to:from:forEvent:] + 75
    6   UIKit                               0x000000010d3f1c80 -[UIControl _sendActionsForEvents:withEvent:] + 467
    7   UIKit                               0x000000010d9fbb86 -[UITextField _resignFirstResponder] + 256
    8   UIKit                               0x000000010d45cbff -[UIResponder resignFirstResponder] + 117
    9   UIKit                               0x000000010d9fb98f -[UITextField resignFirstResponder] + 114
    10  UIKit                               0x000000010d355722 -[UIView(Hierarchy) _removeFirstResponderFromSubtree] + 161
    11  UIKit                               0x000000010d355bdd __UIViewWillBeRemovedFromSuperview + 76
    12  UIKit                               0x000000010d35594c -[UIView(Hierarchy) removeFromSuperview] + 91
    13  UIKit                               0x000000010d3ef12c __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke478 + 785
    14  UIKit                               0x000000010d3eb7bd -[UIPresentationController transitionDidFinish:] + 87
    15  UIKit                               0x000000010d3edd8e __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 133
    16  UIKit                               0x000000010da1d123 -[_UIViewControllerTransitionContext completeTransition:] + 110
    17  UIKit                               0x000000010d3e8ea3 -[UITransitionView _didCompleteTransition:] + 1120
    18  UIKit                               0x000000010d34542a -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 209
    19  UIKit                               0x000000010d345760 -[UIViewAnimationState animationDidStop:finished:] + 76
    20  QuartzCore                          0x000000010d176f9e _ZN2CA5Layer23run_animation_callbacksEPv + 308
    21  libdispatch.dylib                   0x000000010ea64d64 _dispatch_client_callout + 8
    22  libdispatch.dylib                   0x000000010ea50f82 _dispatch_main_queue_callback_4CF + 941
    23  CoreFoundation                      0x000000010c76dae9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    24  CoreFoundation                      0x000000010c73046b __CFRunLoopRun + 2043
    25  CoreFoundation                      0x000000010c72fa06 CFRunLoopRunSpecific + 470
    26  GraphicsServices                    0x00000001103b9abf GSEventRunModal + 161
    27  UIKit                               0x000000010d2ebcf8 UIApplicationMain + 1282
    28  Flipper                             0x000000010c37737d top_level_code + 77
    29  Flipper                             0x000000010c3773ba main + 42
    30  libdyld.dylib                       0x000000010ea99145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我的 VC1 代码是:

class FirstViewController: UIViewController {

    @IBOutlet var textField: UITextField
    var myVariable:String!

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if (segue.identifier == "P1ToP2") {
            var svc = segue!.destinationViewController as SecondViewController;
            myVariable = textField.text
            svc.myVariable = myVariable
        }
    }
}

我的 VC2 代码是:

class SecondViewController: UIViewController {

    @IBOutlet var labelTwo: UILabel
    var myVariable:String!

    override func viewDidLoad() {
        super.viewDidLoad()  
        labelTwo.text = myVariable
    }    

我正在使用一个 Storyboard,其中 VC1 有一个 Button,它调用一个名为 P1ToP2 的 Show(push)segue

知道我做错了什么吗?谢谢

最佳答案

正如您的崩溃所说: 原因:'-[_TtC7Flipper19FirstViewController textField:]:发送到实例的无法识别的选择器 而且您没有提到单击按钮时会发生崩溃。

我的猜测是尝试删除 textField:UITextField 之间的空格。

我想问题就在那里,而不是传递字符串。

关于ios - Swift - 将字符串从 VC1 传递到 VC2 - 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24693029/

相关文章:

Swift 期待声明但已经声明

ios - textView 高度等于没有 viewDidLayoutSubviews 的内容大小

ios - Facebook iOS SDK 导致崩溃

ios - 如何更改 UITextView 的溢出字符颜色?

ios - 将 UITableViewController 插入其他 UIView

iphone - 基于全局变量的变化 View Controller

ios - Google Analytics 集成不适用于 iOS 应用程序

iOS 7 导航栏在 View Controller 之间转换时出现视觉故障

macos - 适用于 Mac OS X 10.11 的 CNContact

swift - 如何调整显示在警报 Controller 上的图像的大小?