Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Can not edit UILabel by tag created programmatically

Hey Guys Im working on an app right now that creates a multiple UILabels and later on can be changed. I couldn't get it to work so I made a small test file to isolate the problem... Code listed below... Can anyone explain why this code works but for only the last UILabel created? Thanks!

ViewController.h



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

IBOutlet UILabel *ChannelLabel;
}

-(IBAction) Add;
-(IBAction) Update;


@end



ViewController.m



#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

Post edited by rvirzera on

Replies

  • FstuffFstuff Posts: 154Registered Users
    Can anyone explain why this code works but for only the last UILabel created?
    Your problem statement is vague so of course any help offered is not likely to be very targeted. I don't know what you are expecting to happen, what is actually happening, or how you are observing this.

    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'.
  • rvirzerarvirzera Posts: 4New Users
    Fstuff;436759 said:
    Your problem statement is vague so of course any help offered is not likely to be very targeted. I don't know what you are expecting to happen, what is actually happening, or how you are observing this.

    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'.
    Hey FStuff,

    So 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!!
  • FstuffFstuff Posts: 154Registered Users
    That is a little illuminating.

    Well 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.
  • rvirzerarvirzera Posts: 4New Users
    Fstuff;436765 said:
    That is a little illuminating.

    Well 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.
    Maybe thats where I'm lost... the goal is to to have one method that can create a UILabel every time the button is hit. Because I would not know how many time the button will be hit my plan was to label these by tag so that I could change the label at a later time with the update method. Right now this code is just creating 5 labels and just trying to update them but with the idea of an unknown amount of labels how would you go about this problem?

    Thanks!
  • FstuffFstuff Posts: 154Registered Users
    Rather than having a UILabel property in your View Controller, you can have a mutable array (NSMutableArray). Each time you create another UILabel, add it to the array. Then when you want to update all the UILabels, you can just iterate through the array to perform some operation on each UILabel, or you can just get a specific UILabel from the array in order to update it.

    Here'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!
  • rvirzerarvirzera Posts: 4New Users
    Fstuff;436801 said:
    Rather than having a UILabel property in your View Controller, you can have a mutable array (NSMutableArray). Each time you create another UILabel, add it to the array. Then when you want to update all the UILabels, you can just iterate through the array to perform some operation on each UILabel, or you can just get a specific UILabel from the array in order to update it.

    Here'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!
    Hey Fstuff,

    I 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
Sign In or Register to comment.