I want to add a custom uiview in my viewcontroller scroll view. and i am using below code but it not working. and not adding my viewThumbnail in scrollview.
my viewThumbnail contains a imageview.
for (int i=0; i<10; i++) {
viewThumbnail *objTemp =[[viewThumbnail alloc] init];
objTemp.frame = CGRectMake(i*165, 0, 126, 161);
[self.scrlViewRecent addSubview:objTemp];
}
[scrlViewRecent setContentSize:CGSizeMake((161 + 10)*10, 180)];
and if i use below code then it added blank view instead of viewThumbnail object
viewThumbnail *headContentView = [[viewThumbnail alloc] initWithFrame:CGRectMake(i*161 + (i*10), 0, 161, 140)];
[headContentView setBackgroundColor:[UIColor brownColor]];
[self.scrlViewRecent addSubview:headContentView];
So please suggest me where i am doing wrong.
Thx
You are calling different init methods, make sure your setup code for the viewThumbnail actually gets called. Not sure if self.scrlViewRecent is the correct view to add to (lacking context here).
Also some observations:
Class names should start with uppercase letter (ViewThumbnail)
scrlViewRecent saves 2 characters out of 16 but makes it hard to read
Your loop is most probably lacking
your viewThumbnail objects. Make
sure you release them somewhen.
Related
So i have a bunch of dynamically loaded labels..
Each of them has the same name because there is no telling how many there will be..
I have another method (not the one that created the labels) changing the text for one of the labels, but when i run it only the last label that was created will change..
I need it to change the one that has a certain tag or something..
Help is much appreciated, this website is yet to let me down.
self.myLabel cannot be connected to multiple labels, so it will contain the reference of last created label, you will have to create new label every time, and you can't track them by class properties, you have to access label by their tag.
you can set tag for each label, below is sample code,
for(int i=0; i< numberOfLabels; i++)
{
UILabel *label = [[UILabel alloc] init];
label.tag = i; // do not use tag 0 here.. u can use i+1, or i+100.. something like this.
[self.view addSubview:label];
}
to access labels,
UILabel *label = (UILabel*)[self.view viewWithTag: labelTag];
Okay since you dont have any code to show i guess i have to speculate.
What i understood is that you are creating Dynamic UILabels in ur code and you want to access them. Since you have same name for all the UILabels you might me loosing the previous UILabel when every time you create a new UILabel. So in order to keep track of how many UILabel you created you must add them in an Array. Declare an NSMutableArray in your viewController.h file and make sure in the viewDidLoad u allocate it like
arrForLabels = [[NSMutableArray alloc]init];
Since it is an NSMutableArray you can add object to it.
So when u create a UILabel make sure you add the same UILabel in the Array as well
for Instance
[arrForLabels addObject:yourLabel];
you can try to NSLog your Array to see its content.
Now all youu got to do is to Create a weak link like that
UILabel *tempLabel = [arrForLabels objectAtIndex:1];
now tempLabel will be the UILabel to change text
tempLabel.text = #"My New Text";
It will work fine.
Feel free to ask for any issues in it.
I have some confusion. Not really a bug or issue I am facing. I want to know something and I am not sure this is right place for this kind of question but here is my question:
In reference to my Question Here I was trying to map single gesture on two views.
Now I am keen to know that why this is not working? I went on basics that in case I have any NSObject subclass lets say if NSString and I add this in an NSMutableArray as follows:
NSString *strTest = #"Test String";
[aryTest1 addObject:strTest];
[aryTest2 addObject:strTest];
String gets added in both of array. Now what I am doing is I created a label, set tag for the label and added that in two views as follows:
UILabel *lblTemp = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
[lblTemp setTag:1];
[lblTemp setText:#"Label"];
[vw1 addSubview:lblTemp];
[vw2 addSubview:lblTemp];
Now every view has an array of subview. When we try to add an object in that view's subview why this is not being added in both of view?
I am getting a result that my second view is showing the label but first view is not having the label.
If I comment the last line of my code and don't add label in vw2 the label will be added in vw1. Than I tried a different thing.
UILabel *lblTemp = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
[lblTemp setTag:1];
NSMutableArray ary1 = [[NSMutableArray alloc] init];
NSMutableArray ary2 = [[NSMutableArray alloc] init];
[ary1 addObject:lblTemp];
[ary2 addObject:lblTemp];
both array has count 1. Thats mean that I have label added in my array. after that I tried to add array's object in view than even ame problem. Label is only being added in second view:
[vw1 addSubview:[ary1 objectAtIndex:0]];
[vw2 addSubview:[ary2 objectAtIndex:0]];
When I printed retainCount of Label after initialize, adding in array1 and array2 everytime the retaincount was 1. So I think the array are not keeping the reference of same object. Its a new object copied in array. Than why label is not being added in same view?
What is actual internal process for a object to add on any view's subview. As of I know subview is an Array type than what happening? Can anyone explain me?
Thanks in Advance
The difference is that addSubview automatically removes a view from it's super view before adding it to the new view (See the class reference: http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/uiview_class/uiview/uiview.html) while an array will happily accept an object even if it is in another array.
You can create a copy of the label and place it in the other view though if this is what you need to do.
What's the correct way of creating an UILabel and releasing it again when I want to re-use this until i'm out of records in my array?
I wanted to this this:
// create label
UILabel *labelIWishToRecycle;
for (int i = 0; i < [myArrayFullOfItems count]; i++) {
// Edit the label
labelIWishToRecycle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 640)];
}
// Release label
[labelIWishToRecycle release];
When I do it this way I get a crash for releasing it. So I just don't release it. Works like a charm now. Of course, this is not the way, so I was wondering what IS.
Should I create and release the label for every item in the array? Or should I create it outside the for-loop but release it within? Or ...?
Thanks in advance.
For what are you using the label!? If you are adding it to a view, than you have to use a new instance everytime.. if you only do size calculations or stuff like that, than you could use the same label over and over again.
But the trick is to initialize your local variable:
UILabel *labelIWishToRecycle = nil;
If you have done this, it is save to send release on it (also if no real label has been assigned yet). Before, your pointer is pointing on a random address and you are trying to release the object at that address. That will be a crash in the most cases.
(Guessing the problem case is the one, when your array count is zero.)
In my app i had to draw certain checkboxes at a same time and i used a single function to add all of them. Now when a user clicks one of them all of those checkboxes should get removed from the superview and currently its just removing the last one. Also i have issue to recognize those checkboxes like which one is clicked. i know it should be done through Tag property but don't know how exactly it should be implemented.
Any suggestions.
Removing all subviews
int numberOfSubviews = [[yourView subviews] count];
for(int i=0;i<numberOfSubviews-1;i++
{
[[youView subviews]objectAtIndex:i]removeFromSuperView];
}
//this will leave check box that you added at last.... for first one to remain loop from 1 to numberOfSubviews....
Using tag property...
when you are creating checkbox objects use
checkBoxObject.tag = i;
//I am considering i as looop count which you are using in a loop
to add checkboxes.
then whenever you need a object of checkbox
[yourViewonwhichYouAddedCheckBox viewWithTag:<your tag >];
Thanks
For identifying a "checkbox" or better said any view within an action-method:
- (void)someActionHandler:(id)sender
{
UIView *actionOriginView = (UIView *)sender;
NSLog(#"this action came from view:%d", actionOriginView.tag);
}
For assigning the tag, you may use the IB or within your code, while instantiating;
UIView *myFunkyView = [[UIView alloc] initWithFrame:CGRectZero];
myFunkyView.tag = 1337;
For removing a bunch of views from your superview - lets assume their tag is set to 10 - 15;
for (int i=10;i <= 15;i++)
{
UIView *childView = [superview viewWithTag:i];
[childView removeFromSuperview];
}
When I programmatically allocated a UILabel in my custom initWithNibName method, and later in viewDidLoad, tried to assign a string to it, the label was not pointing to anything. I didn't release it; the label shows on the screen. If I create the label in IB, and assign text to it in viewDidLoad, it works.
Is it against a rule to set up manually allocated objects in viewDidLoad?
Why is it not pointing to anything, even though viewDidLoad is called after my init?
From the doc of viewDidLoad:
This method is called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method. This method is most commonly used to perform additional initialization steps on views that are loaded from nib files.
In my init:
_descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 218, 280, 10)];
_descriptionLabel.numberOfLines = 0;
_descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
_descriptionLabel.font = [UIFont systemFontOfSize:12.0];
_descriptionLabel.adjustsFontSizeToFitWidth = NO;
_descriptionLabel.text = #"Description not found.";
_descriptionLabel.backgroundColor = [UIColor clearColor];
In viewDidLoad, the variable's value is 0x0.
It's the same with my manually allocated UIButton, which is fully working once the view loads.
If you want to create the UILabel programatically you can, but you still do it in viewDidLoad (as opposed to initWithNibName).
Don't be afraid to do UI setup in viewDidLoad. It is provided to add any static UI elements BEFORE the view appears on screen.
The view will not appear until just before viewDidAppear:(BOOL)animated is called.
If you have dynamic content configure it in viewWillAppear:(BOOL)animated. (This looks like your situation)
Then make sure you add it to the view:
[self.view addSubview:myLabel];
If you need future access to your new label, you will need to create an ivar to hold a pointer to it.
In the code posted, the _descriptionLabel UILabel is not retained and will be released before the view is drawn.
Try
_descriptionLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 218, 280, 10)] retain];
Make sure you put [_descriptionLabel release] in dealloc. This assumes _descriptionLabel is an instance variable.
This basically applies to any object you create with alloc, copy, or new.