1

I am developing application like Instagram. I have code for cellForRowAtIndexPath, every time it creates object for unbutton and it is autorelease. this button display username and it may be 1,2,3,4,5,.. total like users. there for i create UIButton on depend on like cout. I am not using ARC.

but it crash sometimes (Not everyTime) when setColour.

I enabled NSZombie and it keeps saying:

[UIDeviceRGBColor set]: message sent to deallocated instance 0x21b526f0

            UIButton *btnLikeUserName = [[[UIButton alloc] init] autorelease];
                [btnLikeUserName.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];

                if (posx + size.width > 280) {
                    posy = posy + 22;
                    posx = 18;
                    btnLikeUserName.frame = CGRectMake(posx,posy,size.width,size.height);
                    posx = posx + size.width + 5;
                }
                else {
                    btnLikeUserName.frame = CGRectMake(posx,posy,size.width,size.height);
                    posx = posx + size.width + 5;
                }
                btnLikeUserName.tag = shareObjU.userId;
                [btnLikeUserName setTitle:text forState:UIControlStateNormal];
                [btnLikeUserName setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal]; ////Hear is crash
                [btnLikeUserName addTarget:self action:@selector(btnLikeUserNameClick:) forControlEvents:UIControlEventTouchUpInside];
                [cell.viewLikeComment addSubview:btnLikeUserName];

i also see following link

UISwitch setThumbTintColor causing crash (iOS 6 only)?

1 out of 4 colors return errors

iOS App crashes when using colorWithRed:green:blue:alpha

and other .. but all says for global Objct. not local object. how can i solved it. Thank You

Community
  • 1
  • 1
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45

1 Answers1

2

try this code...

[btnLikeUserName setTitleColor:[UIColor colorWithRed:((float) 50 / 255.0f)  
                           green:((float) 79 / 255.0f)  
                            blue:((float) 133 / 255.0f)  
                           alpha:1.0f] forState:UIControlStateNormal];

OR Also you can set UIColor like bellow.

[btnLikeUserName setTitleColor:[UIColor colorWithRed:50.0f/255.0f green:79.0f/255.0f blue:133.0f/255.0f alpha:1.0] forState:UIControlStateNormal];

And Here you Add UIButtons in every cell then try to define like bellow not alloc and autorelease everytime .....

   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

*UPDATE:*See the example which i add in my every cell with multiple button like GridView

 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 button.frame = rect;
 button.tag = imageIndex;
 [button addTarget:self  action:@selector(btnTemp_Clicked:)
                 forControlEvents:UIControlEventTouchDown];

 [button setTitle:[arrTime objectAtIndex:imageIndex] forState:UIControlStateNormal];
 [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];

 //[button setTitleColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"circle03.jpg"]] forState:UIControlStateNormal ];                
 [button setTitleColor:[UIColor colorWithRed:50.0f/255.0f green:79.0f/255.0f blue:133.0f/255.0f alpha:1.0] forState:UIControlStateHighlighted ];

 [button setNeedsDisplay];

 [gridCell.contentView addSubview:button];
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • but error is , message sent to deallocated instance, i think my btnLikeUserName is deallocated. am i right? – SAMIR RATHOD Apr 25 '13 at 11:03
  • @SAMIRRATHOD here this type of error occured with UIDeviceRGBColor message when its type of parameter not matches with its method.. try this and let me know.. :) – Paras Joshi Apr 25 '13 at 11:05
  • ok, i try it and i tell you what is the status. Thanks for answer – SAMIR RATHOD Apr 25 '13 at 11:07
  • @SAMIRRATHOD also define that button like this UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; – Paras Joshi Apr 25 '13 at 11:08
  • i update code plzz check that and try it... also try to add this line after set color and everything [button setNeedsDisplay]; – Paras Joshi Apr 25 '13 at 11:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28889/discussion-between-samir-rathod-and-paras-joshi) – SAMIR RATHOD Apr 25 '13 at 12:00
  • ya but some time its differ with architecture.. :) – Paras Joshi Apr 25 '13 at 12:08
  • I'm sorry, how does this possibly answer the question? – maroux Apr 25 '13 at 17:46
  • @Mar0ux hey here some time with UIColor its required to pass float value and value between 1 to 0 .. and other possiblility i post the code of UIButton with its type not alloc and autoreleaser define... :) here its occure when button color not retain and we assign to setColor to the button... see this link mate http://stackoverflow.com/questions/11318138/ios-app-crashes-when-using-colorwithredgreenbluealpha :) – Paras Joshi Apr 26 '13 at 03:49
  • But OP is using a standard UIButton which _will_ retain UIColor instance. – maroux Apr 26 '13 at 07:41
  • but see above link dude which explain whole region in deep ... :) – Paras Joshi Apr 26 '13 at 07:42