添加子视图 - Monotouch
我有一个 MonoTouch 选项卡视图应用程序。在我的一个选项卡上,当用户单击按钮时,我想显示另一个视图。我使用以下代码执行此操作:
UIView.BeginAnimations("flip");
UIView.SetAnimationDuration(1);
UIView.SetAnimationTransition(UIViewAnimationTransition.FlipFromRight, View, true);
NewFilesViewController newFilesViewController = new NewFilesViewController();
newFilesViewController.View.Frame = new System.Drawing.RectangleF(View.Frame.Top, this.View.Frame.Left, this.View.Frame.Width, this.View.Frame.Height);
View.AddSubview(newFilesViewController.View);
UIView.CommitAnimations();
在新视图上,当我单击按钮时,出现错误:
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
我应该将视图添加到窗口吗?有更好的方法吗?
I have a MonoTouch tab view application. On one of my tabs, when a user clicks a button, I want to show another view. I do this with the following code:
UIView.BeginAnimations("flip");
UIView.SetAnimationDuration(1);
UIView.SetAnimationTransition(UIViewAnimationTransition.FlipFromRight, View, true);
NewFilesViewController newFilesViewController = new NewFilesViewController();
newFilesViewController.View.Frame = new System.Drawing.RectangleF(View.Frame.Top, this.View.Frame.Left, this.View.Frame.Width, this.View.Frame.Height);
View.AddSubview(newFilesViewController.View);
UIView.CommitAnimations();
On the new view, when I click a button, I get an error:
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
Should I be adding the view to the window instead? Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能(代码没有足够的上下文来 100% 确定),因为代码后面的任何地方都没有引用 newFilesViewController 。因此,它可以在下次调用垃圾收集器 (GC) 时被处理。然而,除了视图之外,本机代码仍然存在,并且当它尝试调用(已处置的)实例时将会崩溃。
修复:将您的
newFilesViewController
局部变量提升为字段。这将使引用保持活动状态(只要类型的实例处于活动状态)并且 GC 不会收集它。This is likely (the code does not have enough context to be 100% sure) because
newFilesViewController
is not referenced anywhere later in the code. As such it can be disposed the next time the Garbage Collector (GC) is invoked. However the native code still except the view to exists and this will crash when it tries to call the (disposed) instance.Fix: Promote your
newFilesViewController
local variable to a field. That will keep the reference alive (as long as the type's instance is alive) and the GC won't collect it.