views:

41

answers:

5

How to debug iPhone aplication? How can i find out what's going on in simulator? i'm beginner in xcode development and don't know what's problem with code below.. the app is just crashing on button click..

- (void)viewDidLoad {

    myLabel = [[UILabel alloc]init];

    [myLabel setText:@"Labela"];
    myLabel.frame = CGRectMake(50.0,50.0, 100.0, 30.0);
    [self.view addSubview:myLabel];

    myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [myButton addTarget:self 
                 action:@selector(buttonAction:)
       forControlEvents:UIControlEventTouchDown];

    [myButton setTitle:@"Klikni" forState:UIControlStateNormal];
    [myButton setFrame:CGRectMake(80.0, 210.0, 160.0, 40.0)];

    [self.view addSubview:myButton];
    [super viewDidLoad];

}

- (void)buttonAction{
    [myLabel setText:@"Promijenjen!"];
}
+3  A: 
action:@selector(buttonAction:)

Here you specify that buttonAction selector gets 1 parameter, but it is declared to have none:

- (void)buttonAction{
...

So when button click system tries to call undefined method and that results in crash. To fix that you should either change selector name to

action:@selector(buttonAction)

or change action method declaration:

- (void)buttonAction:(id)sender{
    ...
Vladimir
A: 

Try this.

In all seriousness, apple has a fairly decent guide for debugging using XCode.

Stephen Furlani
+1  A: 

Alt+Click "Build & Run" button to debug. Click "Show console" button. Use NSLog and breakpoints. Try declaring: -(void) buttonAction:(id) sender;

Martha
+1  A: 

If you are a beginner, you should start with a tutorial or better, a good book about the subject.

You can output a message to the console using NSLog(@"My variable value: %@", myVariable);

You can use Debugging, line by line, just add a breakpoint anywhere in the code and Run as Debug.

balexandre
A: 

To fix this problem, change

action:@selector(buttonAction:)

To

action:@selector(buttonAction)

For general debugging, try readin Apple debugging guide, which can be found here: http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/XcodeDebugging/000-Introduction/Introduction.html

You are going to want learn about using the debugger (Cmd +Shift + Y) and the console, accessible with the keyboard shortcut, Cmd +Shift + R.

Good luck!

Moshe