Abushawish
Abushawish

Reputation: 1473

ObjectiveC setting image when thread finished

I have a button which I'm trying to set the image for when a thread is finished. My code may seem useless to just change the image button but this is for practise and going onto a bigger thing.

Goal: Set the image for the b0 as button_x.png (found in the project).

Issue: The button image is not changing, if I make a connection between the button found on the storyboard to the viewController button declaration I get a crash with -[UIButton setImage:]: unrecognized selector sent to instance 0x8b4c270.

Thank you.

ViewController.h:

@interface ViewController : UIViewController {
    IBOutlet UIButton *b0;
    Game *instanceOfGame;
}

@property (nonatomic, strong) UIButton *b0;
@property (nonatomic, strong) Game *instanceOfGame;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize instanceOfGame;
@synthesize b0;
static NSString *postName = @"123";


- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(post:) name:postName object:nil];
    instanceOfGame = [[Game alloc] init];
    [instanceOfGame start];
}

- (void) post: (NSNotification *) result {
    NSNumber *str = [result object];
    if (str == 0) {
        [self.b0 performSelectorOnMainThread:@selector(setImage: ) withObject:[UIImage imageNamed:@"button_x.png"] waitUntilDone:NO];
    }

}

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

@end

Game.h:

#import <Foundation/Foundation.h>

    @interface Game : NSObject
    @property (nonatomic, strong) NSNumber *choice;
    @property (nonatomic, strong) NSNotificationCenter *ns;
    -(void) start;
    @end

Game.m:

#import "Game.h"

@implementation Game

NSNotificationCenter *ns;
@synthesize ns;
@synthesize choice;
static NSString *postName = @"123";

-(id) init {
    if (self = [super init]) {
        ns = [NSNotificationCenter defaultCenter];
    }
    return self;
}

-(void) start {
    [self performSelectorInBackground:@selector(loadData) withObject:Nil];
}

-(void) loadData {
    choice = 0;

    [ns postNotificationName:postName object:choice];
}

@end

Upvotes: 0

Views: 231

Answers (3)

Suyog Patil
Suyog Patil

Reputation: 1620

@Colin Cornaby saying write.

UIButton doesn't have setImage method.

You can also use the same existing code. just update below line

[self.b0 performSelectorOnMainThread:@selector(setImage: ) withObject:[UIImage imageNamed:@"button_x.png"] waitUntilDone:NO];

as

[self performSelectorOnMainThread:@selector(setImage: ) withObject:[UIImage imageNamed:@"ListDropDown.png"] waitUntilDone:NO];

and then define method setImage:

-(void)setImage:(UIImage *)img
{
    [self.bo setImage:img forState:UIControlStateNormal];
}

I hope it will work for you

Upvotes: 2

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try this,

if (str == 0) {
    dispatch_sync(dispatch_get_main_queue(), ^{
        [self.b0 setImage:[UIImage imageNamed:@"button_x.png"] forState:UIControlStateNormal];       
    });
}

Upvotes: 1

Colin Cornaby
Colin Cornaby

Reputation: 746

UIButton doesn't have a setImage: function, hence the exception being thrown.

It does have this function though:

- (void)setImage:(UIImage *)image forState:(UIControlState)state

Try replacing:

[self.b0 performSelectorOnMainThread:@selector(setImage: ) withObject:[UIImage imageNamed:@"button_x.png"] waitUntilDone:NO];

With a similar sort of implementation done using GCD calling the correct function:

dispatch_async(dispatch_get_main_queue(), ^{
   [self.b0 setImage:[UIImage imageNamed:@"button_x.png"] forState:UIControlStateNormal];
});

Upvotes: 3

Related Questions