Modal View destructor not being called when InputAccessoryView is overriden
NickName:Luke Ask DateTime:2011-04-06T19:57:54

Modal View destructor not being called when InputAccessoryView is overriden

I am presenting a UIViewController modally and that view contains various UITextFields. I've overriden the modal views InputAccessoryView so that it shows a simple view, with one button that will ResignFirstResponder from every UITextField, thus dismissing the keyboard.

Recently I've been really cracking down on memory related issues so all my controllers now have a destructor. I've noticed that whenever the keyboard is shown with this overriden InputAccessoryView whenever the Modal View is dismissed the destructor will not be called. Does this mean that the UIViewController is not being destroyed? Am I incorrectly displaying the InputAccessoryView?

The InputAccessoryView code is as follows:

bool accessoryViewInit = false;
UIView accessoryView = new UIView(new RectangleF(0,0,320,30));

public override UIView InputAccessoryView 
{
    get 
    {
        if (!accessoryViewInit)
        {
            accessoryView.BackgroundColor = UIColor.FromRGBA(0.0f, 0.0f, 0.f, 0.5f);
            UIButton dismiss = new UIButton(new RectangleF(50,1, 200, 28));
            dismiss.BackgroundColor = UIColor.Blue;
            dismiss.SetTitle("Close Keyboard", UIControlState.Normal);
            dismiss.TouchUpInside += delegate(object sender, EventArgs e) {
                field1.ResignFirstResponder();
                field2.ResignFirstResponder();
                field3.ResignFirstResponder();
            };
            accessoryView.AddSubview(dismiss);
        }       
        return accessoryView;
    }
}

I have a feeling that is due to the fact I'm assigning a delegate to the TouchUpInside event for the button, is it keeping a reference to this, thus stopping the whole controller from being destroyed?

I've created a sample project which can be found at https://github.com/lukewhitt/InputAccessoryView-test

To recreate the problem: Run app, Present modal view by touching big red button. Now, if you dismiss the view without showing the keyboard, the destructor will be called. If you make on the UITextFields first responder (showing the keyboard and the InputAccessoryView) then dismiss the modal controller, the destructor won't be called.

EDIT

It would appear this is a bug in Monotouch that will be fixed in an up-coming release. In order to get around the problem, it seems you can't use anonymous delegates when assigning to events. So the dismiss.TouchUpInside would become:

public override UIView InputAccessoryView {
// code before
    dismiss.TouchUpInside += HandleDismissTouch;
// rest of code
}

private void HandleDismissTouch (object sender, EventArgs e)
{
    field1.ResignFirstResponder();
    field2.ResignFirstResponder();
    field3.ResignFirstResponder();
}

then in the code that dismisses the modal controller, I've added the following:

if (dismiss != null)
{
    dismiss.TouchUpInside -= HandleDismissTouch;
    dismiss.Dispose();
    dismiss = null;
}

which causes the destructor to be called!

Copyright Notice:Content Author:「Luke」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5565947/modal-view-destructor-not-being-called-when-inputaccessoryview-is-overriden

More about “Modal View destructor not being called when InputAccessoryView is overriden” related questions

Modal View destructor not being called when InputAccessoryView is overriden

I am presenting a UIViewController modally and that view contains various UITextFields. I've overriden the modal views InputAccessoryView so that it shows a simple view, with one button that will

Show Detail

How to prevent inputAccessoryView of a view controller from being shown when an alert is presented?

I have a custom UIViewController in which I have overridden inputAccessoryView to return a view as follows- extension LoginViewController { override var inputAccessoryView: UIView? {

Show Detail

React native IOS InputAccessoryView disappear from the screen after close modal

When I have on my screen InputAccessoryView which has component without nativeID (So it is constantly showing even if the keyboard is not shown) and I open and close Modal (react-native modal) then

Show Detail

Destructor being called twice when being explicitly invoked

I was experimenting with destructors in C++ with this piece of code: #include <iostream> struct temp { ~temp() { std::cout << "Hello!" << std::endl; } }; &

Show Detail

Destructor being called twice when being explicitly invoked

I was experimenting with destructors in C++ with this piece of code: #include <iostream> struct temp { ~temp() { std::cout << "Hello!" << std::endl; } }; &

Show Detail

Destructor being called twice when being explicitly invoked

I was experimenting with destructors in C++ with this piece of code: #include <iostream> struct temp { ~temp() { std::cout << "Hello!" << std::endl; } }; &

Show Detail

viewWillAppear is not being called when using Modal Segue

I have a Table View Controller and a Modal segue to which I can add to the data which is populating the UITableView. My code went from working to not working because the viewWillAppear is now not b...

Show Detail

why Destructor is not being called for this code

I've read this article and this too. I was trying to implement destructor in simple code. class Program { static void Main(string[] args) { CreateSubscriber(); Console.Rea...

Show Detail

Destructor being called multiple times

i am trying to check how new , init and del works and was running below mentioned code , what caught my attention is destructor is being called twice and i don't know the exact reason of it, Can an...

Show Detail

UIViewController Destructor not being called

Currently I have two UIViewControllers called FormA and FormB Now FormA calls FormB as such (formB property in formA is nonatomic and strong) self.formB = [[ MediaPlayer alloc] initWithNibName:@"

Show Detail