I wrote a simple ios5 application (includes garbage collector) that has a single view and a UITextField
I need to analyze input text in this UITextField
here's my code.
header file:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
{
IBOutlet UITextField *myTextField;
}
@property (nonatomic, retain) IBOutlet UITextField *myTextField;
-(IBAction)editingChanged:(UITextField *)sender;
editingChanged:
tracked with send event editing changed so this method calls everytime user changes something in my UITextField
part of implementation file:
#pragma mark - textField
-(NSString *)stringWithoutAbc:(NSString *)sourceString
{
NSString *resultString=[sourceString stringByReplacingOccurrencesOfString:@"abc:" withString:@""];
if (![resultString isEqualToString:sourceString])
{
NSLog(@" sourceString: %@", sourceString);
NSLog(@" resultString: %@", resultString);
};
return resultString;
}
-(IBAction)editingChanged:(UITextField *)sender
{
NSLog(@"editing Changed. text: %@", sender.text);
//removing "abc:" from string in text field
NSString *str=[self stringWithoutAbc:sender.text];
//if something was removed - changing text in text field
if (![str isEqualToString:sender.text])
{
sender.text=str;
};
}
Everytime user changes text in UITextField
we remove "abc:" strings from this text using standard NSString
method.
The problem is: the application is unstable. It sometimes crashes when "abc:" gets removed. Help me please. How to solve this problem?
You can use below textField
's delegate method :-
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{