从这里我的问题: 没有子一个UIView或UIViewController中:可能赶上如果加入一个子视图?
我不知道如何从回答的MonoTouch端口的代码。 它基本上取代了用一个新的方法,然后调用旧的没有子。 是否有可能在所有获得这个指针杂耍它在MonoTouch的工作吗?
//Makes views announce their change of superviews
Method method = class_getInstanceMethod([UIView class], @selector(willMoveToSuperview:));
IMP originalImp = method_getImplementation(method);
void (^block)(id, UIView*) = ^(id _self, UIView* superview) {
[_self willChangeValueForKey:@"superview"];
originalImp(_self, @selector(willMoveToSuperview:), superview);
[_self didChangeValueForKey:@"superview"];
};
IMP newImp = imp_implementationWithBlock((__bridge void*)block);
method_setImplementation(method, newImp);
这看起来像一个很好的候选人为我们提供了一个通用的机制来劫持的方法。 这里是纯C#代码的实现,你可以在此期间使用:
[DllImport ("/usr/lib/libobjc.dylib")]
extern static IntPtr class_getInstanceMethod (IntPtr classHandle, IntPtr Selector);
[DllImport ("/usr/lib/libobjc.dylib")]
extern static Func<IntPtr,IntPtr,IntPtr> method_getImplementation (IntPtr method);
[DllImport ("/usr/lib/libobjc.dylib")]
extern static IntPtr imp_implementationWithBlock (ref BlockLiteral block);
[DllImport ("/usr/lib/libobjc.dylib")]
extern static void method_setImplementation (IntPtr method, IntPtr imp);
static Func<IntPtr,IntPtr,IntPtr> original_impl;
void HijackWillMoveToSuperView ()
{
var method = class_getInstanceMethod (new UIView ().ClassHandle, new Selector ("willMoveToSuperview:").Handle);
original_impl = method_getImplementation (method);
var block_value = new BlockLiteral ();
CaptureDelegate d = MyCapture;
block_value.SetupBlock (d, null);
var imp = imp_implementationWithBlock (ref block_value);
method_setImplementation (method, imp);
}
delegate void CaptureDelegate (IntPtr block, IntPtr self, IntPtr uiView);
[MonoPInvokeCallback (typeof (CaptureDelegate))]
static void MyCapture (IntPtr block, IntPtr self, IntPtr uiView)
{
Console.WriteLine ("Moving to: {0}", Runtime.GetNSObject (uiView));
original_impl (self, uiView);
Console.WriteLine ("Added");
}
我不得不作出以下修改米格尔的例子得到它的工作的设备上。
[DllImport("/usr/lib/libobjc.dylib")]
extern static IntPtr class_getInstanceMethod(IntPtr classHandle, IntPtr Selector);
[DllImport("/usr/lib/libobjc.dylib")]
extern static IntPtr method_getImplementation(IntPtr method);
[DllImport("/usr/lib/libobjc.dylib")]
extern static IntPtr imp_implementationWithBlock(ref BlockLiteral block);
[DllImport("/usr/lib/libobjc.dylib")]
extern static void method_setImplementation(IntPtr method, IntPtr imp);
static IntPtr original_impl;
[MonoNativeFunctionWrapper]
public delegate void OriginalDelegate(IntPtr one,IntPtr two);
static void HijackWillMoveToSuperView()
{
var method = class_getInstanceMethod(new UIView().ClassHandle, new Selector("willMoveToSuperview:").Handle);
original_impl = method_getImplementation(method);
var block_value = new BlockLiteral();
CaptureDelegate d = MyCapture;
block_value.SetupBlock(d, null);
var imp = imp_implementationWithBlock(ref block_value);
method_setImplementation(method, imp);
}
delegate void CaptureDelegate(IntPtr block,IntPtr self,IntPtr uiView);
[MonoPInvokeCallback(typeof(CaptureDelegate))]
static void MyCapture(IntPtr block, IntPtr self, IntPtr uiView)
{
Console.WriteLine("Moving to: {0}", Runtime.GetNSObject(uiView));
var del = (OriginalDelegate) Marshal.GetDelegateForFunctionPointer (original_impl,typeof(OriginalDelegate));
del (self, uiView);
Console.WriteLine("Added");
}
文章来源: How to port “method_getImplementation” and “method_setImplementation” to MonoTouch?