NSTableView Disappearing Text

November 23rd, 2016

Filed under: Cocoa, Mac Development | Be the first to comment!

I was having a problem with disappearing text in a table view in a Mac application I’m developing. Selecting an item in the table view erased that item’s text from the table view. The table view is part of a split view. Dragging the split view’s divider erased the text from every item in the table view.

Finding the cause of the disappearing text was difficult. I had other projects with table views in a split view whose text wasn’t erased. I couldn’t see any differences in the projects whose table views kept their text and the project that erased the table view text.

I eventually discovered the cause of the disappearing table text: source list highlighting. Changing the table view’s highlighting from source list to regular fixed the issue. I can now drag the split view divider without erasing the text of every item in the table view.

Facebooktwittergoogle_plusredditmail

Maintaining NSTextView Insertion Point After Core Data Save

November 21st, 2016

Filed under: Cocoa, Mac Development | 1 comment

I am working on a Mac journaling application that uses Core Data. The user interface consists of a table view and a text view. The table view contains a list of dates. Selecting a date fills the text view with what was written on that date. Use the text view to write.

One problem I had was saving the text. Core Data is set up initially to save a text view or text field’s contents only when leaving the text view or text field. This behavior works fine for a contact form, but not for a journaling application. Core Data would save the text only when I selected a new date in the table view. With this behavior there’s potential for data loss.

I wrote some code to autosave the text view’s contents periodically. The saving worked, but every time I saved, the text view lost focus. When the text view lost focus, I had to click in the text view to resume typing. Autosaving provided a miserable typing experience. How could I autosave while allowing someone to keep typing? I had to perform the following steps:

  1. Call the NSTextView function selectedRange to save where the person was typing during the autosave.
  2. Call the NSWindow function makeFirstResponder to make the text view the first responder so the text view didn’t lose focus.
  3. Call the NSTextView function setSelectedRange to set the typing position back to where it was before the autosave.
  4. Call the NSText function scrollRangeToVisible to make the text view scroll to the point where the person was typing.

Here’s some Swift 3 code.

func updateTextViewAfterAutosave() {
    let insertionRange = textView.selectedRange()
    window.makeFirstResponder(textView)
    textView.setSelectedRange(insertionRange)
    textView.scrollRangeToVisible(insertionRange)
}

Make sure you don’t autosave too often. When I used this code to autosave after every change to the text view’s contents, the text view scrolling was visibly jumpy. Autosaving every 30 seconds worked well.

Facebooktwittergoogle_plusredditmail

Xcode 8: Automation Instrument Removed

November 14th, 2016

Filed under: Instruments, Xcode | Be the first to comment!

Apple removed the Automation instrument from Instruments in Xcode 8. Use Xcode’s user interface testing to test your iOS app’s user interface.

Facebooktwittergoogle_plusredditmail

Xcode 8: Address Sanitizer Supports Swift

September 16th, 2016

Filed under: Xcode | Be the first to comment!

I wrote about Xcode’s Address Sanitizer in my Xcode 7: Address Sanitizer post. Apple added Swift support to Address Sanitizer in Xcode 8.

Facebooktwittergoogle_plusredditmail

Xcode 8: New Project and New File Assistant Changes

September 14th, 2016

Filed under: Xcode | Be the first to comment!

Xcode 8 changes the look of the assistant windows for creating new projects and new files. The following screenshot shows the New Project Assistant:

Xcode8NewProjectAssistant

The following screenshot shows the New File Assistant:

Xcode8NewFileAssistant

Select an operating system at the top of the assistant to view the available templates for that operating system. If you look at the New Project Assistant screenshot, you will see a Cross-platform option at the top.

Xcode8CrossPlatformProjects

The external build system project template has moved to the Cross-platform section. There’s also a project template for making a cross-platform SpriteKit game.

Facebooktwittergoogle_plusredditmail