I'm not sure it's possible, but I'd love to know how Miguel,
Sebastian, Jeff, Rolf and co do it.

I dont think that Extension Methods would work. You can add something
which looks like it's part of a class, but it's not. eg:

public class Foo
{
  public void DoSomething() {}
}

public static class FooExtension
{
  public void DoSomething(this Foo theFooObject) {}
}

I'm pretty sure thats not going to work. Esp if you think that the
DoSomething method is actually a selector, and Obj-C/Cocoa has no
concept of Extension methods, so it wouldn't know what to call.

The only thing I can think of is maybe a partial class, but I can't
see how that would work - you are still only allowed one "instance" of
DoSomething in a given class:

public partial class Foo1
{
  public void DoSomething() {}
}

public partialClass Foo1 {
  public void DoSomething() {} //not allowed
}

What the category is doing is saying "if I pull in this class,
_rewrite the dispatch table for Foo, so that if ANYONE ANYWHERE calls
DoSomething, they call this method over here:.....". All without
changing Foo, except at runtime. Basically, Monkey Patching
(http://en.wikipedia.org/wiki/Monkey_patch -
http://www.codinghorror.com/blog/2008/07/monkeypatching-for-humans.html)

I'd love it if I could do this, in MY code (not somewhere deep in MonoTouch)

public partial class UINavigationBar
{
  [Export("draw:")]
  public void TheNewDrawFunction(....)
  {
    //my code in here, and I can call this.Draw and get the old one if I want
  }
}

Which would do the monkey patch to UINavigationBar... but sadly, I
don't think we CAN do this.

DERMOT: My answer on Earnest (earnestapp.com) was to use UIAppearence
for iOS5, and just have a fall back of a "pretty enough" tint colour
for iOS4. It's trivial to do this with UIAppearence, and fully
supported by Apple (the Category method isn't, and I believe broke a
lot of apps in iOS5). But thats up to you.



On Wed, Apr 18, 2012 at 13:10, Christopher Hatton
<[email protected]> wrote:
> Hi Dermotos,
>
> Yes, a close equivalent to Obj-C Categories, in C#, is Extension Methods.
> See here:
>
> http://msdn.microsoft.com/en-us/library/bb383977.aspx
>
> - Chris
>
>
> On 18 April 2012 12:54, dermotos <[email protected]> wrote:
>>
>> Is there an equivalent of Objective C Categories in Monotouch?
>> Specifically I need to override the draw method in a NavigationBar to
>> custom
>> draw it (in iOS4).
>> I cannot merely subclass the NavigationBar and override the Draw method,
>> as
>> I cannot then set my custom version as the NavigationBar on the
>> NavigationController, as the property is read-only.
>>
>> Obj-c tutorials describe using Categories to overcome this problem, but
>> obviously id like to do this in MT.
>>
>> --
>> View this message in context:
>> http://monotouch.2284126.n4.nabble.com/Obj-C-Categories-in-Monotouch-tp4567460p4567460.html
>> Sent from the MonoTouch mailing list archive at Nabble.com.
>> _______________________________________________
>> MonoTouch mailing list
>> [email protected]
>> http://lists.ximian.com/mailman/listinfo/monotouch
>
>
>
> _______________________________________________
> MonoTouch mailing list
> [email protected]
> http://lists.ximian.com/mailman/listinfo/monotouch
>



-- 
Nic Wise
t.  +44 7788 592 806 | @fastchicken | http://www.linkedin.com/in/nicwise
b. http://www.fastchicken.co.nz/

Earnest: Self-employed? Track your business expenses and income.
http://earnestapp.com
Nearest Bus: find when the next bus is coming to your stop. http://goo.gl/Vcz1p
mobileAgent (for FreeAgent): get your accounts in your pocket.
http://goo.gl/IuBU
Trip Wallet: Keep track of your budget on the go: http://goo.gl/ePhKa
London Bike App: Find the nearest Boris Bike, and get riding! http://goo.gl/Icp2
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to