Can't add a tableview to a new tabbed project - xcode

I'm getting an error that I can't decipher when I try to add a tableview to a new tabbed project.
If I add the tableview to my storyboard, then build the app - it's fine. However as soon as I ctrl+drag the "delegate" and "dataSource" to that yellow circle with a square box icon to link it to my controller, the build fails. Why does this happen?
Exact repro steps:
Create a new "Tabbed View" project in Xcode.
In storyboard, I drag a "Table View" onto the top storyboard.
I "ctrl click" on the "Table View", then drag "dataSource" and "delegate" to the yellow circle + white box icon at the top of the storyboard.
I open FirstViewController.swift and I try to add UITableViewDelegate, UITableViewDataSource to the class. However neither of these show up in autocomplete as if the controller doesn't think they exist. If I command click on one, I get "symbol not found".
FirstViewController.swift
//
// FirstViewController.swift
// To Do List
//
// Created by Donald Pinkus on 12/14/14.
// Copyright (c) 2014 Don Pinkus. All rights reserved.
//
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Build error
2014-12-14 00:27:04.278 To Do List[11149:566768] -[To_Do_List.FirstViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7faafae29ba0
2014-12-14 00:27:04.285 To Do List[11149:566768] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[To_Do_List.FirstViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7faafae29ba0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000105422f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000106f66bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010542a04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010538227c ___forwarding___ + 988
4 CoreFoundation 0x0000000105381e18 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000105f26212 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2353
6 UIKit 0x0000000105f2a098 -[UITableViewRowData numberOfRows] + 97
7 UIKit 0x0000000105d928fc -[UITableView noteNumberOfRowsChanged] + 133
8 UIKit 0x0000000105d9203d -[UITableView reloadData] + 1316
9 UIKit 0x0000000105d9ad86 -[UITableView layoutSubviews] + 31
10 UIKit 0x0000000105d27973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
11 QuartzCore 0x0000000109c25de8 -[CALayer layoutSublayers] + 150
12 QuartzCore 0x0000000109c1aa0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
13 UIKit 0x0000000105d1b847 -[UIView(Hierarchy) layoutBelowIfNeeded] + 611
14 UIKit 0x0000000105e0ff06 -[UITabBarController _layoutViewController:] + 500
15 UIKit 0x0000000105e10014 -[UITabBarController _wrapperViewForViewController:] + 252
16 UIKit 0x0000000105e16e92 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 378
17 UIKit 0x0000000105e13069 -[UITabBarController _setSelectedViewController:] + 311
18 UIKit 0x0000000105d1f5ce +[UIView(Animation) performWithoutAnimation:] + 65
19 UIKit 0x0000000105e10106 -[UITabBarController _selectDefaultViewControllerIfNecessaryWithAppearanceTransitions:] + 221
20 UIKit 0x0000000105e10dfe -[UITabBarController viewWillAppear:] + 121
21 UIKit 0x0000000105dd7821 -[UIViewController _setViewAppearState:isAnimating:] + 487
22 UIKit 0x0000000105d1a136 -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] + 564
23 UIKit 0x0000000105d24aaa -[UIView(Internal) _addSubview:positioned:relativeTo:] + 419
24 UIKit 0x0000000105cf2e37 -[UIWindow addRootViewControllerViewIfPossible] + 456
25 UIKit 0x0000000105cf3041 -[UIWindow _setHidden:forced:] + 247
26 UIKit 0x0000000105cff72c -[UIWindow makeKeyAndVisible] + 42
27 UIKit 0x0000000105caa061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
28 UIKit 0x0000000105cacd2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
29 UIKit 0x0000000105cabbf2 -[UIApplication workspaceDidEndTransaction:] + 179
30 FrontBoardServices 0x0000000108af32a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
31 CoreFoundation 0x000000010535853c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
32 CoreFoundation 0x000000010534e285 __CFRunLoopDoBlocks + 341
33 CoreFoundation 0x000000010534e045 __CFRunLoopRun + 2389
34 CoreFoundation 0x000000010534d486 CFRunLoopRunSpecific + 470
35 UIKit 0x0000000105cab669 -[UIApplication _run] + 413
36 UIKit 0x0000000105cae420 UIApplicationMain + 1282
37 To Do List 0x00000001052403ce top_level_code + 78
38 To Do List 0x000000010524040a main + 42
39 libdyld.dylib 0x0000000107740145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

You are missing the numberOfRowsInSection method which is required.
In order to be a UITableViewDataSource you are required to implement numberOfRowsInSection and cellForRowAtIndexPath

Related

Xcode - Terminating with uncaught exception of type NSException when a button is pressed

I'm new to app development and I'm following Apple's tutorial to learn. I've looked through a lot of questions asked about this type of error but none of them are helping me. In the tutorial, I'm stuck on the "Add Buttons to the View" section where I'm trying to implement a string to be printed out on the console when a button is tapped/clicked on. Only I get an error when doing so.
My code for the button in Swift:
import UIKit
class StarRatingControl: UIView {
// MARK: Initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.cyanColor()
button.addTarget(self, action: "ratingButtonTapped", forControlEvents: .TouchDown)
addSubview(button)
}
override func intrinsicContentSize() -> CGSize {
return CGSize(width: 240, height: 44)
}
// MARK: Button Action
func ratingButtonTapped(button: UIButton) {
print("Button pressed!")
}
}
The error:
2015-09-16 12:43:17.409 FoodTracker[954:13341] -[FoodTracker.StarRatingControl ratingButtonTapped]: unrecognized selector sent to instance 0x7f939b7a43b0
2015-09-16 12:43:17.443 FoodTracker[954:13341] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FoodTracker.StarRatingControl ratingButtonTapped]: unrecognized selector sent to instance 0x7f939b7a43b0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001079f59b5 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001095f4deb objc_exception_throw + 48
2 CoreFoundation 0x00000001079fdfdd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010794b9fa ___forwarding___ + 970
4 CoreFoundation 0x000000010794b5a8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000108214522 -[UIApplication sendAction:to:from:forEvent:] + 92
6 UIKit 0x0000000108373c06 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x0000000108373eac -[UIControl _sendActionsForEvents:withEvent:] + 273
8 UIKit 0x0000000108372b1c -[UIControl touchesBegan:withEvent:] + 261
9 UIKit 0x000000010827bf50 -[UIWindow _sendTouchesForEvent:] + 308
10 UIKit 0x000000010827cd4d -[UIWindow sendEvent:] + 865
11 UIKit 0x00000001082312ae -[UIApplication sendEvent:] + 263
12 UIKit 0x000000010820d36c _UIApplicationHandleEventQueue + 6693
13 CoreFoundation 0x0000000107921b21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x0000000107917a4c __CFRunLoopDoSources0 + 556
15 CoreFoundation 0x0000000107916f03 __CFRunLoopRun + 867
16 CoreFoundation 0x0000000107916918 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x000000010befead2 GSEventRunModal + 161
18 UIKit 0x000000010821299e UIApplicationMain + 171
19 FoodTracker 0x00000001078118dd main + 109
20 libdyld.dylib 0x000000010a11392d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I understand that Xcode keeps old references to action/outlets that I've deleted so I tried right-clicking on the View Controller in the Interface Builder to find items with the exclamation point next to them but I couldn't find anything(only troubleshooting method I know of so far). I'm using Xcode beta 7.0. Any help is appreciated!
You're missing the ":" in the selector, try the following instead:
button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)
I hope this help you.
If you're using Swift 3, the line in question would have to be changed to:
button.addTarget(self, action: #selector(StarRatingControl.ratingButtonTapped), for: .touchDown)
I had the same problem,
Check the version of your Xcode.
#selector comes with Xcode 7.3
If you have an older version update and this will probably fix your errors.
Regards,
Driss
For reference, I've decided to have a look at the sample and got a similar issue.
I'm using XCode 8 + Swift 3 and these are my changes in order to get it working:
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped), for: .touchDown)
Note: considering the file name "RatingControl.swift".
I hope it help =D

