开发者

Getting an exception for Modal Transition that is already in progress,?

开发者 https://www.devze.com 2023-02-14 05:09 出处:网络
Note: I\'m using MonoTouch. I get the following exception when I click on the tbName (textfield, see its delegate below) for the second time. The first time the modal view comes up without a problem

Note: I'm using MonoTouch.

I get the following exception when I click on the tbName (textfield, see its delegate below) for the second time. The first time the modal view comes up without a problem, and I dismiss it (code also provided below). But the second time I click on the tbName (Textfield by the way), I get this exception:

"Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Attemping to being a modal transition from to while a transition is already in progress. Wait for viewDidAppear/viewDidDisappear to know the current transition has completed"

The thing is though, it doesn't matter how long I wait before clicking, so I figure there's a fundamental I'm missing

From the NewEnvelopeViewController class:
public override void ViewDidAppear (bool animated)
   {
        base.ViewDidAppear (animated);

    tbName.TouchDown += delegate(object sender, EventArgs e) {
    // Set everything up for the editting of the envelope's name
        _dataViewController.NumericPad = false;
        _dataViewController.FieldType = NewEnvelopeDataViewController.Fields.Name;
        _dataViewController.EnvelopeToEdit = _envelope;

        this.PresentModalViewController(_dataViewController, true);
    };

From the NewEnvelopeDataViewController class:

public override void ViewDidAppear (bool animated) { base.ViewDidAppear (animated);

        if (NumericPad) {
            tbValueArea.KeyboardType = UIKeyboardType.DecimalPad;
        }
        else {
            tbValueArea.KeyboardType = UIKeyboardType.Default;
        }

        switch (FieldType) {
        case Fields.Name:
            NavigationItem.Title = "Enter the name";
            tbValueArea.Text = EnvelopeToEdit.Name;
      开发者_如何转开发      tbValueArea.KeyboardType = UIKeyboardType.Default;
            break;
        case Fields.Budget:
            NavigationItem.Title = "Set your budget";
            tbValueArea.Text = EnvelopeToEdit.Budget.ToString();
            tbValueArea.KeyboardType = UIKeyboardType.DecimalPad;
            break;
        case Fields.Remainder:
            NavigationItem.Title = "What remains of your budget?";
            tbValueArea.Text = "0";
            tbValueArea.KeyboardType = UIKeyboardType.DecimalPad;
            break;
        default:
            throw new NotImplementedException("ViewDidAppear has a FieldType not accounted for");
        }
    }

    partial void clickedDone (UIBarButtonItem sender)
    {
        switch (FieldType) {
        case Fields.Name:
            EnvelopeToEdit.Name = tbValueArea.Text;
            break;
        case Fields.Budget:
            break;
        default: // Not implemented yet
            throw new NotImplementedException("FieldType not accounted for yet");
            break;
        }

        this.ParentViewController.DismissModalViewControllerAnimated(true);
    }

I've seen an article on StackOverflow regarding doing something with DidAppear, but it didn't seem to work in my case and it wasn't clear what exactly the problem is to me (rather than just patching over it).


Move the following code,

tbName.TouchDown += delegate(object sender, EventArgs e) {
    // Set everything up for the editting of the envelope's name
        _dataViewController.NumericPad = false;
        _dataViewController.FieldType = NewEnvelopeDataViewController.Fields.Name;
        _dataViewController.EnvelopeToEdit = _envelope;

        this.PresentModalViewController(_dataViewController, true);
    };

from the ViewDidAppear method to the ViewDidLoad method. The ViewDidAppear gets triggered everytime the view appears, including when a modal view controller gets dismissed. Hence, what your code right now is doing is adding another handler to the object's TouchDown event so when it is triggered, it always executes one more extra time, resulting in presenting the same modal view controller many times. That's why it works the first time, but the second time it crashes.

In general, if you want to subscribe to events in ViewDidAppear, be sure to unsubscribe them in the ViewDidDisappear or, even better, always subscribe events in ViewDidLoad and unsubscribe in ViewDidUnload.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号