It looks like you're new here. If you want to get involved, click one of these buttons!
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UILabel *ChannelLabel;
}
-(IBAction) Add;
-(IBAction) Update;
@end
#import \"ViewController.h\"
int TagNum = 0, NumY = 0;
@implementation ViewController
-(IBAction) Add {
for (int x=0; x<5; x++) {
TagNum = TagNum + 1;
NumY = NumY + 20;
ChannelLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, NumY, 40, 20)];
ChannelLabel.font = [UIFont fontWithName:@\"DB LCD Temp\" size:14];
ChannelLabel.text = [NSString stringWithFormat:@\"%d\", NumY];
ChannelLabel.backgroundColor = [UIColor clearColor];
ChannelLabel.textColor = [UIColor whiteColor];
ChannelLabel.textAlignment = UITextAlignmentCenter;
ChannelLabel.tag = TagNum;
[self.view addSubview:ChannelLabel];
ChannelLabel.userInteractionEnabled = YES;
}
}
-(IBAction) Update {
for (int y=0; y<5; y++) {
TagNum = 0;
TagNum = TagNum + 1;
NumY = NumY + 20;
ChannelLabel.tag = TagNum;
ChannelLabel.text = [NSString stringWithFormat:@\"%d\", NumY];
}
}
@end
Replies
However, in glancing through your code I noticed that in the Update method you are initializing your TagNum variable within your for loop. Perhaps this is what is making your code not 'work'.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSo for this code, the Add method is working correctly... It creates 5 UILabels labeled "20, 40, 60, 80, 100" and prints them onto the screen with each one having a tag value 1 through 5.
The problem I'm trying to figure out is in the Update method... This method should change the 5 UILabels created to say "120, 140, 160, 180, 200" but when I run the method it just updates the last UILabel created and reads "20, 40, 60, 80, 200".
My question is why can I not select the other UILabels by their tag value and change there text?
Also the reason I re initialized the TagNum Variable was use the same variable to select the tag for the UILabel that should be getting changed...
Thanks!!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeWell you have only one UILabel property in your View Controller, yet you are creating 5 UILabels. When you call your update method, how is your code to retrieve the 5 UILabels that you originally created? And therein lies the problem.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeHere's some Apple documentation on using NSMutableArray: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW2
Start with that. Using arrays and other 'collections' of objects is a core part of programming so you would do well to be familiar with how they are used.
Good luck!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI feel like were going in two different directions or I might be miss understanding what you are telling me... My and goal is to have one button that every time you click it it adds a UILabel to the screen programmatically that has a tag attached to it... The text comes from a UITextField when the button was clicked and is stored in an array... then the user is able to click on that label and move it to a new location on the screen. If the user clicks on the label it can then change the text by entering it in the Textfield and hitting the second button to Update the the Label that is selected by tag.
*** This part in my code is all working! The code I attached is a very dumbed down version to show just the problem Im having... This problem is that I can update the text on the last UILabel that was created but if I create a new label I cant update any labels that were made before that last one... The New one can always be updated....
I have tried multiple things such as the .tag or viewWithTag but in the end I can only update the last label created...
If you were going in that direction with your answer I apologize for the confusion! Please attach some code if you don't mind...
Thanks!
-Rob
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome