Darius Darius - 1 month ago 15
iOS Question

Apple Watch Table on row selected crash

I am using Xamarin to develop an Apple Watch app. I added a table (WKInterfaceTable) and populate it with code-behind. But when I click on a row, the app immediately crashes.

To get this far, I actually manually edited the storyboard XML file, because it wouldn't work by simply dragging and dropping a table on the storyboard. Here is the XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="6221" systemVersion="14A389" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="AgC-eL-Hgc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6213"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="3734"/>
</dependencies>
<scenes>
<!--Interface Controller-->
<scene sceneID="aou-V4-d1y">
<objects>
<controller id="AgC-eL-Hgc" title="Favorites" customClass="InterfaceController" customModuleProvider="">
<items>
<table alignment="left" id="nXC-Mh-TYy">
<items>
<tableRow identifier="default" id="zJA-dk-zIT" customClass="FavoriteRowController">
<group key="rootItem" width="1" height="40" alignment="left" spacing="6" id="ASs-9x-fCK">
<items>
<imageView width="35" height="35" alignment="left" verticalAlignment="center" image="Door" id="rac-gq-1kM"/>
<label alignment="left" verticalAlignment="center" text="Location" id="jkO-KQ-vGr"/>
</items>
<color key="backgroundColor" red="0.93625843524932861" green="0.94465947151184082" blue="0.98500943183898926" alpha="0.14000000000000001" colorSpace="custom" customColorSpace="sRGB"/>
<edgeInsets key="margins" left="2" right="2" top="2" bottom="2"/>
</group>
<connections>
<outlet property="RowLabel" destination="jkO-KQ-vGr" id="Hcb-P2-F5Q"/>
</connections>
</tableRow>
</items>
</table>
</items>
<connections>
<outlet property="InterfaceTable" destination="nXC-Mh-TYy" id="4YS-uT-eWw"/>
</connections>
</controller>
</objects>
<point key="canvasLocation" x="0.0" y="0.0"/>
</scene>
</scenes>




Did I get something wrong in the XML?

Code for
InterfaceController
:

public partial class InterfaceController : WKInterfaceController
{
protected InterfaceController(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
}

public override void Awake(NSObject context)
{
base.Awake(context);

this.LoadTableData();

// Configure interface objects here.
Console.WriteLine("{0} awake with context", this);
}

public override void WillActivate()
{
// This method is called when the watch view controller is about to be visible to the user.
Console.WriteLine("{0} will activate", this);
}

public override void DidDeactivate()
{
// This method is called when the watch view controller is no longer visible to the user.
Console.WriteLine("{0} did deactivate", this);
}

private void LoadTableData()
{
var cityNames = new[] { "r1", "r2", "r3", "r4", "r5" };

InterfaceTable.SetNumberOfRows(cityNames.Length, "default");

for (var i = 0; i < cityNames.Length; i++)
{
var row = (FavoriteRowController)InterfaceTable.GetRowController(i);
row.RowLabel.SetText(cityNames[i]);
}
}
}


Code for
InterfaceController.designer.cs
:

[Register("InterfaceController")]
partial class InterfaceController
{
[Outlet]
public WKInterfaceTable InterfaceTable { get; set; }

void ReleaseDesignerOutlets()
{
if (InterfaceTable != null)
{
InterfaceTable.Dispose();
InterfaceTable = null;
}
}
}


Code for
FavoriteRowController
:

public partial class FavoriteRowController : WKInterfaceController
{
protected FavoriteRowController(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
}

public override void Awake(NSObject context)
{
base.Awake(context);

// Configure interface objects here.
Console.WriteLine("{0} awake with context", this);
}

public override void WillActivate()
{
// This method is called when the watch view controller is about to be visible to the user.
Console.WriteLine("{0} will activate", this);
}

public override void DidDeactivate()
{
// This method is called when the watch view controller is no longer visible to the user.
Console.WriteLine("{0} did deactivate", this);
}
}


Code for
FavoriteRowController.designer.cs
:

[Register("FavoriteRowController")]
partial class FavoriteRowController
{
[Outlet]
public WKInterfaceLabel RowLabel { get; set; }

void ReleaseDesignerOutlets()
{
}
}


The exception of the crash in the application output:

