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

4

1 回答 1

2

试试这个代码...

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

或者你也可以UIColor像下面这样设置。

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

在这里,您UIButtons在每个单元格中添加,然后尝试像下面这样定义,而不是每次都分配和自动释放.....

   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

*更新 *查看我在每个单元格中添加的示例,其中包含多个按钮,例如 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];
于 2013-04-25T11:01:26.563 回答