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

时间:2014-07-11 08:12:21

标签: 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
    }    

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

任何想法我做错了什么?感谢

1 个答案:

答案 0 :(得分:1)

当你的车祸说:     reason: '-[_TtC7Flipper19FirstViewController textField:]: unrecognized selector sent to instance 并且您没有提到单击按钮时发生崩溃。

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

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