NSInternalInconsistencyException when using KVO - ios

I'm trying to use a KVO example I got from an iPhone tutorial book, but get this exception
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<UINavigationController: 0x139630>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: dataUpdated
Observed object: <FilterDetailsController: 0x1b9930>
Change: {
kind = 1;
}
Context: 0x0'
Call stack at first throw:
(
0 CoreFoundation 0x320d3987 __exceptionPreprocess + 114
1 libobjc.A.dylib 0x3271849d objc_exception_throw + 24
2 CoreFoundation 0x320d37c9 +[NSException raise:format:arguments:] + 68
3 CoreFoundation 0x320d3803 +[NSException raise:format:] + 34
4 Foundation 0x35c316e9 -[NSObject(NSKeyValueObserving) observeValueForKeyPath:ofObject:change:context:] + 60
5 Foundation 0x35bd4a3d NSKeyValueNotifyObserver + 216
6 Foundation 0x35bd46e5 NSKeyValueDidChange + 236
7 Foundation 0x35bcc3f5 -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 76
8 Foundation 0x35c30d87 _NSSetObjectValueAndNotify + 98
9 Lucid Dreaming App 0x000108e3 -[FilterDetailsController adjustFilterDetails:] + 138
10 CoreFoundation 0x3207afed -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
11 UIKit 0x323b3ea5 -[UIApplication sendAction:to:from:forEvent:] + 84
12 UIKit 0x323b3e45 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
13 UIKit 0x323b3e17 -[UIControl sendAction:to:forEvent:] + 38
14 UIKit 0x323b3b69 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
15 UIKit 0x323b43c7 -[UIControl touchesEnded:withEvent:] + 342
16 UIKit 0x323a9d4d -[UIWindow _sendTouchesForEvent:] + 368
17 UIKit 0x323a96c7 -[UIWindow sendEvent:] + 262
18 UIKit 0x3239499f -[UIApplication sendEvent:] + 298
19 UIKit 0x323942df _UIApplicationHandleEvent + 5090
20 GraphicsServices 0x35472f03 PurpleEventCallback + 666
21 CoreFoundation 0x320686ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
22 CoreFoundation 0x320686c3 __CFRunLoopDoSource1 + 166
23 CoreFoundation 0x3205af7d __CFRunLoopRun + 520
24 CoreFoundation 0x3205ac87 CFRunLoopRunSpecific + 230
25 CoreFoundation 0x3205ab8f CFRunLoopRunInMode + 58
26 GraphicsServices 0x354724ab GSEventRunModal + 114
27 GraphicsServices 0x35472557 GSEventRun + 62
28 UIKit 0x323c7d21 -[UIApplication _run] + 412
29 UIKit ...
I start by
[advancedController addObserver:self.navigationController forKeyPath:#"dataUpdated" options:0 context:nil];
within self.navigation controller, I have the observation method defined:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(#"2 Observed change for: %#",keyPath);
// if (context == <#context#>) {
// <#code to be executed upon observing keypath#>
// } else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
// }
}
within advancedController I have a NSNumber property
#property(nonatomic,retain)NSNumber* dataUpdated;
When I hit a button, I expect the observer to fire.
- (IBAction)adjustFilterDetails:(id)sender {
self.dataUpdated = [NSNumber numberWithInt:[self.dataUpdated intValue]+1];
}
Do I need to implement some protocol or explicitly state that I will update the value? I read that NSKeyValueObserving is an "informal" protocol. Thank you for your help!
I have figured out the answer to my own question. I had the KVO protocol method defined within a ViewController, however, I have accidentally registered the Navigation controller for the View controller to observe the value. The correct object is found by getting the view controller like this:
[advancedController addObserver:(RootViewController*)self.navigationController. topViewController forKeyPath:#"dataUpdated" options:0 context:nil];

Your problem is the call to super. You shouldn't pass this method to super for properties that you observed yourself. You commented out the code that was there to help you with this.
A possible correct observation would be (note the context):
[advancedController addObserver:self.navigationController forKeyPath:#"dataUpdated" options:0 context:self];
And then the correct observer would be:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == self) {
NSLog(#"2 Observed change for: %#",keyPath);
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
Dave Dribin provides another approach to using context correctly, along with the backstory of why it's necessary, in Proper Key-Value Observer Usage.

Related

I can’t understand a crash.

I got a crash from crashlytics. Probably it connected with weakSelf.
Crashed: com.apple.main-thread
EXC_BREAKPOINT 0x000000000defe
0 libobjc.A.dylib 0x224aea44 _objc_trap()
1 libobjc.A.dylib 0x224aeaa9 _objc_inform + 70
2 libobjc.A.dylib 0x224c8413 weak_register_no_lock + 210
3 libobjc.A.dylib 0x224c8993 objc_initWeak + 130
4 GPS_______appLite 0xd77b1 -[ModuleView observeValueForKeyPath:ofObject:change:context:] (ModuleView.m:232)
5 GPS_______appLite 0xd9287 -[ModuleSpeedometerView observeValueForKeyPath:ofObject:change:context:] (ModuleSpeedometerView.m:198)
6 Foundation 0x2349aa91 NSKVONotify + 52
7 Foundation 0x23479bef NSKeyValueNotifyObserver + 286
8 Foundation 0x23479847 NSKeyValueDidChange + 346
9 Foundation 0x23466599 -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 96
10 GPS_______appLite 0x12c393 -[GPSTracker private_updateCurrentStatisticsWithLocation:] (GPSTracker.mm:1887)
11 GPS_______appLite 0x12c1b9 -[GPSTracker private_addLocationToHistory:] (GPSTracker.mm:1870)
12 GPS_______appLite 0x12c6af -[GPSTracker private_processNewLocation:fromManager:] (GPSTracker.mm:1938)
13 GPS_______appLite 0x12c88b -[GPSTracker locationManager:didUpdateLocations:] (GPSTracker.mm:1953)
14 CoreLocation 0x28c5c493 (null) + 25830
15 CoreLocation 0x28c5765b (null) + 5806
16 CoreLocation 0x28c50725 (null) + 988
17 CoreFoundation 0x22cd5d91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x22cd584d __CFRunLoopDoBlocks + 216
19 CoreFoundation 0x22cd41bf __CFRunLoopRun + 1806
20 CoreFoundation 0x22c232e9 CFRunLoopRunSpecific + 520
21 CoreFoundation 0x22c230d5 CFRunLoopRunInMode + 108
22 GraphicsServices 0x24213ac9 GSEventRunModal + 160
23 UIKit 0x272e80b9 UIApplicationMain + 144
24 GPS_______appLite 0x10adc7 main (main.m:15)
25 libdispatch.dylib 0x228cb873 (Missing)
and code
//ModuleView class
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([object isKindOfClass:[GPSTracker class]])
{
__weak GPSTracker *tracker = (GPSTracker *)object;
__weak __typeof__(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
if ([keyPath isEqualToString:NSStringFromSelector(#selector(trackerMode))])
{
[weakSelf trackerStateChanged];
}
else if ([keyPath isEqualToString:NSStringFromSelector(#selector(currentStatistics))])
{
id oldValue = [change objectForKey:NSKeyValueChangeOldKey];
if (!oldValue
|| [oldValue isKindOfClass:[NSNull class]])
{
// If statistics instance has been initialized
if (!weakSelf.shouldIgnoreUpdates)
{
[tracker.currentStatistics.time addUniqueObserver:weakSelf
forKeyPath:NSStringFromSelector(#selector(elapsed))
options:NSKeyValueObservingOptionNew];
}
}
[weakSelf trackerUpdate];
}
});
}
else if ([object isKindOfClass:[GPSTrackerTimeStatistics class]])
{
__weak __typeof__(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf timerUpdate];
});
}
}
for released version you can see crashes and line of code,
in xCode : Window-> Organizer-> Crashes
and select app and version from left panels
try "Exception Breakpoint"
Exception Breakpoint automatically add breakpoint to line has cause crash, and you can see the problem
to add Exception Breakpoint:
in xCode :
top menu -> Debug -> Breakpoints -> Create Exception Breakpoint...
and run again

-[XBMessage messageHash]: unrecognized selector sent to instance

I've integrated following library from Cocoapods, You may find the code and its sample code at https://github.com/EugeneNguyen/XBChatModule
pod 'XBChatModule'
This library is used to integrate XMPP Chat in Xcode project.
I also added suggested code from its ReadMe file. For your reference I am pasting it below
AppDelegate.m
[[XBChatModule sharedInstance] setUsername:#"admin"];
[[XBChatModule sharedInstance] setPassword:#"admin"];
[[XBChatModule sharedInstance] setHost:#"sflashcard.com"];
[[XBChatModule sharedInstance] connect];
[[XBChatModule sharedInstance] setAvatarFormat:#"http://dev.sflashcard.com/images/mantis_logo.png?test=%#"];
[[XBChatModule sharedInstance] setAvatarPlaceHolder:[UIImage imageNamed:#"girl_9"]];
The View controller is inherited from XBMessageViewController
ViewController.m
- (void)viewDidLoad
{
self.jidStr = #"binh.nx#sflashcard.com";
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadData) name:#"XBChatModuleNewAvatar" object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)reloadData
{
[self.collectionView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
The code works fine until I send a message. Whenever I press send button, it crashes the app by throwing following error in console
2015-09-01 17:20:41.742 Test[16064:251944] -[XBMessage messageHash]: unrecognized selector sent to instance 0x7ffc91cc4180
2015-09-01 17:20:41.800 Test[16064:251944] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XBMessage messageHash]: unrecognized selector sent to instance 0x7ffc91cc4180'
*** First throw call stack:
(
0 CoreFoundation 0x000000011260aa75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001122a3bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000112611d1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001125697ef ___forwarding___ + 495
4 CoreFoundation 0x0000000112569578 _CF_forwarding_prep_0 + 120
5 Test 0x000000010ebaf7ed -[JSQMessagesCollectionViewFlowLayout messageBubbleSizeForItemAtIndexPath:] + 301
6 Test 0x000000010ebb02a7 -[JSQMessagesCollectionViewFlowLayout sizeForItemAtIndexPath:] + 71
7 Test 0x000000010ebc5c93 -[JSQMessagesViewController collectionView:layout:sizeForItemAtIndexPath:] + 131
8 UIKit 0x00000001116369be -[UICollectionViewFlowLayout _getSizingInfos] + 988
9 UIKit 0x0000000111637839 -[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 526
10 UIKit 0x00000001116332b7 -[UICollectionViewFlowLayout collectionViewContentSize] + 66
11 Test 0x000000010ebc2d85 -[JSQMessagesViewController scrollToBottomAnimated:] + 325
12 Test 0x000000010ebc2c34 -[JSQMessagesViewController finishReceivingMessageAnimated:] + 388
13 Test 0x000000010ebc2aa0 -[JSQMessagesViewController finishReceivingMessage] + 48
14 Test 0x000000010ebfaac3 -[XBMessageViewController loadDataToTable] + 1507
15 Test 0x000000010ebf9be9 -[XBMessageViewController viewDidLoad] + 73
16 Test 0x000000010ead0760 -[ViewController viewDidLoad] + 96
17 UIKit 0x0000000111104580 -[UIViewController loadViewIfRequired] + 738
18 UIKit 0x000000011110477e -[UIViewController view] + 27
19 UIKit 0x0000000111023509 -[UIWindow addRootViewControllerViewIfPossible] + 58
20 UIKit 0x00000001110238a1 -[UIWindow _setHidden:forced:] + 247
21 UIKit 0x000000011102ff8c -[UIWindow makeKeyAndVisible] + 42
22 UIKit 0x0000000110fda0c2 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
23 UIKit 0x0000000110fdce3e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
24 UIKit 0x0000000110fdbd35 -[UIApplication workspaceDidEndTransaction:] + 179
25 FrontBoardServices 0x0000000115aab243 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
26 CoreFoundation 0x000000011253fc7c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
27 CoreFoundation 0x00000001125359c5 __CFRunLoopDoBlocks + 341
28 CoreFoundation 0x0000000112535183 __CFRunLoopRun + 851
29 CoreFoundation 0x0000000112534bc6 CFRunLoopRunSpecific + 470
30 UIKit 0x0000000110fdb7a2 -[UIApplication _run] + 413
31 UIKit 0x0000000110fde580 UIApplicationMain + 1282
32 Test 0x000000010ead06d3 main + 115
33 libdyld.dylib 0x0000000112b3c145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I understand it says, that I haven't implemented the function messageHash in Class XBMessage , but the same code works fine in the sample code that I download from Github. Kindly help me to solve this error.
The library that XBChatModule is based on has since been updated. If you look at the history of the protocol that XBMessage conforms to: https://github.com/jessesquires/JSQMessagesViewController/commits/develop/JSQMessagesViewController/Model/JSQMessageData.h you can see that the method messageHash has been made required since the XBChatModule was written.
As such, I recommend contacting the developers of XBChatModule and get them to update their code or, alternatively, implement your own messageHash method.

iPhone- Error With Spritekit Game

I'm making a app using Apple's Spritekit framework. My app has multiple view controllers and everything works fine. However, I tried to make a scene that I would load in one of my viewcontrollers, but that did not work. I get no errors at all: it just does not work. Everything works except when I try to load my scene, everything crashes. Also, I was thinking, could my problem be that I have already added a view and put labels and buttons, etc... on that view before I load my scene? To elaborate, could the problem be that I have already added uiimages, etc... to the view using interface builder before I loaded my scene programmatically? Any help would be great. Here is my code for the view controller where I call my scene:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
SKView * skView = (SKView *)self.view;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
And here is my code for the actual scene:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
SKSpriteNode *mainTurrentSprite = [SKSpriteNode spriteNodeWithImageNamed:#"mainTurrent.png"];
mainTurrentSprite.position = CGPointMake(self.view.center.x, 0);
[self addChild:mainTurrentSprite];
}
return self;
}
And here is my crash/error log :
2014-07-22 14:32:34.783 Doodle Defense[6713:60b] -[UIView presentScene:]: unrecognized selector sent to instance 0x9a32630
2014-07-22 14:32:34.786 Doodle Defense[6713:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView presentScene:]: unrecognized selector sent to instance 0x9a32630'
*** First throw call stack:
(
0 CoreFoundation 0x019231e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0168e8e5 objc_exception_throw + 44
2 CoreFoundation 0x019c0243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0191350b ___forwarding___ + 1019
4 CoreFoundation 0x019130ee _CF_forwarding_prep_0 + 14
5 Doodle Defense 0x000020b1 -[GameViewController viewDidLoad] + 337
6 UIKit 0x0034b33d -[UIViewController loadViewIfRequired] + 696
7 UIKit 0x0034b5d9 -[UIViewController view] + 35
8 UIKit 0x0035af89 -[UIViewController shouldAutorotate] + 36
9 UIKit 0x0035b2d1 -[UIViewController _preferredInterfaceOrientationForPresentationInWindow:fromInterfaceOrientation:] + 297
10 UIKit 0x005f93d5 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:] + 2330
11 UIKit 0x003575d5 -[UIViewController presentViewController:withTransition:completion:] + 6538
12 UIKit 0x00357aef -[UIViewController presentViewController:animated:completion:] + 130
13 UIKit 0x00357b2f -[UIViewController presentModalViewController:animated:] + 56
14 UIKit 0x007a1e00 -[UIStoryboardModalSegue perform] + 271
15 UIKit 0x00790f0c -[UIStoryboardSegueTemplate _perform:] + 174
16 UIKit 0x00790f87 -[UIStoryboardSegueTemplate perform:] + 115
17 libobjc.A.dylib 0x016a0880 -[NSObject performSelector:withObject:withObject:] + 77
18 UIKit 0x0022e3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
19 UIKit 0x0022e345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
20 UIKit 0x0032fbd1 -[UIControl sendAction:to:forEvent:] + 66
21 UIKit 0x0032ffc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
22 UIKit 0x0032f243 -[UIControl touchesEnded:withEvent:] + 641
23 UIKit 0x0026dddd -[UIWindow _sendTouchesForEvent:] + 852
24 UIKit 0x0026e9d1 -[UIWindow sendEvent:] + 1117
25 UIKit 0x002405f2 -[UIApplication sendEvent:] + 242
26 UIKit 0x0022a353 _UIApplicationHandleEventQueue + 11455
27 CoreFoundation 0x018ac77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
28 CoreFoundation 0x018ac10b __CFRunLoopDoSources0 + 235
29 CoreFoundation 0x018c91ae __CFRunLoopRun + 910
30 CoreFoundation 0x018c89d3 CFRunLoopRunSpecific + 467
31 CoreFoundation 0x018c87eb CFRunLoopRunInMode + 123
32 GraphicsServices 0x039035ee GSEventRunModal + 192
33 GraphicsServices 0x0390342b GSEventRun + 104
34 UIKit 0x0022cf9b UIApplicationMain + 1225
35 Doodle Defense 0x00002add main + 141
36 libdyld.dylib 0x01f56701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

SIGABRT error - segmented control

i have a segmented control which, depending on what is selected, is meant to allocate a value to a NS string, as follows:
-(IBAction)driverKnownIndexChanged{
switch (self.driverKnown.selectedSegmentIndex) {
case 0:
driverKnownResponse = #"Yes";
break;
case 1:
driverKnownResponse = #"No";
break;
default:
break;
}
}
however, it is throwing a SIGABRT at the following line:
#import "RoadSafetyAppAppDelegate.h"
int main(int argc, char * argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([RoadSafetyAppAppDelegate class]));
}
}
and here is the console output:
2014-01-30 13:32:16.403 Road Safety App V2[3873:70b] -[DobInAHoonViewController driverKnown:]: unrecognized selector sent to instance 0xcd23440
2014-01-30 13:32:16.409 Road Safety App V2[3873:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DobInAHoonViewController driverKnown:]: unrecognized selector sent to instance 0xcd23440'
*** First throw call stack:
(
0 CoreFoundation 0x01d8f7e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01b0f8e5 objc_exception_throw + 44
2 CoreFoundation 0x01e2c843 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01d7fb0b ___forwarding___ + 1019
4 CoreFoundation 0x01d7f6ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x01b2182b -[NSObject performSelector:withObject:] + 70
6 UIKit 0x007d8309 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x007d8295 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x008d90a1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x008d9496 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x008d90d6 -[UIControl sendActionsForControlEvents:] + 48
11 UIKit 0x00947572 -[UISegmentedControl _setSelectedSegmentIndex:notify:animate:] + 598
12 UIKit 0x009498c3 -[UISegmentedControl touchesEnded:withEvent:] + 175
13 UIKit 0x00b6ccc3 _UIGestureRecognizerUpdate + 7166
14 UIKit 0x008177ca -[UIWindow _sendGesturesForEvent:] + 1291
15 UIKit 0x008186e1 -[UIWindow sendEvent:] + 1021
16 UIKit 0x007ea542 -[UIApplication sendEvent:] + 242
17 UIKit 0x007d42f3 _UIApplicationHandleEventQueue + 11455
18 CoreFoundation 0x01d18d7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
19 CoreFoundation 0x01d1870b __CFRunLoopDoSources0 + 235
20 CoreFoundation 0x01d357ae __CFRunLoopRun + 910
21 CoreFoundation 0x01d34fd3 CFRunLoopRunSpecific + 467
22 CoreFoundation 0x01d34deb CFRunLoopRunInMode + 123
23 GraphicsServices 0x0344f4ed GSEventRunModal + 192
24 GraphicsServices 0x0344f32a GSEventRun + 104
25 UIKit 0x007d6eeb UIApplicationMain + 1225
26 Road Safety App V2 0x00002f9d main + 141
27 libdyld.dylib 0x0308470d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
and Ideas as to why i am having this problem? and a solution would also be appreciated.
This line switch (self.driverKnown.selectedSegmentIndex) { is accessing a property named driverKnown but that driver is unknown. The stack trace and exception are telling you that driverKnown is an undeclared or unknock getter method for the property.
The exception calls out DobInAHoonViewController which in that line of code would be the self. Is driverKnown suppose to be the name of your UISegmentedControl? Can we see the creation of said UISegmentedControl?
your method doesn't know selectedSegmentIndex . because you not value of segment value changed
UISegmentControl *mySegmentedControl = [[UISegmentControl alloc]init];
[mySegmentedControl addTarget:self action:#selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
- (IBAction)segmentValueChanged:(id)sender {
UISegmentedControl *driverKnown = (UISegmentedControl *)sender;
switch (driverKnown.selectedSegmentIndex) {
case 0:
driverKnownResponse = #"Yes";
break;
case 1:
driverKnownResponse = #"No";
break;
default:
break;
}
}

Crash attempting to set delegate of textfield

I have a lot of textfields and what I am trying to make them do is dismiss when the return key is used. In order for this to work you need to set the delegate of each textfield to self, like this [textfield setDelegate: self];. I have over 50 textfields in my project and in order to make it so they all dismiss I have to copy that line of code for each textfield. In the example below I used a for loop to try to shrink this down, but my project crashes and gives me this error when I try. Can someone tell me what im doing wrong and how i can fix this?
//.h
#interface InsertScheduleCGPS : UIViewController <UITextFieldDelegate>{
NSArray *Dayh;
IBOutlet UITextField *Day11;
}
#property(nonatomic, assign) id<UITextFieldDelegate> delegate;
#property (nonatomic,strong) NSArray *Dayh;
.
//.m
- (void)viewDidLoad
{
[super viewDidLoad];
Dayh = [NSArray arrayWithObjects:#"Day11", nil];
NSLog(#"euf");
for(int i=0; i<[self.Dayh count]; i++) {
NSLog(#"dd%#",[self.Dayh objectAtIndex:i]);
[[self.Dayh objectAtIndex:i] setDelegate: self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)Day11 {
[[self view] endEditing:YES];
return NO;
}
ERROR:
2014-01-18 19:15:26.712 Swepple[64912:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString setDelegate:]: unrecognized selector sent to instance 0x144ec'
*** First throw call stack:
(
0 CoreFoundation 0x0183b5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x015be8b6 objc_exception_throw + 44
2 CoreFoundation 0x018d8903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0182b90b ___forwarding___ + 1019
4 CoreFoundation 0x0182b4ee _CF_forwarding_prep_0 + 14
5 Swepple 0x0000bbd6 -[InsertScheduleCGPS viewDidLoad] + 4262
6 UIKit 0x00440318 -[UIViewController loadViewIfRequired] + 696
7 UIKit 0x004405b4 -[UIViewController view] + 35
8 UIKit 0x0044f361 -[UIViewController viewControllerForRotation] + 63
9 UIKit 0x00446f00 -[UIViewController _visibleView] + 84
10 UIKit 0x006d511a -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:] + 5199
11 UIKit 0x0044c0fc -[UIViewController presentViewController:withTransition:completion:] + 6433
12 UIKit 0x0044c61f -[UIViewController presentViewController:animated:completion:] + 130
13 UIKit 0x0044c65f -[UIViewController presentModalViewController:animated:] + 56
14 UIKit 0x00870e16 -[UIStoryboardModalSegue perform] + 271
15 UIKit 0x0086107e -[UIStoryboardSegueTemplate _perform:] + 174
16 UIKit 0x00442280 -[UIViewController performSegueWithIdentifier:sender:] + 72
17 Swepple 0x000052d4 -[SecondViewController insert:] + 244
18 libobjc.A.dylib 0x015d0874 -[NSObject performSelector:withObject:withObject:] + 77
19 UIKit 0x0032e0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
20 UIKit 0x0032e04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
21 UIKit 0x004260c1 -[UIControl sendAction:to:forEvent:] + 66
22 UIKit 0x00426484 -[UIControl _sendActionsForEvents:withEvent:] + 577
23 UIKit 0x00425733 -[UIControl touchesEnded:withEvent:] + 641
24 UIKit 0x0036b51d -[UIWindow _sendTouchesForEvent:] + 852
25 UIKit 0x0036c184 -[UIWindow sendEvent:] + 1232
26 UIKit 0x0033fe86 -[UIApplication sendEvent:] + 242
27 UIKit 0x0032a18f _UIApplicationHandleEventQueue + 11421
28 CoreFoundation 0x017c483f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
29 CoreFoundation 0x017c41cb __CFRunLoopDoSources0 + 235
30 CoreFoundation 0x017e129e __CFRunLoopRun + 910
31 CoreFoundation 0x017e0ac3 CFRunLoopRunSpecific + 467
32 CoreFoundation 0x017e08db CFRunLoopRunInMode + 123
33 GraphicsServices 0x037e09e2 GSEventRunModal + 192
34 GraphicsServices 0x037e0809 GSEventRun + 104
35 UIKit 0x0032cd3b UIApplicationMain + 1225
36 Swepple 0x0000e04d main + 141
37 libdyld.dylib 0x01d7c70d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
You probably meant
Dayh = [NSArray arrayWithObjects:Day11, nil];
At present, Dayh is an array containing the string "Day11", not the text field.

Resources