I'm new to Objective-C. I have created a textview in code, that limits its text to 25 characters. When the user wants to add more than 25 characters in the textview it shows an alert view... Now the problem is, when I click the done button it again shows the alert view. Can anyone tell me how to solve this?
first of all, an alert is perhaps the most primitive, non-elegant, un-friendly form of user interaction a programmer can turn to. you should turn to alerts as an absolute last resort.
though the question is a bit vague, it sounds to me as though your logic is this
while(strlen(textview) > 25) { // show alert }
so of course, when you show the alert, you need to truncate the length of the string to be less than 25, no? else you have a really boring game called 'click away the never ending alert!'
a more elegant solution would be to display the number of characters entered, and when it's getting close to the limit (say, over 20) then to make said display red, and instead of showing an alert to simply disallow further input after the 25th character (EXCEPTING OF COURSE THE BACKSPACE and DELETE!)
I think I have to agree to all previous users that this type of warning could be quite annoying for the user. Even if you change your way to something less obtrusive you still need to validate the input.
I think the correct way would be to use the Data Formatting features of Cocoa. You can find more information about it at: isPartialStringValid:newEditingString:errorDescription:.
As you can read there, you can return NO
, and the last inserted char will not be appended. With the control delegate control:didFailToValidatePartialString:errorDescription: it's even possible to use the default behaviour of Mac OS for such cases, which might change in the future.