Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS app crashes when I click on my UIButton

Summary

This is my first app I'm trying to make with Xamarin. I'm learning from the Hello World, iPhone example. When I click on my button, the app crashes and I can't figure out why.

Details

I've got a simple UIViewController with a single UIButton. I've created an outlet so when I click on the button, I do something.

When I wire up the button's TouchUpInside event, the app crashes when I click on the button. When I remove the wired up event (but the outlet still exists), the app doesn't crash when I click on the button. (Of course, nothing happens because nothing is wired up).

What frustrates me is that I have no idea what is wrong and using the debug info, I can't figure it out! I've been doing C# code for years now but just starting in Xam is a bit different, especially when the crash report is lacking any information. I'm guessing I might have some settings ticked off (eg. no debug info?)

So here's some code...

AuthenticationViewController
.h file

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


@interface AuthenticationViewController : UIViewController {
UIButton *_Google;
}

@property (nonatomic, retain) IBOutlet UIButton *Google;

@end

.m file

#import "AuthenticationViewController.h"

@implementation AuthenticationViewController

@synthesize Google = _Google;

@end

designer file

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;
            }
        }
    }
}

and finally, my

.cs file

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++;
            };
        }
    }
}

Here is the (lacking) crash dump / stack trace

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.
            =================================================================

I'm stuck and have no idea how to continue?

Further more, here's a video showing the error (please select the 720p version).

And here's the solution, zipped up.

like image 348
Pure.Krome Avatar asked Oct 28 '13 02:10

Pure.Krome


1 Answers

Looking at this from an xCode point of view I think that the IBOutlet is causing you app to crash. Simply because IBOutlets are used for state rather than for calling methods. In this case, you should wire the button to a IBAction:

- (IBAction)Google:(id)sender; 

which will eventually call:

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

Also, note that in modern objective-c you don't need @synthesize Google = _Google; that will happen automatically.

happy x(amarin)Coding.

like image 88
carlodurso Avatar answered Oct 06 '22 01:10

carlodurso