Search This Blog

Thursday, 28 February 2013

Number validation-iOS

Hello there,
         Here is the simple thing how to put validation for numbers only in xcode.This example is for contacts ,and will also include '+' ,in validation.


Function:

-(BOOL)numberValidation:(NSString *)number{
   
   
    NSCharacterSet *decimalSet = [NSCharacterSet decimalDigitCharacterSet];
    BOOL stringIsValid = ([[number stringByTrimmingCharactersInSet:decimalSet] isEqualToString:@""] ||
                          [[number stringByTrimmingCharactersInSet:decimalSet] isEqualToString:@"+"]);
    return stringIsValid;
}



Example for how to use:

Suppose you have a UITextField IBOutlet 'abc'

so do it like this:

BOOL check = [self numberValidation:abc.text];

if(!check){

//show alert view ,when validation fails.

}


Happy Programming ;)

Thursday, 14 February 2013

Long Gesture-iOS

Hello friends ,
      So here is something from old school......hehehe.......we still have lot of useful things from old school.What if you are working on xcode project using MainStoryBoard??? and you want to capture the long press on button??? ...dont know ?......awww dont be sad ...here is the code..


CODE:

first add a gesture recognizer,as i am doing below,suppose you have an IBOutlet 'btnEmergencyBeacon' for UIButton.Then in your view did load method ,do this :

  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.btnEmergencyBeacon addGestureRecognizer:longPress];



Now simply override this function , in your same .m file :

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
       //Do something on long press....for example ill code for UIAlertView.

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Demo" message:@"Long Press Countered" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}


thats all,you can add this gesture recognizer to some view or anything else....


Happy programming ;)

Tuesday, 12 February 2013

Custom Marker on MapView-iOS

Hello there,
          So here is a new one, how to place a custom marker on MapView in xcode:


its as simple as table view :

Override this following function in your .m file.Remeber to include MKMapViewDelegate in your .h file.

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
        NSString *identifier = @"marker"; //identifier like we declare in tableview
    annotationV = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if(annotationV==nil){
        annotationV = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationV.canShowCallout = YES;
        annotationV.image = [UIImage imageNamed:@"green_mark.png"];//using custom image for marker.
        annotationV.canShowCallout =NO;
        annotationV.rightCalloutAccessoryView =  [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
    return annotationV;
}

Here what i am doing is using an image for marker,plus i am also displaying a banner above the marker for description.I am using "popup.png" image for banner.






Regards.