views:

69

answers:

1

Hello, I want a counter that shows in a UILabel how many charachters the UITextView has.

I tried this:

IBOutlet UITextView *twittermessage;
UIActionSheet * loadingActionSheet;
IBOutlet UILabel * anzahl;

Here the .m:

- (void)textViewDidChange:(UITextView *)twittermessage{
    int count = [twittermessage.text length];
    anzahl.text = (NSString *)count;
    }

Thanks for your help and sorry for my bad English.

+2  A: 

You can't cast an integer to a NSString using C-style casts. You have to apply a string formatter to count to get a string representation of count.

Your code should look like:

- (void)textViewDidChange:(UITextView *)theTwitterMessage{
    int count = [theTwitterMessage.text length];
    anzahl.text = [NSString stringWithFormat:@"%d",count];
}
Mike
Thanks for your help, but it doenst work. He says in the second line: "Local declaration of "twittermessage" hides instance variable" - Why?
Flocked
It just means that there are two occurrences of twittermessage(one as an argument to the function, and one as an instance variable of your class. I've fixed my example to remedy this issue.
Mike
Hm It doesn't work either. Now there is no warning and problem, but he simply doesn't count. Doesn't have the *)theTwitterMessage point to my UITextview? My UITextView has the name twitterMessage. In your text he reacts on changes in the "theTwitterMessage"-UITextView, doesn't he?
Flocked
Have you wired up the anzahl IBOutlet? You should at least see "0" in that field even if nothing else is working.Also, do you have that class set as the UITextViewDelegate for twittermessage?
Kendall Helmstetter Gelner
Now it does work! It was just because of the delegate.
Flocked