使用segue在视图控制器之间传递变量

时间:2014-02-14 14:13:44

标签: crash segue unrecognized-selector

我一直试图在视图控制器之间传递变量而没有太多运气。我开始从我遇到的问题向后工作,但现在我遇到的问题就是让这个问题付诸行动。

无论是使用标识符方法执行segue还是从我用于执行计算的按钮执行操作,我都会在调试器中收到以下错误。

任何帮助都将不胜感激。

2014-02-14 13:59:58.795 BatteryCalculator[6944:70b] Cannot find executable for CFBundle 0x8dbd110 </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded)
2014-02-14 14:00:00.869 BatteryCalculator[6944:70b] -[DetectorSpacing _setViewDelegate:]: unrecognized selector sent to instance 0x8dd6ff0
2014-02-14 14:00:00.872 BatteryCalculator[6944:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetectorSpacing _setViewDelegate:]: unrecognized selector sent to instance 0x8dd6ff0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0173d5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014c08b6 objc_exception_throw + 44
    2   CoreFoundation                      0x017da903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0172d90b ___forwarding___ + 1019
    4   CoreFoundation                      0x0172d4ee _CF_forwarding_prep_0 + 14
    5   UIKit                               0x00347ecc +[UIViewController setViewController:forView:] + 40
    6   UIKit                               0x00342921 -[UIViewController setView:] + 511
    7   Foundation                          0x010edd28 _NSSetUsingKeyValueSetter + 133
    8   Foundation                          0x010ed253 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
    9   Foundation                          0x0114f70a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 412
    10  UIKit                               0x004d0a15 -[UIRuntimeOutletConnection connect] + 106
    11  libobjc.A.dylib                     0x014d27d2 -[NSObject performSelector:] + 62
    12  CoreFoundation                      0x01738b6a -[NSArray makeObjectsPerformSelector:] + 314
    13  UIKit                               0x004cf56e -[UINib instantiateWithOwner:options:] + 1417
    14  UIKit                               0x00341605 -[UIViewController _loadViewFromNibNamed:bundle:] + 280
    15  UIKit                               0x00341dad -[UIViewController loadView] + 302
    16  UIKit                               0x003420ae -[UIViewController loadViewIfRequired] + 78
    17  UIKit                               0x003425b4 -[UIViewController view] + 35
    18  UIKit                               0x0035c3e2 -[UINavigationController _startCustomTransition:] + 778
    19  UIKit                               0x003690c7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
    20  UIKit                               0x00369cb9 -[UINavigationController __viewWillLayoutSubviews] + 57
    21  UIKit                               0x004a3181 -[UILayoutContainerView layoutSubviews] + 213
    22  UIKit                               0x0ecdc56f -[UILayoutContainerViewAccessibility(SafeCategory) layoutSubviews] + 50
    23  UIKit                               0x00299267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    24  libobjc.A.dylib                     0x014d281f -[NSObject performSelector:withObject:] + 70
    25  QuartzCore                          0x03b4b2ea -[CALayer layoutSublayers] + 148
    26  QuartzCore                          0x03b3f0d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    27  QuartzCore                          0x03b3ef40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    28  QuartzCore                          0x03aa6ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    29  QuartzCore                          0x03aa7e71 _ZN2CA11Transaction6commitEv + 393
    30  QuartzCore                          0x03aa8544 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    31  CoreFoundation                      0x017054ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    32  CoreFoundation                      0x0170541f __CFRunLoopDoObservers + 399
    33  CoreFoundation                      0x016e3344 __CFRunLoopRun + 1076
    34  CoreFoundation                      0x016e2ac3 CFRunLoopRunSpecific + 467
    35  CoreFoundation                      0x016e28db CFRunLoopRunInMode + 123
    36  GraphicsServices                    0x036e29e2 GSEventRunModal + 192
    37  GraphicsServices                    0x036e2809 GSEventRun + 104
    38  UIKit                               0x0022ed3b UIApplicationMain + 1225
    39  BatteryCalculator                   0x0000582d main + 141
    40  libdyld.dylib                       0x01d7b70d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

目前我并没有尝试传递任何内容,我在尝试找到问题时暂时取出了该代码。我正在做的就是调用带有标识符的segue并获得崩溃。我也试过从第一个视图控制器上的按钮调用segue但是同样的错误。

使用我正在使用的代码调用segue时:

[self performSegueWithIdentifier:@“ResultSegue”sender:sender];

1 个答案:

答案 0 :(得分:0)

以下是我在segues

之间传递managedObjectContext的方法

在您将要传递数据的班级中,使用prepareForSegue调用。 (假设这个类有一个名为 _managedObjectContext 的变量,它可以传递给segue类)

来自Segue的课程:

.h文件:

@property (weak, nonatomic) NSManagedObjectContext *managedObjectContext;

.m文件:

@synthesize managedObjectContext

对@synthesize的调用将产生以下结果:

  • 一个名为 _managedObjectContext
  • 的局部变量
  • getManagedObjectContext
  • 的方法
  • setManagedObjectContext
  • 的方法

另外将以下方法添加到您的班级

// Pass on managedObjectContext
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

// If the destination VC is able to take the setManagedObjectContext method the current objectContext will be passed along.
if ([segue.destinationViewController respondsToSelector:@selector(setManagedObjectContext:)]) {
    [segue.destinationViewController performSelector:@selector(setManagedObjectContext:)
                                          withObject:_managedObjectContext];
} else {

    NSLog(@"Segue to controller [%@] that does not support passing managedObjectContext", [segue destinationViewController]);
 }
}

然后在我的“课堂”中接收我做的数据: 在.h文件中我有

@property (weak, nonatomic) NSManagedObjectContext *managedObjectContext;

并在.m文件中我有:

@synthesize managedObjectContext;

这样做(使用syntehsiation)是一个setManagedObjectContext和getManagedObjectContext调用。在准备segue我检查以确保destinationController将“响应”此方法,如果是这样,数据设置。

清楚?