gpt4 book ai didi

c# - DismissViewController 有时会导致程序卡死或崩溃

转载 作者:行者123 更新时间:2023-11-29 11:15:04 26 4
gpt4 key购买 nike

我有以下 MonoTouch 程序:

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, () => {});

}
}
}

它有两个 Controller RootViewController 和 LoginPopupController。 FinishedLoading 方法将 RootViewController 添加到窗口。 ViewDidAppear 方法中的 RootViewController 检查应用程序是否已登录。如果没有,它将显示 LoginPopupController。

LoginPopupController 有一个按钮,按下该按钮会将 IsLoggedIn 设置为 true,然后自行关闭。

基本上,如果用户未登录,我希望出现一个登录窗口,然后在将登录详细信息输入单例设置对象后自行关闭。


但是,该应用程序目前非常不可靠。按下“按我”按钮可能会随机发生以下情况:

  • 按预期工作
  • 卡住应用
  • 使应用程序崩溃
  • 第一次按下时什么都不做,第二次按下时应用程序崩溃

Console.WriteLine 行对此有很大影响 - 如果您删除它,代码会更频繁地成功(但并非总是如此)。

谁能找出导致此问题的原因?这似乎是一种竞争条件(因为结果在运行之间发生变化),但我无法弄清楚是什么导致了这种情况。

我正在使用 iOS 5.1 在模拟器上运行代码。我安装了 MonoTouch 版本 5.2.10 和 Mono 2.10.8。

最佳答案

我已经对您的代码进行了两次更改并且它从未崩溃,至少在我尝试的次数中是这样。

首先,将 UIButton 声明移动到类中:

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

其次,使用 UIButton.FromType(UIButtonType) 静态方法初始化按钮:

button = UIButton.FromType(UIButtonType.Custom);

UIButton() 构造函数在以前版本的 MonoTouch 中存在问题。在某些版本中,它甚至不可用。根据 Apple 文档,我找不到任何特定于其当前状态的内容,但是使用静态方法创建按钮是正常的方法。

关于c# - DismissViewController 有时会导致程序卡死或崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9834868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com