How to update an NSTextView while user is scrolling (without crashing)

I'm using an NSTextView to display the result of a long search, where lines are added as they are found by a background thread using
[self performSelectorOnMainThread: #selector(addMatch:)
withObject:options waitUntilDone:TRUE];
As the update routine I have
-(void)addMatch:(NSDictionary*)options{
...
NSTextStorage* store = [textView textStorage];
[store beginEditing];
[store appendAttributedString:text];
...
[store endEditing];
}
This works fine, until the user scrolls through the matches as they are being updated, at which point there's an exception
-[NSLayoutManager _fillLayoutHoleForCharacterRange:desiredNumberOfLines:isSoft:] *** attempted layout while textStorage is editing. It is not valid to
cause the layoutManager to do layout while the textStorage is editing
(ie the textStorage has been sent a beginEditing message without a
matching endEditing.)
within a layout call:
0 CoreFoundation 0x00007fff92ea364c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8acd16de objc_exception_throw + 43
2 CoreFoundation 0x00007fff92ea34fd +[NSException raise:format:] + 205
3 UIFoundation 0x00007fff8fe4fbc1 -[NSLayoutManager(NSPrivate) _fillLayoutHoleForCharacterRange:desiredNumberOfLines:isSoft:] + 641
4 UIFoundation 0x00007fff8fe5970c _NSFastFillAllLayoutHolesForGlyphRange + 1493
5 UIFoundation 0x00007fff8fda8821 -[NSLayoutManager lineFragmentRectForGlyphAtIndex:effectiveRange:] + 39
6 AppKit 0x00007fff8ef3cb02 -[NSTextView _extendedGlyphRangeForRange:maxGlyphIndex:drawingToScreen:] + 478
7 AppKit 0x00007fff8ef3ba97 -[NSTextView drawRect:] + 1832
8 AppKit 0x00007fff8eed9a09 -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] + 1186
9 AppKit 0x00007fff8eed9458 __46-[NSView(NSLayerKitGlue) drawLayer:inContext:]_block_invoke + 218
10 AppKit 0x00007fff8eed91f1 -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] + 2407
11 AppKit 0x00007fff8eed8873 -[NSView(NSLayerKitGlue) drawLayer:inContext:] + 108
12 AppKit 0x00007fff8efaafd2 -[NSTextView drawLayer:inContext:] + 179
13 AppKit 0x00007fff8ef22f76 -[_NSBackingLayerContents drawLayer:inContext:] + 145
14 QuartzCore 0x00007fff9337c177 -[CALayer drawInContext:] + 119
15 AppKit 0x00007fff8ef22aae -[_NSTiledLayer drawTile:inContext:] + 625
16 AppKit 0x00007fff8ef227df -[_NSTiledLayerContents drawLayer:inContext:] + 169
17 QuartzCore 0x00007fff9337c177 -[CALayer drawInContext:] + 119
18 AppKit 0x00007fff8f6efd64 -[NSTileLayer drawInContext:] + 169
19 QuartzCore 0x00007fff9337b153 CABackingStoreUpdate_ + 3306
20 QuartzCore 0x00007fff9337a463 ___ZN2CA5Layer8display_Ev_block_invoke + 59
21 QuartzCore 0x00007fff9337a41f x_blame_allocations + 81
22 QuartzCore 0x00007fff93379f1c _ZN2CA5Layer8display_Ev + 1546
23 AppKit 0x00007fff8ef226ed -[NSTileLayer display] + 119
24 AppKit 0x00007fff8ef1ec34 -[_NSTiledLayerContents update:] + 5688
25 AppKit 0x00007fff8ef1d337 -[_NSTiledLayer display] + 375
26 QuartzCore 0x00007fff93379641 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 603
27 QuartzCore 0x00007fff93378d7d _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 35
28 QuartzCore 0x00007fff9337850e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
29 QuartzCore 0x00007fff93378164 _ZN2CA11Transaction6commitEv + 390
30 QuartzCore 0x00007fff93388f55 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 71
31 CoreFoundation 0x00007fff92dc0d87 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
32 CoreFoundation 0x00007fff92dc0ce0 __CFRunLoopDoObservers + 368
33 CoreFoundation 0x00007fff92db2f1a __CFRunLoopRun + 1178
34 CoreFoundation 0x00007fff92db2838 CFRunLoopRunSpecific + 296
35 UIFoundation 0x00007fff8fdfe744 -[NSHTMLReader _loadUsingWebKit] + 2097
36 UIFoundation 0x00007fff8fdffb55 -[NSHTMLReader attributedString] + 22
37 UIFoundation 0x00007fff8fe12cca _NSReadAttributedStringFromURLOrData + 10543
38 UIFoundation 0x00007fff8fe10306 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 115
What is wrong, given that everything is between beginEditing and endEditing?
From the stack trace (which is not complete), it looks like a run loop source is firing at an inopportune moment.
NSAttributedString uses WebKit to parse HTML. WebKit sometimes runs the run loop. For the general case, it may need to fetch resources from the network to render properly. Since that takes time, it runs the run loop to wait for the result and process other things at the same time.
One of the other run loop sources seems to be a Core Animation source to do the next step in some animation (scrolling the text view, presumably).
You didn't show all of the code between beginEditing and endEditing. I suspect you have constructed an NSAttributedString from HTML or data fetched from a URL in between those two places. That allows the Core Animation run loop source to fire. That asks the text view to draw, which asks its layout manager to lay out the text. This is occurring after beginEditing but before endEditing, which is the cause for the exception.
So, try reordering your code to construct all NSAttributedStrings before beginEditing.
And file a bug with Apple. In my opinion, when NSAttributeString uses WebKit to render HTML, it needs to make WebKit use a private run loop mode so no other sources can fire. They may prefer a different solution, but the bug is real.
As far as I can tell, there's no fix for this. An alternative that works is storing the matches as attributed strings in an array, and using an NSTableView to show the matches by setting the textField.attributedStringValue (calling reloadData every time you add a new match); something like this (where matchContent is an NSMutableArray):
-(void)addMatch:(NSDictionary*)options{
...
[matchContent addObject:text];
[resultTableView reloadData];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return matchContent.count;
}
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
NSTableCellView *result = [tableView makeViewWithIdentifier:#"MyView" owner:self];
result.textField.attributedStringValue = [matchContent objectAtIndex:row];
return result;
}
If the result is multi-line, you may also need to check the autoresizing mask of the cell/text field, and return the row height for the table view using the attributed string's boundingRectWithSize method.

