Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9096177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:46:54+00:00 2026-06-16T23:46:54+00:00

From my question here: Without subclassing a UIView or UIViewController: possible to catch if

  • 0

From my question here: Without subclassing a UIView or UIViewController: possible to catch if a subview was added?

I’m wondering how to port the code from the answer to MonoTouch. It basically replaces a method with a new one and then calls the old one without subclassing. Is it possible at all to get this pointer juggling it to work in 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:46:56+00:00Added an answer on June 16, 2026 at 11:46 pm

    This looks like a good candidate for us to provide a general purpose mechanism to hijack methods. Here is an implementation in pure C# code that you can use in the meantime:

        [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");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First time asking a question here. Usually I can find my answer without having
The question originated from here: http://www.java.net/forum/topic/glassfish/glassfish/configuring-glassfish-logging-format - without an answer. The default GlassFish 3
From my question at here: Cast in List object I accepted the answer using
The question raised from the question here . In order to have search engines
Following on from my question here , if I replace an image in a
I'm trying to implement this example http://jsfiddle.net/gzF6w/1/ (from another question here on stak) on
Simple question here: is there any way to convert from a jagged array to
I asked a question here about an Linq error that results from mixing Linq-To-SQL
This is a follow-on question from the one I asked here . Can constraints
This is a continuation of this question from yesterday . Here are my three

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.