how to access dynamically created uilabel in iphone - ios

I have created dynamic UILabel with tag value and i have similar kind of other labels without tag values.
Now i want access only tag values labels but i was not able to acces the label text value.
Here is my code.
Dynamic created Label
for (int i= 0; i < [array count]; i++) {
UILabel *defaultLbl = [[UILabel alloc] initWithFrame:CGRectMake(30, 70, 60, 21)];
defaultLbl.text = #"Default";
defaultLbl.backgroundColor = [UIColor clearColor];
defaultLbl.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
[defaultLbl setFont:[UIFont fontWithName:#"Helvetica" size:12]];
defaultLbl.textAlignment = NSTextAlignmentCenter;
defaultLbl.tag = i+1;
[myButton addSubview:defaultLbl];
[defaultLbl release];
UILabel *masterProName = [[UILabel alloc] initWithFrame:CGRectMake(28, 20, 200, 21)];
masterProName.text = [masterProjListArray objectAtIndex:i];//#"Lorem Ipusum";
masterProName.backgroundColor = [UIColor clearColor];
masterProName.textColor = [UIColor colorWithRed:51.0/255.0 green:153.0/255.0 blue:204.0/255.0 alpha:1.0];
[masterProName setFont:[UIFont fontWithName:#"Helvetica-Bold" size:17]];
masterProName.lineBreakMode = NSLineBreakByCharWrapping;
[myButton addSubview:masterProName];
[masterProName release];
UILabel *masterProID = [[UILabel alloc] initWithFrame:CGRectMake(28, 45, 200, 21)];
masterProID.text = [masterProjIDArray objectAtIndex:i];//#"133 FS";
masterProID.backgroundColor = [UIColor clearColor];
masterProID.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];;
[masterProID setFont:[UIFont fontWithName:#"Helvetica" size:17]];
masterProID.lineBreakMode = NSLineBreakByCharWrapping;
[myButton addSubview:masterProID];
[masterProID release];
}
And UILabel access method after user press the long press button
- (void)longPressTap:(UILongPressGestureRecognizer *)sender
{
if ([recognizer.view tag]) {
UILabel *view = (UILabel *)recognizer.view;
NSLog(#"---%#", view.subviews);
for (UILabel *lbl in view.subviews) {
if (recognizer.view.tag == view.tag) {
NSString *text = view.text;
NSLog(#"---%#", text);
}
}
}
}
here am putting the log
"<UIImageView: 0x75b9510; frame = (0 0; 379 100); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x75acef0>>",
"<UILabel: 0x88c4e50; frame = (28 20; 200 21); text = 'Aux Water Waste Trtmnt'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x88c49c0>>",
"<UILabel: 0x88c52f0; frame = (28 45; 200 21); text = 'M10000'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x88c5380>>",
"<UILabel: 0x88c56c0; frame = (30 70; 60 21); text = '**Default**'; clipsToBounds = YES; userInteractionEnabled = NO; tag = 1; layer = <CALayer: 0x88c5750>>",
"<UIButton: 0x88c6440; frame = (240 10; 70 80); opaque = NO; userInteractionEnabled = NO; tag = 1; layer = <CALayer: 0x88c6500>>"
In this log i want to access only "Default" text label
Please give me any suggestion on this.

Well, you need to know which value the tag has of the lable with "Default" in its text. Let's assume it is 42. Then
UILabel *label = [view viewWithTag:42];
would do the trick.
Anyway your solution should work too. Have a look at the actual tag values that you are dealing with. i've got a guts feeling that recognizer.view.tag does not have the same value as the tag of the label with "Default" in it.

You can also create outlets of those at runtime.
Create an array, and go on to add outlets of each label.
Once you are done and need to retrieve them you can easily access by index of arrays.
Even you can create a dictionary for storing outlets, here you can access labels by keys.

As you are assigning tag to UILabel, we can get label from tag (if it known to us, in your case we know).
So give it a try this way
for (int i = 0 ; i <[array count]; i++)
{
UILabel *lbl = (UILabel*)[self.view viewWithTag:i+1];
NSLog(#"--- %#", lbl.text);
}

for get UILabel base on its Tag.
Take int i; in .h file,
And put i=1; in above for Loop
for (NSObject *view in self.View.subviews)
{
if ([view isKindOfClass:[UILabel class]])
{
label = (UILabel *)[[self view] viewWithTag:i];
NSLof(#"%#".label.text);
// break;
}
i++;
}

Firstly, you should be checking
if (lbl.tag == view.tag)
not
if (recognizer.view.tag == view.tag)
Secondly, it won't work anyway. All that does is identify the view that has been touched which you know. How about setting the tag value in the label you want to a unique number and testing
if (lbl.tag == <unique tag number>)

Related

How to ADD label inside infinite button?

This is my code:
-(void)scrollView
{
NSInteger buttonCount = 0;
NSInteger buttonSize = 0;
int divisible= 0;
buttonCount=[[arrFullSubCategory valueForKey:#"name"]count];
if(buttonCount==2)
{
divisible = 2;
buttonSize=self.view.frame.size.width/divisible;
}
else
{
divisible = 3;
buttonSize=self.view.frame.size.width/divisible;
}
[viewBtnScroll addSubview:self.scrollViewBtn];
for(int i=0;i<buttonCount;i++)
{
self.scrollViewBtn.scrollsToTop=YES;
self.scrollViewBtn.directionalLockEnabled=YES;
self.automaticallyAdjustsScrollViewInsets = NO;
UIButton *btn;
btn=[[UIButton alloc]initWithFrame:CGRectMake(buttonSize*i,0,buttonSize,self.scrollViewBtn.frame.size.height)];
[btn setTitle:[NSString stringWithFormat:#"%#",[[arrFullSubCategory objectAtIndex:i] valueForKey:#"name"]] forState:UIControlStateNormal];
btn.userInteractionEnabled=YES;
NSInteger tag=[[[arrFullSubCategory valueForKey:#"category"] objectAtIndex:i] integerValue];
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn setTag:tag];
highLight=[[UILabel alloc]initWithFrame:CGRectMake(buttonSize*i,10,btn.frame.size.width,20)];
highLight.backgroundColor=[UIColor greenColor];
highLight.hidden=YES;
[btn addSubview:highLight];
[self.scrollViewBtn addSubview:btn];
length = [[arrFullSubCategory valueForKey:#"name"] count];
}
self.scrollViewBtn.contentSize=CGSizeMake(buttonSize*buttonCount, self.scrollViewBtn.frame.size.height);
}
1.That button depend up on the coming json values.
2.That button size is calculated for the screen size.
3.i have created label as globally for some reason,and added that to subview of button ,initially it is hide ,when the button pressed that particular button label hidden=NO;
4.everything i have done perfectly,but my problem is How to give x.origin for the particular label?
5.From my code the label present only at end of the button not for all.
6.If i set x.origin as 0 then its present only at the initial button[first button].
7.That button present in scrollview .....
Simplest solution for this would be
create new UIView (lets name it MyView) of the size you want.
create UILabel of size you want and x and y with respect to MyView
and add it to MyView
Add MyView to your UIScrollView.
Then create a UIButton of the same dimensions as MyView and add it to you same UIScrollView
Important here is you add button to your UIScrollView and not MyView
and you are good to go .. hope this helps...
======= EDITS ======== Use the code as you need
float width = GridScrollView.frame.size.width;
int i,k=0;
for ( i= 0; i < imageArray.count; i+=4) {
for (int j =0; j<4; j++) {
if (i+j <nameArray.count) {
NSLog(#"i = %d j = %d k = %d",i,j,k);
UIActivityIndicatorView *spinner2 = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake((width/4)*j ,(width/4)*k , width/4, width/4)];
[spinner2 startAnimating];
[GridScrollView addSubview:spinner2];
UIView *gridBox = [[UIView alloc]initWithFrame:CGRectMake((width/4)*j ,(width/4)*k , width/4, width/4)];
// gridBox.backgroundColor=[UIColor whiteColor];
[GridScrollView addSubview:gridBox];
UIView *labelBack = [[UIView alloc]initWithFrame:CGRectMake((width/4)*j ,(width/4)*k , width/4, width/16)];
labelBack.backgroundColor=[UIColor blackColor];
labelBack.alpha=0.5;
[GridScrollView addSubview:labelBack];
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake((width/4)*j ,(width/4)*k , width/4, width/16)];
nameLabel.text=nameArray[i+j];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.textColor=[UIColor whiteColor];
nameLabel.font=[UIFont fontWithName:#"Arial" size:12];
[GridScrollView addSubview:nameLabel];
UIButton *gridButton = [[UIButton alloc]initWithFrame:CGRectMake((width/4)*j ,(width/4)*k , width/4, width/4)];
[gridButton addTarget:self action:#selector(gridButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
gridButton.tag=i+j;
[GridScrollView addSubview:gridButton];
}
} k++;
}
[GridScrollView setContentSize:CGSizeMake(width, (width/4)*(i/4))];
[self.view addSubview:GridScrollView];

How to create dynamic uitextfiled inside a loop and make all them editable mode?

I am continuously facing problem to create uitextfield programmatically inside a loop and make all them in editable mode?
I have created successfully text fields inside the loop but problem is that. the last one i.e. the last text field is in editable mode.
EX:
suppose an array (myarray) has 5 values.
-(void)createDynamic_textfld{
UITextField *roomratetxt;
UIScrollView *scrlsummery;
NSMutableArray *myarray = [NSMutableArray arrayWithObjects:#[#"one",#"two",#"three",#"four",#"five"], nil];
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc]init];
roomratetxt.frame = CGRectMake(200, 0, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
}
i want to make all 5 textfields in editable mode. but as of now, the last textfield is only in editing mode and the rest all are non editable mode.
Can anyone plz suggest me some ideas ?How to achieve this
Thanks in advance.
Ajeet Kumar
First of all you need to provide different frames for that you can them in different position. Your above code looks correct and it must be in editable mode. I execute below snippet on my project and it works like a charm. I am assuming you are using Scrollview and added all textfield in it. Only thing make sure you are not disabling them from
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
or
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string; // return NO to not change
text
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc]init];
roomratetxt.frame = CGRectMake(200, i*20, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
-(void)createDynamic_textfld{
UITextField *roomratetxt;
UIScrollView *scrlsummery;
NSMutableArray *myarray = [NSMutableArray arrayWithObjects:#[#"one",#"two",#"three",#"four",#"five"], nil];
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc]init];
roomratetxt.frame = CGRectMake(200, i*0, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
}
The frame of all 5 textField are putting in the same position. You might need to change their frame as well.
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc] init];
roomratetxt.frame = CGRectMake(200, i*60, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}

Access text values from dynamically created UITextFields. iOS

I am creating UITextField 's in my view based on the count of an NSArray.
I want to know how I can access the text values based on the tag of the UITextField in my NSArray.
So after I add the below UITextField 's to my view how would I write code to retrieve .text values for myTextfield0, myTextfield1 and myTextfield2 respectively?
Example:
NSString *textFeildTextString = [NSString stringWithFormat:#"%#",myTextfield1.text];
Actual Code:
self.textFieldArray = [NSArray arrayWithObjects:#"one", #"two", #"three",nil];
NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
for (int i=0; i<[self.textFieldArray count]; i++) {
UIView *spacerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
UITextField *myTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 60*i+150, 280, 45)];
myTextField.borderStyle = UITextBorderStyleNone;
myTextField.font = [UIFont systemFontOfSize:14];
myTextField.returnKeyType = UIReturnKeyDefault;
myTextField.placeholder = NSLocalizedString(#"Answer", nil);
myTextField.inputAccessoryView = [super createInputToolbar];
myTextField.background = [UIImage imageNamed:#"TextFieldBg"];
myTextField.textColor = [UIColor appTextColorGrey];
[myTextField setLeftView: spacerView1];
myTextField.leftViewMode = UITextFieldViewModeAlways;
myTextField.delegate = self;
myTextField.tag = 200+i;
[myDict setObject:myTextField forKey:[NSString stringWithFormat:#"myTextfield%d", i]];
[self.view addSubview:myTextField];
}
NSLog(#"Dict: %#",myDict);
The NSlog of my UITexfields
2014-10-31 11:58:32.715 [51992:13536821] Dict: {
myTextfield0 = "<UITextField: 0x7faea14f8430; frame = (20 150; 280 45); text = ''; clipsToBounds = YES; opaque = NO; tag = 200; layer = <CALayer: 0x7faea14f82d0>>";
myTextfield1 = "<UITextField: 0x7faea14fae70; frame = (20 210; 280 45); text = ''; clipsToBounds = YES; opaque = NO; tag = 201; layer = <CALayer: 0x7faea14faca0>>";
myTextfield2 = "<UITextField: 0x7faea16dce30; frame = (20 270; 280 45); text = ''; clipsToBounds = YES; opaque = NO; tag = 202; layer = <CALayer: 0x7faea16d19c0>>";
}
for (int i=0; i<[self.textFieldArray count]; i++) {
UITextField *txt = (UITextField *)[myDict objectForKey:[NSString stringWithFormat:#"myTextfield%d", i]];
NSLog(#"Text %#",txt.text);
}
You can access them either by their tag :
UITextField *textField= (UITextField *)[self.view viewWithTag:200]; // Your first text field (201 your second etc..)
NSString *text = textField.text;
Or access them directly via your dictionary as you already know the key for each one of them:
UITextField *textField = (UITextField *)[myDict objectForKey:#"myTextField0"];
Note that in order for the dictionary to stay around declare a strong property member in your class and assign it to the created local dictionary (otherwise it will be lost by ARC) so at the end of the method you will have something like:
self.myDict = myDict;

iOS Image View turn off selection highlighting

Bit of a strange question, so i've attached a screen recording to help...
Video : https://www.dropbox.com/s/3aaefixsk8eejln/Error.mov (See past the watermark!)
My issue is that in my application when the user is at the "Preview" view and is reviewing their images, each time the image is selected, the image receives a touched overlay, which could be confusing for a user as nothing happens when touched.
I would like to disable this if possible..
Below is the example code to how the images are being displayed..
- (void)setReviewImages
{
continueButtonDisabled = YES;
downloadedImageCount = 0;
NSArray *reviewViews = scrollView.subviews;
for (IMReviewView *reviewView in reviewViews) {
[reviewView removeFromSuperview];
}
NSUInteger photoCount = [[IMLocalUser localUser] cachedPhotoCount];
if ( nPageNum == 0 ){
for (NSUInteger i = 0; i < photoCount; i++) {
CGRect frame;
frame.origin.x = scrollView.frame.size.width * i;
frame.origin.y = 65;
frame.size = CGSizeMake(scrollView.frame.size.width, 327.0f);
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:i];
[scrollView addSubview:subview];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * i, scrollView.frame.size.height);
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
[self.view addSubview:headingLabel];
headingLabel.text = #"Time To Preview!";
headingLabel.textColor = [UIColor blackColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.textAlignment = NSTextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blackColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
}
It seems to me like IMReviewView has a UIButton subview. If so, try to set its adjustsImageWhenHighlighted property to NO.

Change text on swipe

I am creating a text jokes app what i need is i want the UITextView in swipe form like when i swipe the first joke after a gap another UITextview comes in with another joke , so far what i have done is i have manually created the UITextView ( like if there is 10 jokes then 10 uitextview ) and added them in a scroll but that is taking too much time in viewdidload method and the app takes 6,7 second to load ...
if you have anyother idea please do let me know
for (int i = 0; i < arrresult.count; i++) {
CGRect frame;
frame.origin.x = self.scrollmain.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollmain.frame.size;
// UITextView *subview = [[UITextView alloc] initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+10, 230, 120)];
// UITextView *subview2 = [[UITextView alloc]initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+160,230, 120)];
UITextView *subview = [[UITextView alloc] initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+10, 230, 120)];
UITextView *subview2 = [[UITextView alloc]initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+160,230, 120)];
subview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BG_text.png"]];
subview2.backgroundColor = [UIColor whiteColor];
[subview setFont:[UIFont boldSystemFontOfSize:14]];
[subview2 setFont:[UIFont boldSystemFontOfSize:14]];
subview.layer.cornerRadius = 10 ;
subview2.layer.cornerRadius = 10;
NSString *p = [duaobj2 dua_arabic];
NSString *b = [duaobj2 eng_trans];
subview.text = p;
subview2.text = b;
[self.scrollmain addSubview:subview];
[self.scrollmain addSubview:subview2];
subview.editable = NO;
subview2.editable = NO;
subview.textColor = [UIColor blackColor];
subview2.textColor = [UIColor blackColor];
subview.backgroundColor = [UIColor whiteColor];
subview2.backgroundColor = [UIColor whiteColor];
scrollmain.showsHorizontalScrollIndicator = NO;
CGRect frame1 = subview.frame;
CGRect frame2 = subview2.frame;
frame1.size.height = subview.contentSize.height;
frame2.size.height = subview2.contentSize.height;
subview.frame = frame1;
subview2.frame = frame2;
}
self.scrollmain.contentSize = CGSizeMake(self.scrollmain.frame.size.width * arrresult.count,self.scrollmain.frame.size.height);
scrollmain.pagingEnabled = TRUE;
scrollmain.delegate = self;
There is no need to load the subviews at the time of viewDidLoad.
You should delay this by using scrollView delegate methods.
You should create only 3 textviews at max. Current, previous and next.
On scrollViewDidScroll delegate method, you should wisely remove previously loaded textview and you should add new textview.

Resources