What should be passed into a :(NSString *)text - iphone

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'

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

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

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.

Related

Why am I getting exc_bad_access errors for objectAtIndex in my floodfill algorithm

I have a flood fill function:
-(void) fillArea :(int) fillNum x:(int) xSpot y:(int) ySpot
{
int gridValue = 1;
int gridCount = [theGrid count];
[[theGrid objectAtIndex:(xSpot+ySpot*120)] getValue:&gridValue];
if (gridValue != 0) {
return;
}
[theGrid replaceObjectAtIndex:(xSpot + ySpot*120) withObject:[NSNumber numberWithInt:fillNum]];
[self fillArea:fillNum x:(xSpot+1) y:(ySpot)];
[self fillArea:fillNum x:(xSpot+1) y:(ySpot-1)];
[self fillArea:fillNum x:(xSpot) y:(ySpot-1)];
[self fillArea:fillNum x:(xSpot-1) y:(ySpot-1)];
[self fillArea:fillNum x:(xSpot-1) y:(ySpot)];
[self fillArea:fillNum x:(xSpot-1) y:(ySpot+1)];
[self fillArea:fillNum x:(xSpot) y:(ySpot+1)];
[self fillArea:fillNum x:(xSpot+1) y:(ySpot+1)];
return;
}
theGrid is an NSMutableArray of ints (either a 0 or a 1). It is just a 1D array that simulates a 2D array by multiplying the ySpot by 120 (the width of my grid). I checked the gridCount and it is equal to 9600.
However, I get an exc_bad_access at [[theGrid objectAtIndex:(xSpot+ySpot*120)] getValue:&gridValue]. I check my xSpot and ySpot when this happens and I know that (xSpot+ySpot*120) < 9600 every time. So I know it's not that I'm trying to access an object who's index is outside my array.
Futhermore, in my tick function I ran the code:
int gVal = 1;
int gIndex = 0;
while (gIndex < [theGrid count]) {
[[theGrid objectAtIndex:gIndex] getValue:&gVal];
gIndex += 1;
}
I did not get an exc_bad_access error. Please help me figure out why I'm getting an exc_bad_access error.
EDIT:
I split [[theGrid objectAtIndex:(xSpot+ySpot*120)] getValue:&gridValue]; into:
id object = [theGrid objectAtIndex:(xSpot+ySpot*widthInGridSize)];
gridValue = [object intValue];
I still get exc_bad_access and it says it is on the line:
gridValue = [object intValue];
So I assume this means object has already been released? I don't understand how that's possible. I thought ints didn't need to be retained in any way since they're just ints. Also I thought adding an object to an array automatically retained it so why would my int get released.
In the debug section the value of object is said equal: (_NSCFNumber *) 0x005aec80 (int) 0
As per comments - this isn't actually an out of bounds issue, but rather the object you're pulling out of 'theGrid' is bogus (already released). Break that into multiple lines to confirm; and turn on "zombies" in your debug settings. Cheers!

unrecognized selector sent to issue

I am getting the following error:
- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] init];
label = [[UILabel alloc] initWithFrame:view.bounds];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [label.font fontWithSize:50.0f];
[view addSubview:label];
}
else
{
label = [[view subviews] lastObject];
}
//set label
label.text = (index == 0)? #"[": #"]";
return view;
}
-[SocialCatalogViewController carousel:placeholderViewAtIndex:]: unrecognized selector sent to instance 0x1296db80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SocialCatalogViewController carousel:placeholderViewAtIndex:]: unrecognized selector sent to instance 0x1296db80'
*** First throw call stack:
(0x2f3c052 0x30cdd0a 0x2f3dced 0x2f3e083 0x2ea30c9 0x2ea2ce2 0x2f1b25 0x2f1e6c 0x2f25b3 0x2f5863 0x2f1a44 0x2f2a50 0x2edd77 0x352e4b 0x143664e 0x1436941 0x144847d 0x144866f 0x144893b 0x14493df 0x1449986 0x14495a4 0x2e01b9 0x2ea1d4 0x2f3dec9 0x13735c2 0x137355a 0x1418b76 0x141903f 0x14182fe 0x1631a2a 0x2f109ce 0x2ea7670 0x2e734f6 0x2e72db4 0x2e72ccb 0x3349879 0x334993e 0x1370a9b 0x2e06 0x2d75 0x1)
terminate called throwing an exceptionCurrent language: auto; currently objective-c
Any idea?
Well, yes. The error indicates you're passing two parameters (for carousel and placeholderViewAtIndex). However, the method is actually carousel: placeholderViewAtIndex: reusingView:, i.e. taking three parameters. So, just pass one more for reusingView (or nil, as the method has logic to handle this case).

issue with UITapGestureRecognizer for a button