2016-11-03 10:10:58.780 MyAppWatchExtension[16255:474000] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FavoriteRowController 0x799ec7e0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key InterfaceTable.'
*** First throw call stack:
(
0 CoreFoundation 0x00383a22 __exceptionPreprocess + 194
1 libobjc.A.dylib 0x059a0e76 objc_exception_throw + 52
2 CoreFoundation 0x00383951 -[NSException raise] + 17
3 Foundation 0x013c5732 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 238
4 Foundation 0x012eac53 _NSGetUsingKeyValueGetter + 146
5 Foundation 0x012eab3b -[NSObject(NSKeyValueCoding) valueForKey:] + 282
6 WatchKit 0x04b06c16 __58-[SPRemoteInterface handlePlistDictionary:fromIdentifier:]_block_invoke.882 + 39
7 libdispatch.dylib 0x06252c4f _dispatch_call_block_and_release + 15
8 libdispatch.dylib 0x0627550f _dispatch_client_callout + 14
9 libdispatch.dylib 0x0625be31 _dispatch_main_queue_callback_4CF + 1031
10 CoreFoundation 0x00343e7e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
11 CoreFoundation 0x00306dcf __CFRunLoopRun + 2319
12 CoreFoundation 0x0030624b CFRunLoopRunSpecific + 395
13 CoreFoundation 0x003060ab CFRunLoopRunInMode + 123
14 Foundation 0x012c5192 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 308
15 Foundation 0x012c504f -[NSRunLoop(NSRunLoop) run] + 69
16 libxpc.dylib 0x0656d8a5 _xpc_objc_main + 476
17 libxpc.dylib 0x06570175 xpc_main + 215
18 Foundation 0x01323880 +[NSXPCListener serviceListener] + 0
19 PlugInKit 0x111556b6 -[PKService run] + 954
20 WatchKit 0x04b43108 main + 148
21 libxamarin-debug.dylib 0x0584fb33 xamarin_main + 3475
22 TycoMobilePassUIiOSWatchExtension 0x001055bc xamarin_watchextension_main + 124
23 libdyld.dylib 0x062ae85d start + 1
24 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Answer

If the crash happens when selecting an item and not when displaying the Table then I would assume it is to do with the row selection code, and would start there.

There are two options here depending on what you are trying todo:

DidSelectRow

Used when the row is selected and you want to manipulate some data or move to another view.

public override async void DidSelectRow(WKInterfaceTable table, nint rowIndex)
{
    var contextForNextInterfaceController = rows[(int)rowIndex]; // this depends how you have set up the table.
    Console.WriteLine($"Row selected: {rowData}");
    PushController("TheNextInterFaceController", contextForNextInterfaceController);
}

GetContextForSegue

Used when you set a segue in iOS Designer or Xcode so you can set the context for the next interface controller you are using.

public override NSObject GetContextForSegue(string segueIdentifier, WKInterfaceTable table, nint rowIndex)
{
    // Can check segueIdentifier if using more segues
    return new ContextForNextInterfaceController() // This needs to sub class NSObject
    {
        model = modelForNextInterfaceController,
    };
}

When moving to another view after setting the context you will neext to cast the context object and check it is of the type you are looking for:

public override void Awake(NSObject context)
{
    base.Awake(context);
    Console.WriteLine("{0} awake with context", this);
    var currentContext = context as ContextForNextInterfaceController;
    if (currentContext != null)
    {
        // Do stuff with context
    }
}

If you could post your Interface Controller code or the row selection code, then I might be able to help more. Also if you are getting any error in the application output that could be helpful too.

=========================================================================== Update

Ok from your error output I would says its the FavoriteRowController thats missing an outlet or wrong class set in the storyboard.

this class is not key value coding-compliant for the key InterfaceTable.'

Is a common error when you are missing a outlet or removed one but not removed the link in the storyboard.

RowControllers dont need to be InterfaceControllers so I would set it up like so:

Code for FavoriteRowController:

using Foundation;

namespace WatchConnectivity.OnWatchExtension
{
    public partial class FavoriteRowController : NSObject
    {
        protected FavoriteRowController()
        {
        }
    }
}

Code for FavoriteRowController.designer.cs:

using Foundation;
using System.CodeDom.Compiler;

namespace WatchConnectivity.OnWatchExtension
{
    [Register ("FavoriteRowController")]
    partial class FavoriteRowController
    {
        [Outlet]
        public WatchKit.WKInterfaceLabel RowLabel { get; set; }

        void ReleaseDesignerOutlets ()
        {
            if (RowLabel != null) {
                RowLabel.Dispose ();
                RowLabel = null;
            }
        }
    }
}

Check this sample

Comments