Bug 17761 - Crash when using ShouldReturn on textfield in UIAlertView
Summary: Crash when using ShouldReturn on textfield in UIAlertView
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 7.0.4.x
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-02-13 16:19 UTC by René Ruppert
Modified: 2014-04-07 14:10 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description René Ruppert 2014-02-13 16:19:32 UTC
The code below shows a UIAlertView and makes it "block" by using a TaskCompletionSource. Works fine. However if I try to use ShouldReturn (the marked code), it crashes shortly after showing the alert. What I would like to achieve: hitting return on the keyboard should dismiss the alert. By default, only the keyboard disappears. There is no stack trace. It just dies.

The reason seems to be that either the textfield or the alert view are already collected. A workaround is to capture the textfield in a variable. The issue has been around since December 2011!

See also there: http://stackoverflow.com/questions/8343397/

public Task<string> ShowInputDialog(string title, string message, string btnOk, string btnCancel)
    var alertView = new UIAlertView(title == null ? string.Empty : title, message == null ? string.Empty : message, null, btnCancel, btnOk);
    alertView.AlertViewStyle = UIAlertViewStyle.PlainTextInput;

        // Comment this in and it crashes:
        alertView.GetTextField(0).ShouldReturn = sender => {
    alertView.DismissWithClickedButtonIndex(alertView.FirstOtherButtonIndex, true);
    return true;

    var tcs = new TaskCompletionSource<string>();

    alertView.Clicked += (sender, args) =>
        if (args.ButtonIndex == alertView.CancelButtonIndex)
        return tcs.Task;

Latest iOS versions available from the alpha channel are used on iOS 7.
Comment 2 Sebastien Pouliot 2014-04-07 14:10:41 UTC
This issue is resolved when using the New-Refcount (NRC) feature [1]. This is not a new feature [2] but it has totally revamped [3] in the last two months based on the bug reports, like this one.

With XI 7.2.1 this features works with both Boehm (default) and Sgen garbage collectors. NRC has also been enhanced and, right now, there are no known issues (bugs) against it.

While NRC is not the default option (in 7.2.1) we plan to make it so in the near future. Additional testing and feedback on the feature would be appreciated.

[1] http://docs.xamarin.com/guides/ios/advanced_topics/newrefcount/
[2] http://docs.xamarin.com/releases/ios/MonoTouch_5/MonoTouch_5.2/
[3] Newer versions of Xamarin Studio will remove the experimental tag on the feature when XI 7.2.1+ is used.