首先,我尝试了stackoverflow中给出的几乎所有解决方案,但我没有成功实现全局变量,我什至做了一步一步的教程,但我仍然得到同样的错误。
继承人的问题:
我有一个带有多个视图的程序,我想在所有视图上使用变量,所以我使用了全局变量,
我有:
全局变量.h
#import <UIKit/UIKit.h>
extern NSArray *truckBoxes;
extern NSArray *farmerlist;
extern NSString *farmerCardNumber;
extern NSString *fName;
@interface GlobalVars : NSObject
{
}
@end
全球变量.m
#import "GlobalVars.h"
NSArray *farmerlist;
NSArray *truckBoxes;
NSString *farmerCardNumber;
NSString *fName;
@implementation GlobalVars
{
}
@end
我在类中包含文件(连接到视图控制器):在我添加的 .h 文件下#import "GlobalVars.h
我可以从整个应用程序访问farmerlist
和,但无法访问或。truckBoxes
fName
farmerCardNumber
我可以从同一个视图写入farmerCardNumber
和读取,但如果我稍后尝试从另一个视图访问它,我会得到
2013-10-26 11:11:45.612 C3Cms[10427:70b] Branch : 11558
2013-10-26 11:11:59.459 C3Cms[10427:70b] -[__NSArrayM length]: unrecognized selector sent to instance 0x8b7fbc0
2013-10-26 11:11:59.463 C3Cms[10427:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x8b7fbc0'
*** First throw call stack:
(
0 CoreFoundation 0x02a3b5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014d58b6 objc_exception_throw + 44
2 CoreFoundation 0x02ad8903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x02a2b90b ___forwarding___ + 1019
4 CoreFoundation 0x02a2b4ee _CF_forwarding_prep_0 + 14
5 Foundation 0x00f0db2d -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 39
6 Foundation 0x00f0e79a -[NSConcreteMutableAttributedString initWithString:attributes:] + 293
7 UIKit 0x001e8116 -[UILabel _setText:] + 97
8 UIKit 0x001e82d4 -[UILabel setText:] + 40
9 C3Cms 0x00001c1d -[printForm viewDidLoad] + 77
10 UIKit 0x0014e9a8 -[UIViewController loadViewIfRequired] + 696
11 UIKit 0x0014ec44 -[UIViewController view] + 35
12 UIKit 0x00168a72 -[UINavigationController _startCustomTransition:] + 778
13 UIKit 0x00175757 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
14 UIKit 0x00176349 -[UINavigationController __viewWillLayoutSubviews] + 57
15 UIKit 0x002af39d -[UILayoutContainerView layoutSubviews] + 213
16 UIKit 0x000a5dd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
17 libobjc.A.dylib 0x014e781f -[NSObject performSelector:withObject:] + 70
18 QuartzCore 0x030d772a -[CALayer layoutSublayers] + 148
19 QuartzCore 0x030cb514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
20 QuartzCore 0x030cb380 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
21 QuartzCore 0x03033156 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
22 QuartzCore 0x030344e1 _ZN2CA11Transaction6commitEv + 393
23 QuartzCore 0x03034bb4 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
24 CoreFoundation 0x02a0353e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
25 CoreFoundation 0x02a0348f __CFRunLoopDoObservers + 399
26 CoreFoundation 0x029e13b4 __CFRunLoopRun + 1076
27 CoreFoundation 0x029e0b33 CFRunLoopRunSpecific + 467
28 CoreFoundation 0x029e094b CFRunLoopRunInMode + 123
29 GraphicsServices 0x029979d7 GSEventRunModal + 192
30 GraphicsServices 0x029977fe GSEventRun + 104
31 UIKit 0x0003b94b UIApplicationMain + 1225
32 C3Cms 0x00002882 main + 130
33 libdyld.dylib 0x019bc70d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException