user3621223
user3621223

Reputation: 1

Test App in Xcode Crashes on click of a button, but not a switch

I'm making a test application in Xcode to learn objective-c, but when I click on the button in my app, it crashes.

My Viewcontroller.h

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController{
        IBOutlet UILabel *myLabel;
        UIButton *myButton;
        UISwitch *mySwitch;
        BOOL switched;


    }

    @property (nonatomic, retain)IBOutlet UILabel *myLabel;
    @property (nonatomic, retain)IBOutlet UIButton *myButton;
    @property (nonatomic, retain)IBOutlet UISwitch *mySwitch;

    -(IBAction)buttonClick:(id)sender;
    -(IBAction)switchMoved:(id)sender;


    @end

My Viewcontroller.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize myButton;
@synthesize myLabel;
@synthesize mySwitch;

- (IBAction)buttonClick:(id)sender {
    [myLabel setTextColor:[UIColor blueColor]];
    sleep(1);
    [myLabel setTextColor:[UIColor redColor]];
}

-(IBAction)switchMoved:(id)sender{
    if(switched == TRUE){
        [myLabel setTextColor:[UIColor blueColor]];
        switched = FALSE;
    }
    else{
        [myLabel setTextColor:[UIColor redColor]];
        switched = TRUE;
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [myLabel setText:@"This is a label"];
    [myLabel setTextColor:[UIColor redColor]];
    switched = TRUE;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

The app crashes when I click the button in the simulator, but not when I switch the switch back and forth. Why is this happening? All of the outlets are connected correctly.

Upvotes: 0

Views: 457

Answers (2)

Justin Cabral
Justin Cabral

Reputation: 585

I use GCD instead of perform selector but either should work. He's my implementation using GCD. You should never use sleep(); for UI changes. Either animation, perform selector, or GCD.

-(void)buttonClicked:(UIButton *)sender
{
   [self setTextColor:[UIColor blueColor] forState:UIControlEventTouchUpInside];

    dispatch_time_t waitTime = dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0 * NSEC_PER_SEC));

dispatch_after(waitTime, dispatch_get_main_queue(), ^(void)
{  
  [self setTextColor:[UIColor redColor] forState:UIControlEventTouchUpInside] 
});
}

Upvotes: 1

johankj
johankj

Reputation: 1767

Instead of using sleep(1), which blocks the main thread and thus any UI-interaction/updates (as @Martin H also said in the comments), try to use performSelector:withObject:afterDelay: which will run the method given after a delay.

Here's an example:

- (IBAction)buttonClick:(id)sender {
    [myLabel setTextColor:[UIColor blueColor]];
    [self performSelector:@selector(changeTextColor:) withObject:nil afterDelay:2];

}

- (void)changeTextColor:(id)sender {
    [myLabel setTextColor:[UIColor greenColor]];
}

// Edit: Oh… I'm posting this because I (as others) can't seem to find anything wrong with the code. Perhaps a wrong IB-binding. Perhaps you need to reset the the simulator.

Upvotes: 2

Related Questions