I've done the following thing:
buttonPlaylistView = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width *(urlList.count+1), 0, self.view.frame.size.width, self.view.frame.size.height)];
buttonPlaylistView.tag = 0;
UITapGestureRecognizer *doubleTap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap3 setNumberOfTapsRequired:2];
[buttonPlaylistView addGestureRecognizer:doubleTap3];
[doubleTap3 release];
-(void) handleDoubleTap:(UITapGestureRecognizer *)sender{
if(sender.state == UIGestureRecognizerStateEnded)
int x = [sender tag];
return;
}
But I get SIGAGRT at this line: int x = [sender tag]; saying:
[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x61280b0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x61280b0'
NOW:What's the problem and what's the solution for this?Thanks
-(void) handleDoubleTap:(UITapGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateEnded)
{
int x = [sender.view tag];
}
}
Will fix the issue.
An UITapGestureRecognizer does not have a property named tag - as you see, the sender you get is NOT the button. You have to access the buttonPlayListView directly, like
int x = [buttonPlayListView tag];
or otherwise remember which button you want to access.
Even though I'm quite sure that you're going about this the wrong way, adding a double tap gesture recognizer to a UIButton, there is a way you can still perform the task you require that shouldn't be too much work for you.
You've made the comment
and how could I remember if I create let say 100 buttons
to one of the answers, the one which highlights what the issue is that's causing your SIGBART. UIGestureRecognizer does not have a tag property.
Here's what you could do, is to iterate through all the subviews of your [self view] and find the one that has the same UIGestureRecognizer, it's not the prettiest solution, and the more subview's you have the longer the loop will take. But it'll do what you seem to be looking for, so if you're adding .
In your handleDoubleTap function you could do the following
-(void) handleDoubleTap:(UITapGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateEnded)
{
int iButtonTag = -1 //This is important later to escape the below for loop as we don't need to needlessly go around in circles
for(UIView* psubView in [[self view] subviews])
{
if( [psubView isKindOfClass:[UIButton class]] )
{
UIButton* pButton = (UIButton*)psubView;
for(UIGestureRecognizer* pGesture in [pButton gestureRecognizers] )
{
if( pGesture == sender )//this is the button we're after
{
iButtonTag = [pButton tag];
break;
}
}
if( iButton != -1 )//found what we came for
{
break;
}
}
}
//do what ever it was you needed to do now that you have the views tag, or you could have kept a reference to the button etc.
}
}
That should solve your problem. Alternatively if you're going to be adding buttons to subviews of subviews it would be better to keep track of your UIButtons in an NSMutableArray , you would do this by creating a class property (or member variable) and adding the buttons to this using the 'addObject:' function of NSMutableArray. Then instead of the line
for(UIView* psubView in [[self view] subviews])
above you could exchange that for
for( UIButton* pButton in m_pMutableButtonArray )
where "m_pMutableButtonArray" is the variable name you gave to your NSMutableArray you were storing the UIButtons in. This also means you would do away with the following if isKindOfClass test on the following line.
That should fix your problem.
Why are you putting a UITapGestureRecognizer in a button? The button already handles that for you and will send you a callback, you can add a target to a button using this UIControl method
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

how to get local top 10 highscore from openfeint

this is my coding am used to get local top 10 highscore but , debugging terminated error occurs.
[OFHighScoreService getPageWithLoggedInUserForLeaderboard: theLeaderboardID onSuccess:OFDelegate(self, #selector(_scoresDownloaded:))
onFailure:OFDelegate()];
selector:-
- (void)_scoresDownloaded:(OFPaginatedSeries*)page
{
NSMutableArray* highscores = nil;
if ([page count] > 0)
{
if ([[page objectAtIndex:0] isKindOfClass:[OFTableSectionDescription class]])
{
highscores = [(OFTableSectionDescription*)[page objectAtIndex:0] page].objects;
}
else
{
highscores = page.objects;
}
}
NSString *userID = [OpenFeint lastLoggedInUserName];
for (OFHighScore* score in highscores)
{
ccColor3B theColor = ccBLACK;
if ([score.user.name isEqualToString: userID] ) {
//score now contains the users data... Do what I want with it.
NSLog(#"%d %# %d", score.rank, score.user.name, score.score);
break;
}
}
}
this is my console window error:-
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Levelone canReceiveCallbacksNow]: unrecognized selector sent to instance 0x6af2070'
*** Call stack at first throw:
terminate called after throwing an instance of 'NSException'
As the error says, the object you are using as the callback delegate for OFHighScoreService does not recognize the selector canReceiveCallbacksNow. As per OpenFeint documentation, your callback must implement the OFCallbackable protocol which defines this. Simply implement the function, e.g. just have it return YES all the time.
OpenFeint only stores the latest qualifying score per player on a given leaderboard. No user would ever appear ranked at more than one slot on a leaderboard.

EXC_BAD_ACCESS when removeAllObject

I have a class named UICustomLabel with a variable NSMutableString : _text . I created a lot of this objects to display the content as page. Here is code to create :
for (int i = 0; i < linesOfFirstPage; i++) //first page is the special case
{
UICustomLabel * _centerLabel =[[UICustomLabel alloc] initWithFrame:CGRectMake(100.0, 555 + 35 * i, 575.0, 35.0)];//create label in follow kPortraitIpadfirstLabelRect area
if (!_isSmallFont) _centerLabel.frame = CGRectMake(100, 505 + 45 * i, 575, 45);
_centerLabel._textColor=kLabelTextColor;
_centerLabel._backgroundColor=kBackgroundColor;
_centerLabel._font=[UIFont systemFontOfSize:isSmallFont?25:32];
[_contentOfPage addObject:_centerLabel];
[self addSubview:_centerLabel];
[_centerLabel release];
}
After that when i want to remove all Label in my page :
if (_contentOfPage)
{
for (int i = 0; i < [_contentOfPage count]; i++)
{
UICustomLabel * tmp = [_contentOfPage objectAtIndex:i];
[tmp removeFromSuperview];
}
[_contentOfPage removeAllObjects];
}
But i get an error EXC_BAD_ACCESS when command [_contentOfPage removeAllObjects] excuted; When i reject the command [_text release] in the method dealloc of UICustomLabel, the program running fine. So what was the reason?