views:

41

answers:

3

Hi guys,

Feeling a little confused. I am trying to pass a NSString as an argument to this method

-(void) setRightLabelText:(NSString *)text { rightLabel.text = text; }

The code i use to call the method

for(int index=0; index<5; index++)
{
    NSNumber *num = [card.statsArray objectAtIndex:index];

    StatView *statView = (StatView *)[self.frontView viewWithTag:10+index];
    NSString *nameHolder = @"test";
    [statView setRightLabelText:nameHolder];
}

The code I used to create the View :

for(int i=0; i<totalButtons; i++) 
{       
    StatView *sv = [[StatView alloc] initWithYPos:ypos];
    sv.tag = 100 + i;
    [sv.overlayButton addTarget:self action:@selector(statTapped:)  
            forControlEvents:UIControlEventTouchUpInside];
    sv.overlayButton.tag = 10 + i;
    [self.frontView addSubview:sv];
    ypos += 26;
}

This to me looks perfect, but i get a crash when I get to this method call in the app. Error Msg : -[UIButton setRightLabelText:]: unrecognized selector sent to instance 0x5d116e0 2010-09-13 11:39:44.761 LeinsterRugby[1387:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setRightLabelText:]: unrecognized selector sent to instance 0x5d116e0'

+1  A: 

StatView *statView = (StatView *)[self.frontView viewWithTag:10+index]; returns an UIButton instead of a StatView because the tag matches the one you assign to the button with sv.overlayButton.tag = 10 + i;.

tob
You are right tob. Thanks very much. I really should have noticed that. Thanks everybody for the help
Code
+1  A: 

It looks as though you're sending the setRightLabelText message (selector) to an instance of UIButton rather than an object of the type that you're implementing.

Rather than:

[statView setRightLabelText:nameHolder];

Do you perhaps mean:

[self setRightLabelText:nameHolder];
Stephen Darlington
A: 

Probable reason for the crash would be

StatView *statView = (StatView *)[self.frontView viewWithTag:10+index];

Your computation to get the StatView from (10+index) is wrong. Calculate your index properly.

Satyam svv