Keyboard focus lost after inserting and editing new object, when NSArrayController „Auto Rearrange Content“ is enabled

I have a master-detail kind of application OS X 10.9 only:
An NSTableView on the left side of the window, the details as NSTextFields on the right side.
I have not set the tab order specifically, out of the box works just fine.
Except after inserting a new object:
after inserting a new object the object is selected in the NSTableView.
hit tab
the first NSTextField is selected
write something, hit tab
now instead of selecting the next NSTextField nothing is selected,
hit tab
the NSTableView is selected
hit tab
the first NSTextField is selected
What i want:
after inserting a new object, the first NSTextField of the detail part should be selected
hit tab
the next NSTextField of the detail part should be selected
…
UPDATE:
I found that the strange behaviour is triggered by enabling NSArrayController’s "Auto Rearrange Content“ in Interface Builder. If i disable it, i do not loose my focus anymore. See CoreData-bound NSTableView loses input focus when items change, but only if sorted.
BUT, of course, i still want my content to be automatically sortet — just without loosing my focus during that operation. Can this be done?
UPDATE2: i followed #KenThomases advice and recorded the stack trace at the moment, when my textfield looses keyboard focus:
2014-08-17 16:30:26.003 ResponderExperiment[712:303] (
0 ResponderExperiment 0x00000001000014dd -[WMWindow makeFirstResponder:] + 61
1 AppKit 0x00007fff8d25c778 -[NSTextView(NSPrivate) _giveUpFirstResponder:] + 257
2 AppKit 0x00007fff8d25c56c -[NSTextView(NSKeyBindingCommands) insertTab:] + 270
3 AppKit 0x00007fff8d22dc2f -[NSResponder doCommandBySelector:] + 71
4 AppKit 0x00007fff8d25b10a -[NSTextView doCommandBySelector:] + 196
5 AppKit 0x00007fff8d22d151 -[NSKeyBindingManager(NSKeyBindingManager_MultiClients) interpretEventAsCommand:forClient:] + 1392
6 AppKit 0x00007fff8d24c1c2 -[NSTextInputContext handleEvent:] + 845
7 AppKit 0x00007fff8d22b9dd -[NSView interpretKeyEvents:] + 180
8 AppKit 0x00007fff8d24bd6d -[NSTextView keyDown:] + 658
9 AppKit 0x00007fff8d1f856b -[NSWindow sendEvent:] + 1843
10 AppKit 0x00007fff8d199b32 -[NSApplication sendEvent:] + 3395
11 AppKit 0x00007fff8cfe99f9 -[NSApplication run] + 646
12 AppKit 0x00007fff8cfd4783 NSApplicationMain + 940
13 ResponderExperiment 0x0000000100002de2 main + 34
14 libdyld.dylib 0x00007fff8c5bf5fd start + 1
15 ??? 0x0000000000000003 0x0 + 3
)
2014-08-17 16:30:26.014 ResponderExperiment[712:303] (
0 ResponderExperiment 0x00000001000014dd -[WMWindow makeFirstResponder:] + 61
1 AppKit 0x00007fff8d03c918 -[NSControl abortEditing] + 83
2 AppKit 0x00007fff8d2723e0 -[NSValueBinder discardEditing] + 162
3 AppKit 0x00007fff8d1aee30 -[NSController discardEditing] + 115
4 AppKit 0x00007fff8d1af971 -[NSArrayController rearrangeObjects] + 27
5 AppKit 0x00007fff8d3697a9 -[NSArrayController observeValueForKeyPath:ofObject:change:context:] + 294
6 AppKit 0x00007fff8d39ac08 -[NSArrayController _setMultipleValue:forKeyPath:atIndex:] + 323
7 AppKit 0x00007fff8d39ba12 -[NSArrayController _setSingleValue:forKeyPath:] + 137
8 Foundation 0x00007fff8bf9e19a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 285
9 AppKit 0x00007fff8d25444c -[NSBinder _setValue:forKeyPath:ofObject:mode:validateImmediately:raisesForNotApplicableKeys:error:] + 364
10 AppKit 0x00007fff8d254287 -[NSBinder setValue:forBinding:error:] + 245
11 AppKit 0x00007fff8d74c4ee -[NSValueBinder _applyObjectValue:forBinding:canRecoverFromErrors:handleErrors:typeOfAlert:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:] + 194
12 AppKit 0x00007fff8d74c871 -[NSValueBinder applyDisplayedValueHandleErrors:typeOfAlert:canRecoverFromErrors:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:error:] + 621
13 AppKit 0x00007fff8d74c9d4 -[NSValueBinder _applyDisplayedValueIfHasUncommittedChangesWithHandleErrors:typeOfAlert:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:error:] + 127
14 AppKit 0x00007fff8d253c2e -[NSValueBinder validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:] + 436
15 AppKit 0x00007fff8d253a57 -[_NSBindingAdaptor _validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:bindingAdaptor:] + 160
16 AppKit 0x00007fff8d25399d -[_NSBindingAdaptor validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:] + 260
17 AppKit 0x00007fff8d25e0ba -[NSTextField textShouldEndEditing:] + 402
18 AppKit 0x00007fff8d25dc63 -[NSTextView(NSSharing) resignFirstResponder] + 393
19 AppKit 0x00007fff8d136170 -[NSWindow makeFirstResponder:] + 455
20 ResponderExperiment 0x000000010000154c -[WMWindow makeFirstResponder:] + 172
21 AppKit 0x00007fff8d25c778 -[NSTextView(NSPrivate) _giveUpFirstResponder:] + 257
22 AppKit 0x00007fff8d25c56c -[NSTextView(NSKeyBindingCommands) insertTab:] + 270
23 AppKit 0x00007fff8d22dc2f -[NSResponder doCommandBySelector:] + 71
24 AppKit 0x00007fff8d25b10a -[NSTextView doCommandBySelector:] + 196
25 AppKit 0x00007fff8d22d151 -[NSKeyBindingManager(NSKeyBindingManager_MultiClients) interpretEventAsCommand:forClient:] + 1392
26 AppKit 0x00007fff8d24c1c2 -[NSTextInputContext handleEvent:] + 845
27 AppKit 0x00007fff8d22b9dd -[NSView interpretKeyEvents:] + 180
28 AppKit 0x00007fff8d24bd6d -[NSTextView keyDown:] + 658
29 AppKit 0x00007fff8d1f856b -[NSWindow sendEvent:] + 1843
30 AppKit 0x00007fff8d199b32 -[NSApplication sendEvent:] + 3395
31 AppKit 0x00007fff8cfe99f9 -[NSApplication run] + 646
32 AppKit 0x00007fff8cfd4783 NSApplicationMain + 940
33 ResponderExperiment 0x0000000100002de2 main + 34
34 libdyld.dylib 0x00007fff8c5bf5fd start + 1
35 ??? 0x0000000000000003 0x0 + 3
)
So perhaps I could write my own NSArrayController rearrangeObjects: method, that does not loose focus?
Here's a possible solution. The idea is to re-implement the response of the text field (or, more accurately, the field editor serving the text field) to the Tab key as two separate steps. First, end editing, which will make the array controller rearrange its content. We end editing by resetting focus, so we don't care what the array controller may happen to do to focus anyway. Then, advance the focus from the originally-focused text field ourselves.
Set a delegate on your text fields. In that delegate, implement the following method:
- (BOOL) control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)command
{
if (command == #selector(insertTab:))
{
// End editing. This will commit the edited value and cause the array controller
// to rearranged its contents.
[control.window makeFirstResponder:nil];
// This will put focus on the next control from the text field.
[control.window selectKeyViewFollowingView:control];
// Prevent the normal processing of this command
return YES;
}
if (command == #selector(insertBacktab:))
{
// End editing. This will commit the edited value and cause the array controller
// to rearranged its contents.
[control.window makeFirstResponder:nil];
// This will put focus on the previous control from the text field.
[control.window selectKeyViewPrecedingView:control];
// Prevent the normal processing of this command
return YES;
}
return NO;
}
One thing I'm not sure of: what happens if instead of Tab-ing away from the text field, you click on another text field. Does the array controller still reset focus in that case?
In my case, I have overrided abortEditing method of NSControl, with my own logic.
- (BOOL)abortEditing {
MyObject *object = [myArrayController.arrangedObjects objectAtIndex:self.editedRow];
return ![[Manager sharedInstance] isObjectBeingEditing:object];
}

Problems when deselecting checkboxes inside bound NSTableview's columns

I am using this project, created by aneesh171 that shows the creation of checkboxes inside NSTableView and how to bind them. I am new to OSX development and trying to understand how it works.
The project is basically checkboxes inside a NSTableView's column that is bound to an array controller.
For some reason, when I deselect one of those checkboxes, the following messages appear on the console window:
2014-04-03 04:22:55.579 MyApp[5735:303] Error setting value for key path objectValue.selected of object <NSAutounbinder: 0x600000258120 - a nonretaining proxy for <NSTableCellView: 0x60000019f890>> (from bound object <NSButton: 0x60000015efb0>): [<__NSDictionaryI 0x61000006f540> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key selected.
2014-04-03 04:22:55.581 MyApp[5735:303] (
0 CoreFoundation 0x00007fff87b3a25c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8a3f6e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff87b3a038 +[NSException raise:format:arguments:] + 104
3 AppKit 0x00007fff927936c5 -[NSBinder _setValue:forKeyPath:ofObject:mode:validateImmediately:raisesForNotApplicableKeys:error:] + 837
4 AppKit 0x00007fff92793327 -[NSBinder setValue:forBinding:error:] + 245
5 AppKit 0x00007fff92c8b4ee -[NSValueBinder _applyObjectValue:forBinding:canRecoverFromErrors:handleErrors:typeOfAlert:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:] + 194
6 AppKit 0x00007fff92c8b871 -[NSValueBinder applyDisplayedValueHandleErrors:typeOfAlert:canRecoverFromErrors:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:error:] + 621
7 AppKit 0x00007fff927c5107 -[NSValueBinder performAction:] + 288
8 AppKit 0x00007fff927c4fc5 -[_NSBindingAdaptor _objectDidTriggerAction:bindingAdaptor:] + 133
9 AppKit 0x00007fff927531a0 -[NSControl sendAction:to:] + 56
10 AppKit 0x00007fff9279fced -[NSCell _sendActionFrom:] + 128
11 AppKit 0x00007fff927b96c5 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2316
12 AppKit 0x00007fff927b8a97 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 487
13 AppKit 0x00007fff927b81ad -[NSControl mouseDown:] + 706
14 AppKit 0x00007fff92739b48 -[NSWindow sendEvent:] + 11296
15 AppKit 0x00007fff926d86e4 -[NSApplication sendEvent:] + 2021
16 AppKit 0x00007fff92528b89 -[NSApplication run] + 646
17 AppKit 0x00007fff92513913 NSApplicationMain + 940
18 MyApp 0x0000000100049132 main + 34
19 libdyld.dylib 0x00007fff85ffe5fd start + 1
When the checkbox is selected this message does not show on the console.
Can you guys tell me what is wrong?
I updated the git version of the project.
Whenever you bind a control through value binding, then it calls its getter method to load the values. whereas when you tries to modify these values, it will try to call the setter method of the property. In the project the dictionaries were hardcoded therefore there were no setter method and that is the reason why you got the "Error setting value for key path objectValue.selected".
Check the new version where the FruitModel has been created which resolves the setter issue.
Let me know if you face any problems.

Detecting tab view change Xcode OSX AppleScriptObjC

I am building an application in AppleScriptObjC and I have a beginner question - how can I handle clicking on tab buttons in my app? I'm trying to make it have different sizes for each tab. I can make the window resize, but not when you click on a tab. Anyway, I've connected the tab view to the delegate
and such, and added a didSelectTabView function as follows:
on didSelectTabViewItem_(tabItem)
display alert "Testing"
end didSelectTabViewItem_
...and I get the following error:
2013-05-30 18:50:10.970 MacUtil[16354:303] Error setting value for key path self of object <AppDelegate #0x1005d3040: OSAID(4)> (from bound object <NSTabView: 0x101c05490>): [<AppDelegate 0x1005d3040> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key self.
2013-05-30 18:50:10.972 MacUtil[16354:303] (
0 CoreFoundation 0x00007fff911210a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff9522e3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff91120ee8 +[NSException raise:format:arguments:] + 104
3 AppKit 0x00007fff92e3414c -[NSBinder _setValue:forKeyPath:ofObject:mode:validateImmediately:raisesForNotApplicableKeys:error:] + 902
4 AppKit 0x00007fff92e33d6d -[NSBinder setValue:forBinding:error:] + 248
5 AppKit 0x00007fff93493625 -[NSViewStateBinder _setValue:forBinding:errorFallbackMessage:] + 63
6 AppKit 0x00007fff933a5198 -[NSTabViewBinder tabView:didSelectTabViewItem:] + 330
7 AppKit 0x00007fff934b9c75 -[_NSBindingAdaptor tabView:didSelectTabViewItem:] + 136
8 AppKit 0x00007fff92fe9029 -[NSTabView selectTabViewItem:] + 1172
9 AppKit 0x00007fff9303ea8c -[NSTabView mouseDown:] + 156
10 AppKit 0x00007fff92fad60e -[NSWindow sendEvent:] + 6853
11 AppKit 0x00007fff92fa9744 -[NSApplication sendEvent:] + 5761
12 AppKit 0x00007fff92ebf2fa -[NSApplication run] + 636
13 AppKit 0x00007fff92e63cb6 NSApplicationMain + 869
14 MacUtil 0x0000000100000f0a main + 74
15 libdyld.dylib 0x00007fff93a297e1 start + 0
16 ??? 0x0000000000000003 0x0 + 3
)
Any help would be appreciated!
Solved! I just had to uncheck "Raises for Non Applicable Keys" in the place where you bind it to the app delegate. It was doing that because I hadn't implemented all of the methods for tab view event handling.

Resources