使用segue在视图控制器之间传递变量 [英] Passing variables between view controllers using segue
问题描述
我一直试图在视图控制器之间传递变量,没有太多的运气。我开始回到我遇到的问题,但现在有问题,只是得到segue行动。
无论我执行segue与标识符方法还是它的操作从按钮我用于执行计算我得到以下错误在调试器。
任何帮助将不胜感激。
2014-02-14 13:59:58.795 BatteryCalculator [6944:70b]找不到可执行文件CFBundle 0x8dbd110< /Applications/Xcode.app/Contents/Developer/Platforms /iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (未加载)
2014-02-14 14:00:00.869 BatteryCalculator [6944:70b] - [DetectorSpacing _setViewDelegate:]:无法识别的选择器发送到实例0x8dd6ff0
2014-02-14 14:00: 00.872 BatteryCalculator [6944:70b] ***由于未捕获异常NSInvalidArgumentException终止应用程序,原因:' - [DetectorSpacing _setViewDelegate:]:无法识别的选择器发送到实例0x8dd6ff0'
***第一次调用堆栈:
(
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基础0x010ed253 - [NSObject(NSKeyValueCoding)setValue:forKey:] + 267
9基础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]
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:以NSException类型的未捕获异常结束
(lldb)
目前我不想传递任何东西,我暂时把这段代码,同时试图找到问题。所有我在做的是调用segue与标识符,并得到崩溃。我也试过从第一个视图控制器上的按钮调用segue,但同样的错误。
当我使用代码调用segue时:
[self performSegueWithIdentifier:@ResultSeguesender:sender];
这是我如何在segue之间传递managedObjectContext
在你的类中,你将传递数据使用prepareForSegue调用。 (假设这个类有一个变量 _managedObjectContext ,可以传递给segue类)
p>
.h文件:
@property(weak,nonatomic)NSManagedObjectContext * managedObjectContext;
.m文件:
@synthesize managedObjectContext
调用@synthesize将会产生以下结果: / p>
- 一个名为 _managedObjectContext 的局部变量
- getManagedObjectContext
//传递managedObjectContext
- (void)prepareForSegue:(UIStoryboardSegue * )segue sender:(id)sender
{
//如果目标VC能够采用setManagedObjectContext方法,当前的objectContext将被传递。
if([segue.destinationViewController responsesToSelector:@selector(setManagedObjectContext :)]){
[segue.destinationViewController performSelector:@selector(setManagedObjectContext :)
withObject:_managedObjectContext];
} else {
NSLog(@Segue to controller [%@] that does not support passing managedObjectContext,[segue destinationViewController]);
}
}
然后在我的类中接收数据I do:
在.h文件中有
@property(weak,nonatomic)NSManagedObjectContext * managedObjectContext;
,在.m文件中有:
@synthesize managedObjectContext;
这样做(使用syntehsiation)是一个setManagedObjectContext和getManagedObjectContext调用。当我要检查以确保destinationController将响应这个方法,如果是这样的数据设置。
清除?
I've been trying to pass variables between view controllers without much luck. I started to work backwards from the problem I was encountering but am now having problems just getting the segue to action.
Whether I perform the segue with identifier method or have it actioning from the button I am using to perform a calculation I get the below error in the debugger.
Any help would be appreciated.
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)
At the moment I'm not trying to pass anything, I took that code out temporarily while trying to locate the problem. All I'm doing is calling a segue with identifier and getting the crash. I've also tried just calling the segue from a button on the first view controller but the same error.
When calling the segue with code I was using:
[self performSegueWithIdentifier:@"ResultSegue" sender:sender];
Here is how I pass managedObjectContext between segues
In your class where you will pass the data use prepareForSegue call. (The assumption is this class has a variable called _managedObjectContext which can be passed along to the segue class)
Class to Segue From:
.h file:
@property (weak, nonatomic) NSManagedObjectContext *managedObjectContext;
.m file:
@synthesize managedObjectContext
The call to @synthesize will make the following:
- a local variable called _managedObjectContext
- a method to getManagedObjectContext
- a method to setManagedObjectContext
Additionally add the following method to your class
// 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]);
}
}
Then in my "class" to receive the data I do: in the .h file i have
@property (weak, nonatomic) NSManagedObjectContext *managedObjectContext;
and in the .m file i have:
@synthesize managedObjectContext;
What this does (with syntehsiation) is make a setManagedObjectContext and getManagedObjectContext call. Upon being about to segue I check to make sure the destinationController will "respond to" this method, if so the data gets set.
clear?
这篇关于使用segue在视图控制器之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!