Question

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?

Was it helpful?

Solution

You can use below textField's delegate method :-

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

OTHER TIPS

If you are using Arc, you don't have to retain the object. And btw, ARC is not garbage collector. http://longweekendmobile.com/2011/09/07/objc-automatic-reference-counting-in-xcode-explained/ MAybe because you use retain on the textField.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top