I am trying to create a page of UISliders. The amount of sliders is dynamic.
I have a PageViewController class and when I put the UISliders in a subview of PageViewController the sliders work.
I would like to encapsulate the sliders into a UIView subclass so I can save their state/change coupled labels, etc. However when I do it the sliders stop working. I've tried a few things and am at the point to where I think I may just be missing a fundamental understanding that hopefully someone can point out.
This works (from PageViewController.m):
if ([[controlData type] isEqualToString:#"SliderInt"])
{
NSDictionary *data = controlData.data;
NSLog(#"build SliderInt %# %# %#", [controlData.data objectForKey:#"min"], [controlData.data objectForKey:#"max"], [controlData.data objectForKey:#"value"]);
CGRect frame = CGRectMake(startX, startY, 200.0, 22.00);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;
slider.value = 25.0;
CGRect labelFrame = CGRectMake(startX, startY+22.0, 100.0, 30.0);
UILabel *minLabel = [[UILabel alloc] initWithFrame:labelFrame];
[minLabel setFont:[UIFont systemFontOfSize:12]];
minLabel.textAlignment = UITextAlignmentLeft;
[minLabel setText:#"minLabel:"];
labelFrame = CGRectMake(startX+200.0, startY+22.0, 100.0, 30.0);
UILabel *maxLabel = [[UILabel alloc] initWithFrame:labelFrame];
[maxLabel setFont:[UIFont systemFontOfSize:12]];
maxLabel.textAlignment = UITextAlignmentLeft;
[maxLabel setText:#"maxLabel:"];
labelFrame = CGRectMake(startX+220.0, startY+0.0, 100.0, 30.0);
UILabel *valueLabel = [[UILabel alloc] initWithFrame:labelFrame];
[valueLabel setFont:[UIFont systemFontOfSize:12]];
valueLabel.textAlignment = UITextAlignmentLeft;
[valueLabel setText:#"valueLabel:"];
minLabel.text = [data objectForKey:#"min"];
maxLabel.text = [data objectForKey:#"max"];
valueLabel.text = [data objectForKey:#"value"];
slider.minimumValue = [[data objectForKey:#"min"] intValue];
slider.maximumValue = [[data objectForKey:#"max"] intValue];
slider.value = [[data objectForKey:#"value"] intValue];
//
[contentView addSubview:slider];
[contentView addSubview:minLabel];
[contentView addSubview:maxLabel];
[contentView addSubview:valueLabel];
[slider release];
[minLabel release];
[maxLabel release];
[valueLabel release];
startY+=60;
}
if ([[controlData type] isEqualToString:#"SliderFloat"])
{
NSLog(#"build SliderFloat");
}
However moving it into this class breaks it
#interface SliderIntView : UIView {
UILabel *minLabel;
UILabel *maxLabel;
UILabel *valueLabel;
UISlider *slider;
NSDictionary *data;
}
#property(nonatomic, retain) UISlider *slider;
#property(nonatomic, retain) UILabel *minLabel;
#property(nonatomic, retain) UILabel *maxLabel;
#property(nonatomic, retain) UILabel *valueLabel;
#property(nonatomic, retain) NSDictionary *data;
- (id)initWithFrame:(CGRect)frame withData:(NSDictionary *)controlData;
-(void) sliderAction:(id) sender;
#end
The implementation
#import "SliderIntView.h"
#implementation SliderIntView
#synthesize slider, minLabel, maxLabel, valueLabel, data;
- (id)initWithFrame:(CGRect)frame withData:(NSDictionary *) controlData {
self = [super initWithFrame:frame];
if (self) {
self.data = controlData;
float startX = frame.origin.x;
float startY = frame.origin.y;
slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
CGRect labelFrame = CGRectMake(startX, startY+22.0, 100.0, 30.0);
minLabel = [[UILabel alloc] initWithFrame:labelFrame];
[minLabel setFont:[UIFont systemFontOfSize:12]];
minLabel.textAlignment = UITextAlignmentLeft;
[minLabel setText:#"minLabel:"];
labelFrame = CGRectMake(startX+200.0, startY+22.0, 100.0, 30.0);
maxLabel = [[UILabel alloc] initWithFrame:labelFrame];
[maxLabel setFont:[UIFont systemFontOfSize:12]];
maxLabel.textAlignment = UITextAlignmentLeft;
[maxLabel setText:#"maxLabel:"];
labelFrame = CGRectMake(startX+220.0, startY+0.0, 100.0, 30.0);
valueLabel = [[UILabel alloc] initWithFrame:labelFrame];
[valueLabel setFont:[UIFont systemFontOfSize:12]];
valueLabel.textAlignment = UITextAlignmentLeft;
[valueLabel setText:#"valueLabel:"];
minLabel.text = [data objectForKey:#"min"];
maxLabel.text = [data objectForKey:#"max"];
valueLabel.text = [data objectForKey:#"value"];
slider.minimumValue = [[data objectForKey:#"min"] intValue];
slider.maximumValue = [[data objectForKey:#"max"] intValue];
slider.value = [[data objectForKey:#"value"] intValue];
[self addSubview:slider];
//[self addSubview:maxLabel];
//[self addSubview:minLabel];
//[self addSubview:valueLabel];
}
return self;
}
-(void) sliderAction:(id) sender
{
NSLog("#never happens");
}
The frame was the offending code. I just needed to start the slider with it's own frame as opposed to using the one passed in.
float startX = frame.origin.x;
float startY = frame.origin.y;
slider = [[UISlider alloc] initWithFrame:frame];
needs to be
slider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 0.0, 250, 22.0)];
Did you set userInteractionEnabled to YES for your new subclass? Is your view sure to cover the whole area your sliders take up?
Related
Any idea how to move the UITableViewCell text over so that it doesn't overlap the small image on the left hand side?
here's a screenshot:
thanks for any help
here's my TableCell code:
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UILabel* nameLb;
UILabel* detailsLb;
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:#"log-cell"];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"log-cell"] autorelease];
UIImage* img = [ImgUtil image:[NSString stringWithFormat:#"butList3_%d.png", [[SelectCategory instance] select] + 1 ]];
UIImageView* bgImage = [[[UIImageView alloc] initWithImage:img ]autorelease];
[cell addSubview:bgImage ];
[cell sendSubviewToBack:bgImage ];
nameLb = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 250, 40)] autorelease];
[nameLb setNumberOfLines:4];
[nameLb setBackgroundColor:[UIColor clearColor]];
[nameLb setTextColor:[UIColor whiteColor]];
[nameLb setTag:NAME_TEXT];
[nameLb setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15]];
[nameLb setMinimumFontSize:10];
[cell addSubview:nameLb];
//add UIImage
cell.imageView.image = [UIImage imageNamed:#"shop.png"];
detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(5, 45, 250, 20)] autorelease];
[detailsLb setBackgroundColor:[UIColor clearColor]];
[detailsLb setTextColor:[UIColor whiteColor]];
[detailsLb setTag:DETAILS_TEXT];
[detailsLb setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[cell addSubview:detailsLb];
}else {
nameLb = (UILabel*)[cell viewWithTag:NAME_TEXT];
detailsLb = (UILabel*)[cell viewWithTag:DETAILS_TEXT];
}
ObjectInfo* obj = [myList objectAtIndex:indexPath.row ] ;
nameLb.text = [obj name];
// cell.textLabel.textColor = [UIColor whiteColor];
// cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
double distance = [ProjUtil getDistanceWithLat1:currentLocation.latitude long1:currentLocation.longitude lat2:obj.location.latitude long2:obj.location.longitude];
detailsLb.text = [NSString stringWithFormat:#"%.2f miles", [ProjUtil kmToMi:distance]];
// cell.detailTextLabel.textColor = [UIColor whiteColor];
// cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setBackgroundColor:[UIColor clearColor]];
///[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
When you create the label you are positioning them with this line:
detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(5, 45, 250, 20)] autorelease];
This sets the frame of your label. CGRectMake is a function that gives you a CGRect, in this case with origin (5,45), width 250 and height 20.
If you want to move the labels over to the right, increase the x value of your frame (the 5 in the CGRectMake call above).
(Givin one of my code ,widout modifying sry)
I am not able to see the image but you should create a custom cell and you can set the frames of your controls of cell in method:-
-(void)layoutSubviews
{
}
sample code :-
#import <UIKit/UIKit.h>
#interface CustomizedCellProductDetails : UITableViewCell {
UILabel *sNO;
UILabel *abcWine;
UILabel *redWine;
UILabel *two;
UILabel *hundred;
UILabel *fourTwo;
UILabel *twoOne;
UIImageView *imgView;
UILabel *itemNo;
UILabel *itemName;
UILabel *itemDesc;
UILabel *department;
UILabel *qtyAvailable;
UIButton *check;
}
#property (nonatomic , retain) UILabel *sNO;
#property (nonatomic , retain) UILabel *abcWine;
#property (nonatomic , retain) UILabel *redWine;
#property (nonatomic , retain) UILabel *two;
#property (nonatomic , retain) UILabel *hundred;
#property (nonatomic , retain) UILabel *fourTwo;
#property (nonatomic , retain) UILabel *twoOne;
#property (nonatomic , retain) UIImageView *imgView;
#property (nonatomic , retain) UILabel *itemNo;
#property (nonatomic , retain) UILabel *itemName;
#property (nonatomic , retain) UILabel *itemDesc;
#property (nonatomic , retain) UILabel *department;
#property (nonatomic , retain) UILabel *qtyAvailable;
#property (nonatomic , retain) UIButton *check;
-(void) clicked;
#end
#import "CustomizedCellProductDetails.h"
#implementation CustomizedCellProductDetails
#synthesize sNO,abcWine,redWine,two,hundred,fourTwo,twoOne,imgView,itemNo,itemName,itemDesc,department,qtyAvailable,check;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
sNO=[[UILabel alloc] init];
abcWine=[[UILabel alloc] init];
redWine=[[UILabel alloc] init];
two=[[UILabel alloc] init];
hundred=[[UILabel alloc] init];
fourTwo=[[UILabel alloc] init];
twoOne=[[UILabel alloc] init];
imgView=[[UIImageView alloc] init];
itemNo=[[UILabel alloc] init];
itemName=[[UILabel alloc] init];
itemDesc=[[UILabel alloc] init];
department=[[UILabel alloc]init];
qtyAvailable=[[UILabel alloc] init];
check=[UIButton buttonWithType:UIButtonTypeCustom];
[check addTarget:self action:#selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[check setTitle:#"Check" forState:UIControlStateNormal];
[self.contentView addSubview:sNO];
[self.contentView addSubview:abcWine];
[self.contentView addSubview:redWine];
[self.contentView addSubview:two];
[self.contentView addSubview:hundred];
[self.contentView addSubview:fourTwo];
[self.contentView addSubview:twoOne];
[self.contentView addSubview:itemNo];
[self.contentView addSubview:itemName];
[self.contentView addSubview:itemDesc];
[self.contentView addSubview:department];
[self.contentView addSubview:qtyAvailable];
[self.contentView addSubview:check];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame=CGRectMake(boundsX+10, 0, 50, 40);
sNO.frame = frame;
frame= CGRectMake(boundsX+70 ,0, 150, 40);
abcWine.frame = frame;
frame= CGRectMake(boundsX+230 ,0, 150, 40);
redWine.frame = frame;
frame= CGRectMake(boundsX+390 ,0, 50, 40);
two.frame = frame;
frame= CGRectMake(boundsX+450 ,0, 50, 40);
hundred.frame = frame;
frame= CGRectMake(boundsX+510 ,0, 50, 40);
fourTwo.frame = frame;
frame= CGRectMake(boundsX+570 ,0, 50, 40);
twoOne.frame = frame;
/************************ ***********************/
frame= CGRectMake(boundsX+10 ,50, 100, 200);
imgView.frame = frame;
}
-(void) clicked
{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
#end
also change the height in cellForRowAtIndexPath delegate method of uitableviewcell.
When generating your cell in cellForRowAtIndexPath, update your creation of the labels with this code to set the frames correctly
nameLb = [[[UILabel alloc] initWithFrame:CGRectMake(bgImage.frame.origin.x + bgImage.frame.size.width + 5, 5, 250, 40)] autorelease];
detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(bgImage.frame.origin.x + bgImage.frame.size.width + 5, 5, 250, 40)] autorelease];
That will dynamically create your custom rows text to the right of the image.
I have a class that implements "UIView" and inside - (void)drawRect:(CGRect)rect method I add some custom buttons to self. Inside a UIViewController I declare an instance of this view and i add it to self.view.
If i do this in - (void)viewDidLoad, everything works perfect, but if I create a method that is called when I push a button, and add that view to self.view, the buttons background is displayed after a few second. Till then only the name of the button appears written in white.
What do i do wrong?
Regards
#implementation CustomButton
#synthesize isPressed, buttonViewNumber;
- (id)initWithFrame:(CGRect)frame
title:(NSString *)title
tag:(int)tag
{
self = [super initWithFrame:frame];
if (self) {
[self setTitle:title forState:UIControlStateNormal];
[self setTag:tag];
self.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
[self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self setBackgroundImage:[[UIImage imageNamed:#"btn_white_add.png"] stretchableImageWithLeftCapWidth:20.0 topCapHeight:0.0] forState:UIControlStateNormal];
[self addTarget:self action:#selector(didSelect) forControlEvents:UIControlEventTouchUpInside];
self.isPressed = NO;
}
return self;
}
#implementation ButtonsView
#synthesize listOfButtons;
- (id)initWithFrame:(CGRect)frame bundle:(NSArray *)data
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
listOfNames = [[NSArray arrayWithArray:data] retain];
listOfButtons = [[NSMutableArray alloc] init];
currentViewNumber = 0;
viewNumber = 0;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGSize maximumLabelSize = CGSizeMake(300,24);
UIFont *buttonFont = [UIFont fontWithName:#"Helvetica" size:14.0];
float lineSize = 0;
float lineNumber = 0;
NSArray *listOfFrameX = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%d",FIRST_LINE_Y],
[NSString stringWithFormat:#"%d",SECOND_LINE_Y],
[NSString stringWithFormat:#"%d",THIRD_LINE_Y],
nil];
for (int i = 0; i < [listOfNames count]; i++) {
CGSize expectedLabelSize = [[[listOfNames objectAtIndex:i] objectForKey:#"name"] sizeWithFont:buttonFont
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeTailTruncation];
if ((lineSize + expectedLabelSize.width) > 260) {
lineNumber++;
lineSize = 0;
}
if (lineNumber > 2) {
viewNumber ++;
lineSize = 0;
lineNumber = 0;
}
CGRect newFrame = CGRectMake(lineSize,
[[listOfFrameX objectAtIndex:lineNumber] floatValue],
expectedLabelSize.width+30,
24);
CustomButton *myButton = [[CustomButton alloc] initWithFrame:newFrame
title:[[listOfNames objectAtIndex:i] objectForKey:#"name"]
tag:[[[listOfNames objectAtIndex:i] objectForKey:#"id"] intValue]];
myButton.buttonViewNumber = viewNumber;
[listOfButtons addObject:myButton];
if (viewNumber == 0) {
[self addSubview:myButton];
}
lineSize += expectedLabelSize.width + 40;
[myButton release];
}
}
this way it works:
- (void)viewDidLoad
{
[super viewDidLoad];
placeholders = [[NSArray arrayWithObjects:
something,something, something,something,nil] retain];
CGRect frame = CGRectMake(10, 247, 300, 84);
buttonsView = [[ButtonsView alloc] initWithFrame:frame bundle:placeholders];
[self.view addSubview:buttonsView];
and like this is doesnt work:
- (void)getCompanyNames:(id)result
{
NSArray *listOfCompanies = (NSArray *)result;
CGRect frame = CGRectMake(10, 247, 300, 84);
buttonsView = [[ButtonsView alloc] initWithFrame:frame bundle:listOfCompanies];
[self.view addSubview:buttonsView];
}
i need to add the "buttonsView" object to self.view after the -viewDidLoad
drawRect: isn't exactly the place you should be creating subviews (Buttons are subviews) for your view. This should either go into initWithFrame: or awakeFromNib, depending on your view creation method: programmatically or using a nib.
I am new to iPhone development. I am creating an application in which i need to create buttons and labels programmatically.
So I am doing this in .h file
#interface FirstQuizViewControlleriPad : UIViewController{
IBOutlet UIButton *startgame;
UILabel *theQuestion;
IBOutlet UIScrollView *scrollview;
UILabel *Question;
UILabel *Description;
UIView *ContainerView;
UILabel *Challengetext;
UIImageView *img1;
UIImageView *img2;
UIImageView *background;
IBOutlet UIButton *reviewbtn;
UILabel *ans1;
UILabel *ans2;
UILabel *ans3;
UILabel *ans4;
UIButton *button;
UIButton *button2;
UIButton *button3;
UIButton *button4;
UILabel *rightans;
UILabel *theScore;
UIButton *nextbtn;
UIButton *backbtn;
NSString *setchallange;
AVAudioPlayer *AVback;
AVAudioPlayer *AVstart;
NSArray *theQuiz;
NSInteger questionNumber;
NSInteger rightAnswer;
NSInteger myScore;
NSInteger finalScoreipad1;
BOOL restartGame;
id QuestionReview;
}
#property (retain , nonatomic) IBOutlet UIButton *startgame;
#property (retain , nonatomic) NSArray *theQuiz;
#property (retain , nonatomic) IBOutlet UILabel *theScore;
#property (retain , nonatomic) IBOutlet UIButton *nextbtn;
#property (retain , nonatomic) IBOutlet UIScrollView *scrollview;
#property (retain , nonatomic) IBOutlet UIButton *backbtn;
#property (retain , nonatomic) IBOutlet UILabel *Challengetext;
#property (retain , nonatomic) IBOutlet UIImageView *img1;
#property (retain , nonatomic) IBOutlet UIImageView *img2;
#property (retain , nonatomic) id QuestionReview;
#property (retain , nonatomic) IBOutlet UIButton *reviewbtn;
#property (retain ,nonatomic) IBOutlet UIButton *button;
#property (retain ,nonatomic) IBOutlet UIButton *button2;
#property (retain ,nonatomic) IBOutlet UIButton *button3;
#property (retain ,nonatomic) IBOutlet UIButton *button4;
#property (retain ,nonatomic) IBOutlet UILabel *ans1;
#property (retain ,nonatomic) IBOutlet UILabel *ans2;
#property (retain ,nonatomic) IBOutlet UILabel *ans3;
#property (retain ,nonatomic) IBOutlet UILabel *ans4;
#property (retain , nonatomic) IBOutlet UILabel *Question;
#property (retain , nonatomic) NSString *setchallange;
-(void)askQuestion;
in .m file
questionNumber = questionNumber + 1;
NSInteger row = 0;
if(questionNumber == 1)
{
row = questionNumber - 1;
}
else
{
row = ((questionNumber - 1) * 10);
}
NSString *selected = [theQuiz objectAtIndex:row];
NSString *activeQuestion = [[NSString alloc] initWithFormat:#"%#", selected];
QuestionReview = activeQuestion;
//lbl ref to btn1
ans1 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 292, 697, 100)] autorelease];
ans1.numberOfLines = 0;
ans1.font = [UIFont systemFontOfSize:26];
ans1.text = [theQuiz objectAtIndex:row+1];
ans1.lineBreakMode = UILineBreakModeWordWrap;
[ans1 sizeToFit];
[self.view addSubview:ans1];
//lbl ref to btn2
ans2 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 312 + ans1.frame.size.height , 697, 100)] autorelease];
ans2.numberOfLines = 0;
ans2.font = [UIFont systemFontOfSize:26];
ans2.text = [theQuiz objectAtIndex:row+2];
ans2.lineBreakMode = UILineBreakModeWordWrap;
[ans2 sizeToFit];
[self.view addSubview:ans2];
//lbl ref to btn3
ans3 = [[[UILabel alloc] initWithFrame:CGRectMake(36 ,332 + ans1.frame.size.height + ans2.frame.size.height , 697 , 100)] autorelease];
ans3.numberOfLines = 0;
ans3.font = [UIFont systemFontOfSize:26];
ans3.text = [theQuiz objectAtIndex:row+3] ;
ans3.lineBreakMode = UILineBreakModeWordWrap;
[ans3 sizeToFit];
[self.view addSubview:ans3];
//lbl ref to btn4
ans4 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 352 + ans1.frame.size.height + ans2.frame.size.height + ans3.frame.size.height, 697, 100)] autorelease];
ans4.numberOfLines = 0;
ans4.font = [UIFont systemFontOfSize:26];
ans4.text = [theQuiz objectAtIndex:row+4];
ans4.lineBreakMode = UILineBreakModeWordWrap;
[ans4 sizeToFit];
[self.view addSubview:ans4];
rightAnswer = [[theQuiz objectAtIndex:row+5] intValue];
Question = [[[UILabel alloc] initWithFrame:CGRectMake(22, 130, 725, 160)] autorelease];
Question.numberOfLines = 0;
//Question.font = [UIFont systemFontOfSize:27];
Question.text = activeQuestion;
Question.lineBreakMode = UILineBreakModeWordWrap;
[Question setFont:[UIFont fontWithName:#"Futura" size:26]];
Question.backgroundColor = [UIColor clearColor];
[Question sizeToFit];
[self.view addSubview:Question];
//For 1st Option.
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(buttonOne)
forControlEvents:UIControlEventTouchDown];
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button.titleLabel.font = [UIFont systemFontOfSize:24];
[button setTitle:[theQuiz objectAtIndex:row+1] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.frame = CGRectMake(34, 200 + Question.frame.size.height , 700, ans1.frame.size.height + 20);
[self.view addSubview:button];
button.layer.borderWidth = 3;
button.layer.borderColor = [[UIColor purpleColor ] CGColor];
button.layer.cornerRadius = 10.0;
[button setBackgroundImage:[UIImage imageNamed:#"buttun.png"] forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
//For 2nd Option.
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self
action:#selector(buttonTwo)
forControlEvents:UIControlEventTouchDown];
[button2 setTitle:[theQuiz objectAtIndex:row+2] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button2.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button2.titleLabel.font = [UIFont systemFontOfSize:24];
button2.frame = CGRectMake(34, 230 + Question.frame.size.height + button.frame.size.height , 700, ans2.frame.size.height + 20);
button2.layer.borderWidth = 3;
button2.layer.borderColor = [[UIColor darkGrayColor ] CGColor];
button2.layer.cornerRadius = 10.0;
[self.view addSubview:button2];
[button2 setBackgroundImage:[UIImage imageNamed:#"buttun2.png"] forState:UIControlStateNormal];
button2.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
//For 3rd Option.
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self
action:#selector(buttonThree)
forControlEvents:UIControlEventTouchDown];
[button3 setTitle:[theQuiz objectAtIndex:row+3] forState:UIControlStateNormal];
[button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button3.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button3.titleLabel.font = [UIFont systemFontOfSize:24];
button3.frame = CGRectMake(34, 260 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height , 700, ans3.frame.size.height + 20);
button3.layer.borderWidth = 3;
button3.layer.borderColor = [[UIColor purpleColor ] CGColor];
button3.layer.cornerRadius = 10.0;
[self.view addSubview:button3];
[button3 setBackgroundImage:[UIImage imageNamed:#"buttun.png"] forState:UIControlStateNormal];
button3.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
//For 4th Option.
button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button4 addTarget:self
action:#selector(buttonFour)
forControlEvents:UIControlEventTouchDown];
[button4 setTitle:[theQuiz objectAtIndex:row+4] forState:UIControlStateNormal];
[button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button4.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button4.titleLabel.font = [UIFont systemFontOfSize:24];
button4.frame = CGRectMake(34, 290 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height + button3.frame.size.height , 700, ans4.frame.size.height + 20);
button4.layer.borderWidth = 3;
button4.layer.borderColor = [[UIColor darkGrayColor ] CGColor];
button4.layer.cornerRadius = 10.0;
[self.view addSubview:button4];
[button4 setBackgroundImage:[UIImage imageNamed:#"buttun2.png"] forState:UIControlStateNormal];
button4.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
NSString *strright = [theQuiz objectAtIndex:row+7];
NSString *strtext = [[NSString alloc] initWithFormat:#"%#" , strright];
rightans = [[UILabel alloc] initWithFrame:CGRectMake(50, 330, 650, 150)];
rightans.numberOfLines = 0;
rightans.text = strtext;
rightans.backgroundColor = [UIColor clearColor];
rightans.font = [UIFont systemFontOfSize:26];
[self.view addSubview:rightans];
[rightans sizeToFit];
[rightans setHidden:YES];
//Description.
NSString *des = [theQuiz objectAtIndex:row+6];
NSString *destext = [[NSString alloc] initWithFormat:#"\n> Explanation :\n%#\n\n" , des];
Description = [[[UILabel alloc] initWithFrame:CGRectMake(40, 400 + rightans.frame.size.height , 690, 150)] autorelease];
Description.numberOfLines = 0;
// Description.font = [UIFont systemFontOfSize:13.5];
Description.text = destext;
[Description setFont:[UIFont fontWithName:#"Futura" size:26]];
Description.lineBreakMode = UILineBreakModeWordWrap;
Description.backgroundColor = [UIColor clearColor];
[Description sizeToFit];
[self.view addSubview:Description];
[Description setHidden:YES];
ContainerView = [[UIView alloc] initWithFrame:CGRectMake(30, 400 + rightans.frame.size.height , 700 ,Description.frame.size.height)];
ContainerView.backgroundColor = [UIColor clearColor];
ContainerView.layer.cornerRadius = 10.0;
ContainerView.layer.borderColor = [[UIColor grayColor] CGColor];
ContainerView.layer.borderWidth = 3;
// [ContainerView addSubview:Description];
[self.view addSubview:ContainerView];
[ContainerView setHidden:YES];
//For Challenge Text
NSString *challange = [theQuiz objectAtIndex:row+8];
setchallange = [[NSString alloc] initWithFormat:#"%#" , challange];//Memory leak here
Challengetext.text = setchallange;
//Image For Every Challenge
background = [[[UIImageView alloc] initWithFrame:CGRectMake(15.0f, 15.0f, 130.0f, 110.0f)] autorelease];
[background setImage:[UIImage imageNamed:[theQuiz objectAtIndex: row + 9]]];
[self.view addSubview:background];
In dealloc function , I release all the labels and buttons and other objects that i have created but still i got this warning for memory leakage (received memory warning level 1 and some times level 2)
I searched where my memory is being leaked and it is leak at NSString setchallenge
Please help me out this..
Any help will be appreciated..
Thanks..
You're creating a new string with every call to setchallenge = [[NSString alloc]....
Should be:
//For Challenge Text
NSString *challange = [theQuiz objectAtIndex:row+8];
Challengetext.text = [NSString stringWithFormat:#"%#" , challange];
stringWithFormat returns an auto-released string, which will get deallocated next time the auto-release pool is drained.
joe
Since you alloced memory, you need to release it as well. Anytime you use alloc, copy or new, remember to always release. Its not a good idea to release everything only at dealloc. Release objects when you are done with them. For example:
setchallange = [[NSString alloc] initWithFormat:#"%#" , challange];//Memory leak here
Challengetext.text = setchallange;
[setchallange release]
Also checkout out the iOS memory management guide. I'm sure it will help.
May be for some object you allocating more than one times and release it once. just check it out I hope it will be solve.
in this block of code my uiimageview always get nil.as a result the code crashes.i can t figure out where is the uiimageview getting nil.in this code self refers to uitableviewcell.
the code crashes in the code where i am inserting the imageview to the array.is this a problem of memory retain?
MyAppDelegate *appDelegate =(myAppDelegate *)[[UIApplication sharedApplication]delegate];
UIImageView *profileImageView;
UILabel *tweetLabel;
UILabel *timeLabel;
UILabel *profileNameLabel;
UIView *message;
if(self==nil)
{
profileImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
profileImageView.backgroundColor= [UIColor clearColor];
profileImageView.tag = 1;
tweetLabel = [[UILabel alloc] initWithFrame:CGRectZero];
tweetLabel.backgroundColor = [UIColor clearColor];
tweetLabel.tag = 2;
tweetLabel.numberOfLines = 3;
tweetLabel.lineBreakMode = UILineBreakModeWordWrap;
tweetLabel.font = [UIFont systemFontOfSize:10.0];
tweetLabel.textColor=[UIColor blackColor];
timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.tag = 3;
timeLabel.numberOfLines = 1;
timeLabel.lineBreakMode = UILineBreakModeWordWrap;
timeLabel.font = [UIFont systemFontOfSize:12.0];
timeLabel.textColor=[UIColor blackColor];
profileNameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
profileNameLabel.backgroundColor = [UIColor clearColor];
profileNameLabel.tag = 4;
profileNameLabel.numberOfLines = 1;
profileNameLabel.lineBreakMode = UILineBreakModeWordWrap;
profileNameLabel.font = [UIFont boldSystemFontOfSize:14.0];
profileNameLabel.textColor=[UIColor blackColor];
message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height)];
message.tag = 0;
[message addSubview:profileImageView];
[message addSubview:tweetLabel];
[message addSubview:timeLabel];
[message addSubview:profileNameLabel];
[self addSubview:message];
}
else{
tweetLabel = (UILabel *)[[self.contentView viewWithTag:0]viewWithTag:2];
timeLabel = (UILabel *)[[self.contentView viewWithTag:0]viewWithTag:3];
profileNameLabel = (UILabel *)[[self.contentView viewWithTag:0]viewWithTag:4];
profileImageView = (UIImageView *)[self.contentView viewWithTag:1];
NSLog(#" profileImageView %#",profileImageView);
}
NSString *tweet=[tweetObject tweet];
NSString *profileName=[tweetObject author];
NSString *time=[tweetObject time];
CGSize textSize = [tweet sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];
CGSize timeSize = [time sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];
CGSize profileNameSize = [profileName sizeWithFont:[UIFont boldSystemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];
profileImageView.frame = CGRectMake(5,self.frame.size.height/2, 44, 44);
NSLog(#" profileImageView %#",profileImageView);
tweetLabel.frame = CGRectMake(54, profileImageView.frame.size.width, 240,25);
timeLabel.frame = CGRectMake(180, 5.0f, 100, timeSize.height);
profileNameLabel.frame = CGRectMake(54,5, 150, profileNameSize.height);
tweetLabel.text=tweet;
timeLabel.text=time;
profileNameLabel.text=profileName;
profileImageView.image = [[UIImage imageNamed:#"fbdefaultProfile.gif"]retain];
NSLog(#" myArray WILL FILL WITH THIS ELEMNTS %# %# %# ",profileImageView,[tweetObject imageURL],#"fbdefaultProfile.gif");
NSArray *myArray = [NSArray arrayWithObjects:profileImageView,[tweetObject imageURL],#"fbdefaultProfile.gif",nil];
NSLog(#" myArray %#",myArray);
NSLog(#"%# myArray ",myArray);
[appDelegate performSelectorInBackground:#selector(updateImageViewInBackground:) withObject:myArray];
}
return;
if(self==nil)
{
what is this.. I think it should be
if(self!=nil)
{
and if it is fine then why are you adding subview to a nill
if(self==nil)
{
//your code .....
//and then you are adding sub view to a nil which is useless
[self addSubview:message];
}
The issue could be in the below line of code.
profileImageView = (UIImageView *)[self.contentView viewWithTag:1];
May be you don't have the an view with tag id 1;
plz check against nil after getting the view from viewWithTag.
if([self.contentView viewWithTag:1] != nil)
{
profileImageView = (UIImageView *)[self.contentView viewWithTag:1];
}
How does one create a dynamic thumbnail grid?
How is a grid created with thumbnails of images like the photo app on iphone?
How are these spaces arranged dynamically? e.g. adding and removing thumbnails.
this is my simple grid view app
- (void)viewDidAppear:(BOOL)animated {
[[Grid_ViewAppDelegate sharedAppDelegate] hideLoadingView];
temp = temp+1;
if (temp ==1){
NSLog(#"temp:%d",temp);
int n = 20; //Numbers of array;
NSArray *t = [[NSArray alloc] initWithObjects:#"calpie.gif",#"chrysler_electric.png",#"chrysler_fiat_cap.gif",#"cleanup.gif",#"ecylindri.gif",#"elect08.png",#"globe.gif",#"heat.gif",#"insur.gif"
,#"jobs.gif",#"office.gif",#"opec1.gif",#"orng_cap.gif",#"poll.gif",#"pollen.gif",#"purbar.gif",#"robinson.gif",#"robslink_cap.gif",#"scot_cap.gif",#"shoes.gif",nil];
myScrollView.contentSize = CGSizeMake(320, 460+n*2);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 1;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
NSString *mxiURL = #"http://meta-x.com/biflash/images/";
int i =0,i1=0;
while(i<n){
int yy = 4 +i1*79;
for(int j=0; j<4;j++){
if (i>=n) break;
CGRect rect;
rect = CGRectMake(4+79*j, yy, 75, 75);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
NSString *nn = [mxiURL stringByAppendingString:[t objectAtIndex:i]];
UIImage *buttonImageNormal=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: nn]]];
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
button.tag =i;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
//[self.view addSubview:button];
[myScrollView addSubview:button];
//[buttonImageNormal release];
[button release];
i++;
//
}
i1 = i1+1;
//i=i+4;
}
}
}
in AppDelegate
+ (Grid_ViewAppDelegate *)sharedAppDelegate
{
return (Grid_ViewAppDelegate *)[UIApplication sharedApplication].delegate;
}
- (void)showLoadingView
{
if (loadingView == nil)
{
loadingView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
loadingView.opaque = NO;
loadingView.backgroundColor = [UIColor grayColor];
loadingView.alpha = 0.5;
UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(142.0, 222.0, 37.0, 37.0)];
[spinningWheel startAnimating];
spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loadingView addSubview:spinningWheel];
[spinningWheel release];
CGRect label = CGRectMake(142.0f, 255.0f, 71.0f, 20.0f);
lblLoading = [[UILabel alloc] initWithFrame:label];
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont boldSystemFontOfSize:14];
// UILabel
lblLoading.backgroundColor =[UIColor clearColor];
[lblLoading setText:#"Loading..."];
//[lblLoading setTextAlignment:UITextAlignmentCenter];
[loadingView addSubview:lblLoading];
}
[window addSubview:loadingView];
}
- (void)hideLoadingView
{
[loadingView removeFromSuperview];
}
definitely if u apply this logic. u get succeed