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

  • SEARCH
  • Home
  • 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 7702115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:09:10+00:00 2026-05-31T23:09:10+00:00

I have the following MonoTouch program: using System.Drawing; using MonoTouch.Foundation; using MonoTouch.UIKit; using System;

  • 0

I have the following MonoTouch program:

using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System;

namespace Experiment
{
    // This is just for the example, I use a singleton in my app to do this.
    public class AppSettings
    {
        public static bool IsLoggedIn = false;
    }

    [Register ("AppDelegate")]
    public class AppDelegate : UIApplicationDelegate
    {
        UIWindow window;
        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            UIViewController baseViewController = new RootViewController();

            window = new UIWindow(UIScreen.MainScreen.Bounds);
            window.AddSubview(baseViewController.View);
            window.MakeKeyAndVisible();

            return true;
        }

        static void Main(string[] args)
        {
            UIApplication.Main(args, null, "AppDelegate");
        }
    }

    public class RootViewController : UITableViewController
    {
        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);
            if (!AppSettings.IsLoggedIn) {
                this.PresentViewController(new LoginPopupController(), false, () => {});
            }

        }
    }

    public class LoginPopupController : UIViewController
    {
        public override void LoadView()
        {
            View = new UIView(UIScreen.MainScreen.ApplicationFrame);
            View.BackgroundColor = UIColor.White;

            UIButton button = new UIButton();
            button.SetTitle("press me", UIControlState.Normal);
            button.Frame = new RectangleF(50, 50, 80, 80);
            button.TouchUpInside += delegate {
                LoginSucceeded();
            };
            button.BackgroundColor = UIColor.Purple;
            View.AddSubview(button);
        }


        public void LoginSucceeded()
        {
            AppSettings.IsLoggedIn = true;
            Console.WriteLine("This line causes multiple problems at random");
            DismissViewController(true, () => {});

        }
    }
}

It has two controllers the RootViewController and the LoginPopupController. The FinishedLoading method adds the RootViewController to the window. The RootViewController in the ViewDidAppear method checks if the app is logged in. If not, it will present the LoginPopupController.

The LoginPopupController has a button, which when pressed will set IsLoggedIn to true, and dismiss itself.

Basically, I want a login window to appear if the user isn’t logged in, and then dismiss itself after it has entered login details into a singleton settings object.


However, the app is very unreliable at the moment. Pressing the “press me” button can cause the following to happen at random:

  • Work as expected
  • Freeze the app
  • Crash the app
  • Do nothing on first press, crash the app on the second press

The Console.WriteLine line has a big effect on this – if you remove it the code succeeds more often (but not always).

Can anyone figure out what is causing this problem? It seems like a race condition (as the results change between runs), however I can’t figure out what could be causing that.

I’m running the code on the simulator using iOS 5.1. I have MonoTouch version 5.2.10 installed with Mono 2.10.8.

  • 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-05-31T23:09:11+00:00Added an answer on May 31, 2026 at 11:09 pm

    I have done two changes in your code and it never crashed, at least for my number of tries.

    First, move the UIButton declaration to the class:

    UIButton button;
    public override LoadView()
    {
      //...
    }
    

    Second, initialize the button with the UIButton.FromType(UIButtonType) static method:

    button = UIButton.FromType(UIButtonType.Custom);
    

    The UIButton() constructor had problems in previous versions of MonoTouch. In some versions it was not even available. I could not find anything specific for its current state, however creating buttons with the static method is the normal way to go, according to Apple docs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code example to demonstrate a simple MonoTouch.Dialog table using MessageElement
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I am trying to build a sample app using Monotouch. I have this configuration/setup
I have following table it shows like this: ABC A B C 123 Hello
Using AdMob in MonoTouch Projects with iOS SDK 4.2 We have a test project
I have following batch file code: @echo off SET INSTALL_PATH=c:\program files\ :ask_again if exist
I have a MonoTouch project using some code I share with a Windows Phone
I have a MonoTouch 5 app I have written. I am using a custom
I have an iPhone Windows-based Monotouch application that appears to use more memory to
I have been trying to use protobuf-net with MonoTouch but I have no idea

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.