Sunday, 22 May 2016

iOS Drop-down like android Part-2



      I my  previous  tutorial I have explained how to create iOS drop-down
 http://androiddhina.blogspot.in/2016/05/ios-drop-down-like-android-part- 
 1.html.Here I am going to explain how to implement iOS drop-down in your application

                      
      

Step 1)ViewController.h


#import <UIKit/UIKit.h>
#import "NIDropDown.h"

@interface ViewController : UIViewController<NIDropDownDelegate,UITextFieldDelegate>
{
    
    
     IBOutlet UITextField *txtDropDown;
     IBOutlet UITextField *txtDropDownNew;
      NIDropDown *dropDown;
    

}

@property (retain, nonatomic) IBOutlet UITextField *txtDropDown;
@property (retain, nonatomic) IBOutlet UITextField *txtDropDownNew;

-(void)rel;
@end




Step 2)ViewController.m

#import "ViewController.h"
#import "NIDropDown.h"

@interface ViewController ()



@end

@implementation ViewController

@synthesize txtDropDown,txtDropDownNew;


- (void)viewDidLoad {
    [super viewDidLoad];
    
    txtDropDown.inputView = [[UIView alloc] initWithFrame:CGRectZero];
    self. txtDropDown.delegate = self;
    [self. txtDropDown setReturnKeyType:UIReturnKeyDone];
    
    txtDropDownNew.inputView = [[UIView alloc] initWithFrame:CGRectZero];
    self. txtDropDownNew.delegate = self;
    [self. txtDropDownNew setReturnKeyType:UIReturnKeyDone];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) niDropDownDelegateMethod: (NIDropDown *) sender
{
    [self rel];
    
}

-(void)rel{
    
    dropDown = nil;
}



-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    NSArray * arr = [[NSArray alloc] init];
   
    
    if(textField == txtDropDown)
    {
        arr = [NSArray arrayWithObjects:@"Air" ,@"Ocean",@"Road" ,nil];
        txtDropDown.text=@"";
        [self myMethod:txtDropDown array:arr];
    }
    
    if(textField == txtDropDownNew)
    {
         arr = [NSArray arrayWithObjects:@"Export",@"Domestic",nil];
        
        txtDropDownNew.text=@"";
        
        [self myMethod:txtDropDownNew array:arr];
    }
    
}

-(IBAction)textFieldReturn:(id)sender
{
    [sender resignFirstResponder];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //hides keyboard when another part of layout was touched
    [self.view endEditing:YES];
   
    [super touchesBegan:touches withEvent:event];
    
    
}

- (void)myMethod:(UITextField*)textField array:(NSArray*)arr
{
   CGFloat f ;

    if(dropDown == nil)
    {
          if([arr count]==2)
           {
               f= 80;
           }
           else if(([arr count]==3))
           {
               f= 120;
           }
           else if(([arr count]==4))
           {
               f= 160;
           }
           else{
               f= 200;
           }
        
        
        dropDown = [[NIDropDown alloc]showDropDown:textField :&f:arr:@"down"];
        dropDown.delegate = self;
        
    }
    else {
        [dropDown hideDropDown:textField];
        [self rel];
    }
    
   }

@end

Happy Coding:)