title: Handling multiple buttons in UIToolbar
body: <p>I have a problem using multiple <code>UIBarButtonItem</code>s within a <code>UIToolbar</code> on one side of a <code>UINavigationController</code>. I‘ve used one of the many examples on how to put multiple buttons in the navigation bar (using a toolbar), and while I got it displaying correctly now, I always get an unkown Objective-C exception thrown when a button is pressed.</p>
  
  <p>This is the full test UIViewController I use for adding multiple buttons:</p>
  
  <pre><code>using System;
  using System.Drawing;
  using MonoTouch.UIKit;
  
  namespace TestProject
  {
      public class Test : UIViewController
      {
          public override void ViewDidLoad ()
          {
              base.ViewDidLoad();
  
              UIBarButtonItem btn1 = new UIBarButtonItem( "foo", UIBarButtonItemStyle.Bordered, delegate(object sender, EventArgs e) {
                  Console.WriteLine( "asdf" );
              } );
  
              UIBarButtonItem btn2 = new UIBarButtonItem();
              btn2.Style = UIBarButtonItemStyle.Bordered;
              btn2.Title = "bar";
              btn2.Clicked += delegate(object sender, EventArgs e) {
                  Console.WriteLine( "fdsa" );
              };
  
              UIToolbar tb = new UIToolbar( new RectangleF( 0, 0, 150, 44.0f ) );
              tb.SetItems( new UIBarButtonItem[] { btn1, btn2 }, false );
              NavigationItem.RightBarButtonItem = new UIBarButtonItem( tb );
          }
      }
  }
  </code></pre>
  
  <p>I put that controller within a <code>UINavigationController</code> and display its view as the only window content. Everything works fine, until I click one of the buttons:</p>
  
  <pre><code>Error connecting stdout and stderr (127.0.0.1:10001)
  Stacktrace:
  
    at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) &lt;IL 0x0009f, 0xffffffff&gt;
    at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26
    at TestProject.Application.Main (string[]) [0x00000] in /Users/Apple/Projects/TestProject/TestProject/Main.cs:16
    at (wrapper runtime-invoke) &lt;Module&gt;.runtime_invoke_void_object (object,intptr,intptr,intptr) &lt;IL 0x00050, 0xffffffff&gt;
  
  Native stacktrace:
  
      0   TestProject                         0x000d0de8 mono_handle_native_sigsegv + 343
      1   TestProject                         0x0000f74c mono_sigsegv_signal_handler + 322
      2   libSystem.B.dylib                   0x906fc45b _sigtramp + 43
      3   ???                                 0xffffffff 0x0 + 4294967295
      4   UIKit                               0x01ee6cc3 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
      5   UIKit                               0x01cd44fd -[UIApplication sendAction:to:from:forEvent:] + 119
      6   UIKit                               0x01d64799 -[UIControl sendAction:to:forEvent:] + 67
      7   UIKit                               0x01d66c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
      8   UIKit                               0x01d657d8 -[UIControl touchesEnded:withEvent:] + 458
      9   UIKit                               0x01cf8ded -[UIWindow _sendTouchesForEvent:] + 567
      10  UIKit                               0x01cd9c37 -[UIApplication sendEvent:] + 447
      11  UIKit                               0x01cdef2e _UIApplicationHandleEvent + 7576
      12  GraphicsServices                    0x0404f992 PurpleEventCallback + 1550
      13  CoreFoundation                      0x00ea2944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
      14  CoreFoundation                      0x00e02cf7 __CFRunLoopDoSource1 + 215
      15  CoreFoundation                      0x00dfff83 __CFRunLoopRun + 979
      16  CoreFoundation                      0x00dff840 CFRunLoopRunSpecific + 208
      17  CoreFoundation                      0x00dff761 CFRunLoopRunInMode + 97
      18  GraphicsServices                    0x0404e1c4 GSEventRunModal + 217
      19  GraphicsServices                    0x0404e289 GSEventRun + 115
      20  UIKit                               0x01ce2c93 UIApplicationMain + 1160
      21  ???                                 0x077efe64 0x0 + 125763172
      22  ???                                 0x077ef1e2 0x0 + 125759970
      23  ???                                 0x077ef0be 0x0 + 125759678
      24  ???                                 0x077ef145 0x0 + 125759813
      25  TestProject                         0x0000f507 mono_jit_runtime_invoke + 1332
      26  TestProject                         0x001ed259 mono_runtime_invoke + 137
      27  TestProject                         0x001ef940 mono_runtime_exec_main + 669
      28  TestProject                         0x001eed2a mono_runtime_run_main + 843
      29  TestProject                         0x000a3093 mono_jit_exec + 200
      30  TestProject                         0x002a163d main + 4060
      31  TestProject                         0x00002819 _start + 208
      32  TestProject                         0x00002748 start + 40
  
  Debug info from gdb:
  
  dyld: could not load inserted library: /Users/Apple/Library/Application Support/iPhone Simulator/4.3/Applications/86C48AC1-821F-49DD-B344-58471F02D31B/TestProject.app/monotouch-fixes.dylib
  
  
  =================================================================
  Got a SIGSEGV while executing native code. This usually indicates
  a fatal error in the mono runtime or one of the native libraries 
  used by your application.
  =================================================================
  </code></pre>
  
  <p>Does anyone know what‘s causing this, and more importantly how to avoid it? When setting just a single button as the <code>RightBarButtonItem</code> it works without any problems.</p>
  
  <h3>edit</h3>
  
  <p>Above error seems to randomly cycle with this one:</p>
  
  <pre><code>Error connecting stdout and stderr (127.0.0.1:10001)
  2011-08-25 13:30:05.409 TestProject[1235:a0b] -[__NSCFSet InvokeAction:]: unrecognized selector sent to instance 0x7168980
  
  Unhandled Exception: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[__NSCFSet InvokeAction:]: unrecognized selector sent to instance 0x7168980
    at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
    at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26 
    at TestProject.Application.Main (System.String[] args) [0x00000] in /Users/Apple/Projects/TestProject/TestProject/Main.cs:16 
  [ERROR] FATAL UNHANDLED EXCEPTION: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[__NSCFSet InvokeAction:]: unrecognized selector sent to instance 0x7168980
    at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
    at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26 
    at TestProject.Application.Main (System.String[] args) [0x00000] in /Users/Apple/Projects/TestProject/TestProject/Main.cs:16 
  </code></pre>