Wednesday, February 2, 2011

Setting maximum length limit in UITextField in iPhone.

There is no direct method to set the maximum length of input an UITextField should allow. But we can restrict the user to input to a certain length of characters.
Suppose we want a uitextfield only to accept upto 10 characters. Any character (other than enter and delete) should not be allowed to input if the user has already
entered 10 characters. i.e, we want to set a maximum length verifier to the UITextField. In Objective C we can do it as follows.
    We can create our own delegate for the UITextField. In this delegate we can put our logic to verify the input. And also we need to set this delegate as the UITextfield 's
delegate.

Step1.
    Writing the delegate:
    In MaxLengthDelegate.h

    @interface MaxLengthDelegate : NSObject <UITextFieldDelegate>{
        @private
            int max_length; //
      }
      @end

    In MaxLengthDelegate.m implement the following method and also modify the init method to init this with the maximum length user wants.

        - (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered
        {
            //check for enter and delete keys. These should be allowed even after the length is equal to max_length.
            if (([textField.text length] && range.length == 1) || [textEntered isEqualToString:@"\n"]) {
                return YES;
            }
   
            //check the length and return NO if it exceeds the max_length.
            if ([textField.text length] >= max_length) {
                return NO;
            }
   
            return YES;
        }

        //Setting the default max_length to 10.
        - (id) init
        {
            self = [super init];
            if (self != nil) {
                max_length = 10;
            }
            return self;
        }

        //to init delegate with the length.
        - (id) init : (int) maxLength
        {
            self = [super init];
            if (self != nil) {
                max_length = maxLength;
            }
            return self;
        }

Steps 2:
    Set the delegate of the UITextField which length needs to be verified as the maxLengthDelegate. For example, if we want to restrict input length of the UITextField (txtField) to 5, we can do it as:

    MaxLengthDelegate  maxLenDel = [[MaxLengthDelegate alloc] init: 5];
    [txtField setDelegate: maxLenDel];

Thanks

Writing simple callback in java

Callback can be used when you want to invoke some methods on the basis of some property changes. Suppose you want to call a piece of code of
another class automatically when there is a change in property. Or you have a single property that is used by more than one classes to do some
work. So when the value of this property is changed you want to notify those classes so that they can do their work with this updated value.
This can be done by callback routing.
   
    Steps to write callback routine.
Step1.
    Write one interface with the method/methods need(s) to be invoked.
   
    eg.
   
    public interface ICallback {
        public void notifyChange();
    }
   
Step2.
    Now write your binder class that will notify the object which is binded with it.
   
    public class CallbackBinder {
        private ICallback iCallback;   
       
        public void bindCallback (ICallback callBack){
            this.iCallback = callBack;
        }
       
        //so whenever the property changes notify the class object.
        public void notifyCallBack(){
            iCallback.notifyChange();
        }
   
    }

Step3.
    We need to bind the class with the callbackBinder to receive the notification. To do so we have to implement the ICallback interface and also pass the class reference to the binder.
   
    public class TestClass implements ICallback {
       
        public TestClass () {
            new CallbackBinder ().bindCallback (this);
        }
       
        //you have to define the notify method and do the work as you want.
        public void notifyChange () {
           
        }   
    }
   
    The notityChange method will be fired when the callbackBinder execute the notifyCallBack method.
   
    We can also handle multiple classes objects with the same property. Suppose a single property has to bind with multiple classes objects and on changing the value of that property we need to call all the class objects. To accomplish this we can maintain a map which will contain the property as the key and the list of ICallback objects as value. We can make the CallbackBinder class as singleton to bind all the class objects to the same instance of the binder class.
        
    private Map<String, List<ICallback>> propertyCallbackMap = new HashMap<String, List<ICallback>>();

    when the property value is changed just get the object list and call notifyChange on them.

Thanks


For free computer engineering programming books you can visit:
Free Computer Books