iOS应用程序崩溃,当我点击我的UIButton

本文关键字:我的 UIButton 应用程序 崩溃 iOS | 更新日期: 2023-09-27 18:37:14

摘要

这是我尝试使用 Xamarin 制作的第一个应用程序。我正在从Hello World, iPhone的例子中学习。当我单击我的按钮时,应用程序崩溃,我不知道为什么。

我有一个简单的UIViewController,只有一个UIButton.我创建了一个出口,所以当我点击按钮时,我会做一些事情。

当我连接按钮的TouchUpInside event时,当我单击按钮时,应用程序崩溃。当我删除有线事件(但插座仍然存在)时,当我单击按钮时,应用程序不会崩溃。(当然,什么都不会发生,因为没有连接)。

让我沮丧的是,我不知道出了什么问题,使用调试信息,我无法弄清楚!我已经做 C# 代码多年了,但刚开始使用 Xam 有点不同,尤其是当崩溃报告缺少任何信息时。我猜我可能勾选了一些设置(例如,没有调试信息?

所以这里有一些代码...

身份验证视图控制器
.h 文件

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface AuthenticationViewController : UIViewController {
UIButton *_Google;
}
@property (nonatomic, retain) IBOutlet UIButton *Google;
@end

.m 文件

#import "AuthenticationViewController.h"
@implementation AuthenticationViewController
@synthesize Google = _Google;
@end

设计器文件

using MonoTouch.Foundation;
using System.CodeDom.Compiler;
namespace Foo.Client.iPhone
{
    [Register ("AuthenticationViewController")]
    partial class AuthenticationViewController
    {
        [Outlet]
        MonoTouch.UIKit.UIButton Google { get; set; }
        void ReleaseDesignerOutlets ()
        {
            if (Google != null) {
                Google.Dispose ();
                Google = null;
            }
        }
    }
}

最后,我的

.cs文件

using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Foo.Client.iPhone
{
    public partial class AuthenticationViewController : UIViewController
    {
        public int xxx = 0;
        public event EventHandler OnAuthenticationCompleted;
        public AuthenticationViewController() 
            : base ("AuthenticationViewController", null)
        {
        }
        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            Google.TouchUpInside += (sender, e) => 
            {
                xxx++;
            };
        }
    }
}

这是(缺少)故障转储/堆栈跟踪

2013-10-25 21:37:13.785 FooClientiPhone[8852:1403] MonoTouch: Socket error while connecting to MonoDevelop on 127.0.0.1:10000: Connection refused
mono-rt: Stacktrace:
mono-rt:   at <unknown> <0xffffffff>
mono-rt:   at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
mono-rt:   at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
mono-rt:   at Foo.Client.iPhone.Application.Main (string[]) [0x00008] in /Users/PureKrome/Documents/Mac Projects/Foo iOS Client/Code/Foo.Client.iPhone/Main.cs:16
mono-rt:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
mono-rt: 
    Native stacktrace:
 mono-rt: 
    =================================================================
    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.
            =================================================================

我被困住了,不知道如何继续?

此外,这是一个显示错误的视频(请选择720p版本)。

这是解决方案,压缩。

iOS应用程序崩溃,当我点击我的UIButton

从xCode的角度来看,我认为IBOutlet导致您的应用程序崩溃。仅仅因为IBOutlet用于状态而不是调用方法。在这种情况下,您应该将按钮连接到 IBAction:

- (IBAction)Google:(id)sender; 

最终将调用:

Google.TouchUpInside += (sender, e) => 
            {
                xxx++;
            };

另外,请注意,在现代objective-c中,你不需要@synthesize Google = _Google;它会自动发生。

快乐的x(阿玛林)编码。

这告诉我们也许我们正在失去对某些东西的引用。我知道有人已经说过要启用僵尸,但这可能不是您的包装器/编译器的选项。

mono-rt:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

尝试更换:

@interface AuthenticationViewController : UIViewController {
    UIButton *_Google; 
}
@property (nonatomic, retain) IBOutlet UIButton *Google;

只需:

@interface AuthenticationViewController : UIViewController 
@property (nonatomic, retain) IBOutlet UIButton *Google;

当您单击该按钮时,编译器可能不知道您在说什么。

你为一个简单的hello world编写了很多代码

这是你不需要的

@property (nonatomic, retain) IBOutlet UIButton *Google;
@synthesize Google = _Google;
using  MonoTouch . Foundation ; 
using  System . CodeDom . Compiler ; 
namespace  Foo . Client . iPhone
{ 
[ Register  ( "AuthenticationViewController" )] 
partial  class  AuthenticationViewController 
{ 
    [ Outlet ] 
    MonoTouch . UIKit . UIButton  Google  {  get ;  set ;  } 
    void  ReleaseDesignerOutlets  () 
    { 
        if  ( Google  !=  null )  { 
            Google . Dispose  (); 
            Google  =  null ; 
        } 
       } 
    } 
   }
  using System;
  using System.Drawing;
  using MonoTouch.Foundation;
  using MonoTouch.UIKit;
  namespace Foo.Client.iPhone
 {
 public partial class AuthenticationViewController : UIViewController
 {
    public int xxx = 0;
    public event EventHandler OnAuthenticationCompleted;
    public AuthenticationViewController() 
        : base ("AuthenticationViewController", null)
    {
    }
    public override void DidReceiveMemoryWarning()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        Google.TouchUpInside += (sender, e) => 
        {
            xxx++;
        };
    }
   }
   } 2013-10-25 21:37:13.785 FooClientiPhone[8852:1403] MonoTouch: Socket error while           connecting to MonoDevelop on 127.0.0.1:10000: Connection refused
    mono-rt: Stacktrace:
   mono-rt:   at <unknown> <0xffffffff>
  mono-rt:   at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain         (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
  mono-rt:   at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  mono-rt:   at Foo.Client.iPhone.Application.Main (string[]) [0x00008] in /Users/PureKrome/Documents/Mac Projects/Foo iOS Client/Code/Foo.Client.iPhone/Main.cs:16
mono-rt:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object          (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
   mono-rt: 
   Native stacktrace:
  mono-rt: 
   =================================================================
    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.
        =================================================================

所以如果你用按钮做一个你好世界 不需要任何这个

要做一个你好世界只需要这个

@interface ViewController ()

@property (nonatomic, strong) UILabel *myLabel;

@implementation ViewController
- (IBAction)myButton:(id)sender 
{ 
  _myLabel.Text = @"Hello World ";
}
@end