Thursday, 8 November 2012

Create a Date Picker for iPhone(Hide Past Date's)

         //Create a Date Picker
theDatePicker1 = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0,140.0, 320.0, 120.0)];
theDatePicker1.transform = CGAffineTransformMakeScale(1.0, 0.8); theDatePicker1.datePickerMode = UIDatePickerModeDate;
self.datePicker = theDatePicker1;
[theDatePicker1 release];
[datePicker addTarget:self 
                           action:@selector(dateChanged1)    
                           forControlEvents:UIControlEventValueChanged];

[self.view addSubview:datePicker];
       //Date Picker Hide the Past Dates Coding
        NSDate *now = [[NSDate alloc] init];
now = [now addTimeInterval:60*60];
NSLog(@"%@",now);
theDatePicker1.minimumDate = now;



        // Date Format Coding

-(void)dateChanged1
{
NSDateFormatter *df=[[NSDateFormatter alloc]init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:@"MM/dd/yyyy"];
txt1.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datePicker.date]];
[df release];
}

Add a UIPickerView in UIActionSheet for iOS

.h File

@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate>
{
    
    IBOutlet UITextField *txtText;
  NSArray *arrState;
    UIPickerView *pktStatePicker;
    UIToolbar *mypickerToolbar;
    UIActionSheet *sheet;
}

-(IBAction)open:(id)sender;


@end


.m File

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad

{
    
    [super viewDidLoad];
    
    arrState=[[NSArray alloc]initWithObjects:@"NewYork",@"NewJercy",@"Carlifornia",@"Florida",@"Fremont",@"SantaClara",@"San Diego",@"San Fransisco",@"San Jose", nil];
   
    
    pktStatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43 , 320, 480)];
    
    pktStatePicker.delegate = self;

    pktStatePicker.dataSource = self;
    
    [pktStatePicker  setShowsSelectionIndicator:YES];
    
    
    
    // Create done button in UIPickerView
    
    
    mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
    
    mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
    
    [mypickerToolbar sizeToFit];
    
    
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    
    
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    [barItems addObject:flexSpace];
    
    
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];
    
    [barItems addObject:doneBtn];
    
    
    [mypickerToolbar setItems:barItems animated:YES];
    
}


-(IBAction)open:(id)sender
{
    sheet = [[UIActionSheet alloc] initWithTitle:nil
                                                      delegate:self
                                             cancelButtonTitle:@"Done"
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:nil];
    [sheet addSubview:pktStatePicker];
    [sheet showInView:self.view.superview];
    [sheet addSubview:mypickerToolbar];
    [sheet showInView:self.view.superview];
    
    [sheet setBounds:CGRectMake(0, 20, 320, 430)];
    
    [pktStatePicker release];
    [mypickerToolbar release];
    [sheet release];

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{
    
    return 1;
    
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{
    
    return [arrState count];
    
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{
    
    return [arrState objectAtIndex:row];
    
}


- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{
    NSLog(@"You selected this: %@", [arrState objectAtIndex: row]);
    txtText.text = [arrState objectAtIndex:row];
    
}


- (void)pickerDoneClicked
{
    NSLog(@"Done Clicked");
    
    [txtText resignFirstResponder];
    
    [sheet dismissWithClickedButtonIndex:0 animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end



Xib File:



Output:






Reference Sites - Download Samples & Reference Sites using iPhone/iPad Codings

Click on UITextField to show a UIPickerView

Some time it is required in iPhone to add UIPickerView in UIActionSheet. Here are the steps to do that

In ViewController.h
@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate>

{

  IBOutlet UITextField *txtText;
  NSArray *arrState;
   UIPickerView *pktStatePicker ;
   UIToolbar *mypickerToolbar;

}

In ViewController.m


@implementation ViewController

- (void)viewDidLoad

{

  [super viewDidLoad];
 arrState= [NSArray    initWithObjects:@"NewYork",@"NewJercy",@"Carlifornia",@"Florida",@"Fremont",@"SantaClara",@"San Diego",@"San Fransisco",@"San Jose"nil];

  pktStatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43, 320, 480)];

  pktStatePicker.delegate = self;

  pktStatePicker.dataSource = self;

  [pktStatePicker  setShowsSelectionIndicator:YES];

  txtText.inputView =  pktStatePicker  ;

// Create done button in UIPickerView


mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];

mypickerToolbar.barStyle = UIBarStyleBlackOpaque;

[mypickerToolbar sizeToFit];


NSMutableArray *barItems = [[NSMutableArray alloc] init];


UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

[barItems addObject:flexSpace];


UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:selfaction:@selector(pickerDoneClicked)];

[barItems addObject:doneBtn];


 [mypickerToolbar setItems:barItems animated:YES];


 txtText.inputAccessoryView = mypickerToolbar;

}


-(void)pickerDoneClicked

{
  NSLog(@"Done Clicked");

 [txtText resignFirstResponder];
  mypickerToolbar.hidden=YES;
  pktStatePicker.hidden=YES;

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

 return 1;

}




- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

 return [arrState count];

}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

 return [arrState objectAtIndex:row];

}


- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

  txtText.text = [arrState objectAtIndex:row];

}




Thursday, 1 November 2012

Code for Text Field & UITextView Not Allowed Special Characters and Spaces in iPhone & iPad

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == aText)
   {
         if ([string isEqualToString:@"&"] || [string isEqualToString:@" "] ) 
        {
               return NO;
        }
   }
}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == TextField)
{
        
        if ([string length] == 0 && range.length > 0)
        {
            TextField.text = [TextField.text stringByReplacingCharactersInRange:range withString:string];
            return NO;
        }
        
        NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
        if ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
}






- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if(textView == YourReview1)
    {
        if ([text isEqualToString:@"&"]||[text isEqualToString:@" "])
        {
            return NO;
        }
        return YES;
    }
}