Bug 9701 - NSInvalidArgumentException when using LeftBarButtonItems/RightBarButtonItems
Summary: NSInvalidArgumentException when using LeftBarButtonItems/RightBarButtonItems
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 6.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-01-22 03:21 UTC by Maxim
Modified: 2013-01-23 03:18 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FEATURE

Description Maxim 2013-01-22 03:21:19 UTC
I tested my app on iOS simulator 4.3 and found that when I'm using UINavigationItem's LeftBarButtonItems/RightBarButtonItems properties, app crashed:

2013-01-22 11:18:42.455 Moments[23923:18303] -[UINavigationItem leftBarButtonItems]: unrecognized selector sent to instance 0x75f70b0
2013-01-22 11:18:47.526 Moments[23923:18303] Error when call addLinesToBarButton: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[UINavigationItem leftBarButtonItems]: unrecognized selector sent to instance 0x75f70b0
  at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSend (intptr,intptr)
  at MonoTouch.UIKit.UINavigationItem.get_LeftBarButtonItems () [0x00010] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UINavigationItem.g.cs:443 
  at Moments.Views.Tools.NavigationControllerCustom+NavigationControllerDelegate.WillShowViewController (MonoTouch.UIKit.UINavigationController navigationController, MonoTouch.UIKit.UIViewController viewController, Boolean animated) [0x0005b] in /Users/mobile/Projects/msn/msn_client/Moments/Views/Tools/Navigation/NavigationControllerCustom.cs:109 

Note that when I'n using these properties, no any UIBarButtonItems are added. So properties values should be equals to null.
Comment 1 Sebastien Pouliot 2013-01-22 10:34:37 UTC
The properties were added in iOS 5.0 [1] so it's normal (for ObjC) to throw an exception when calling an unknown selector.

If you want your application to work on earlier iOS version (and still use new features when available) then you need to add runtime checks for them. E.g.

if (UIDevice.CurrentDevice.CheckSystemVersion (5,0)) {
   // set many items
} else {
   // adapt UI to use a single item
}

[1] http://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationItem_Class/Reference/UINavigationItem.html#//apple_ref/occ/instp/UINavigationItem/leftBarButtonItems
Comment 2 Maxim 2013-01-22 11:07:14 UTC
Hm... I am using the same solution for methods, but not properties.
Why not check for iOS version and "do nothing" on < 5.0 version by default?
Comment 3 Sebastien Pouliot 2013-01-22 11:23:57 UTC
Several reasons, including that:

a) it would bloat monotouch.dll (a lot of extra code) and make it harder to test (hence more buggy);

b) the extra checks would always be executed (even if your app only supports 6.0 as it's minimum). This would add a non-trivial overhead (e.g. for a selector called inside a loop);

c) doing nothing is not always possible (e.g. ref and out parameters) or has side effects. The resulting crash could happen elsewhere and be much harder to diagnose;

d) it's non-obvious when testing an app. In your example not showing the extra items might remove important features from your application. If you test your app on 4.3 then the exception will tell you (or anyone else) there's something wrong;

IMHO the real solution is to have some extra tool (maybe a gendarme rule) that will analyze applications and shows API not available in the deployment target. Then it becomes a simple review to ensure that a version check surround those API calls.
Comment 4 Maxim 2013-01-23 03:18:45 UTC
Solid arguments. Thank you. Since not I'll be more accurate in code.