Can you wire 2 UIBarButtons (or any 开发者_如何学JAVAcontrol really) to the same Action on a controller?
I have tried with Interface Builder, is there a way to do it? If there is, I feel there is a trick in IB I don't know.
Right now I have been making MyAction1:, and MyAction2: and have them call the same method within the controller, which is really ugly to me.
You most definitely can! Just have one
-(IBAction) MyAction:
and hook up (using Ctrl + Select) multiple UIButtons or UIControls to it in Interface Builder. Next to listing of MyAction in "File's Owner" in the IB, it would be displayed as "Multiple" (and you can expand this to see the list) to indicate more than one control is connected to this action. Have used it successfully, many times over.
If you want to have multiple buttons use the same handler to share the same code:
void handler (object sender, EventArgs args)
{
if (sender == button1)
Console.WriteLine ("button1");
else
Console.WriteLine ("some other button");
}
button1.TouchDown += handler;
button2.TouchDown += handler;
also if you need more information, you can visit the documentation about this on
http://monotouch.net/Documentation/Events
Hope this helps =)
Alex
精彩评论