views:

36

answers:

3
countryField=[[UITextField alloc]initWithFrame:CGRectMake(0,0,100,25)];
    countryField.textColor = [UIColor blackColor];
    countryField.font = [UIFont systemFontOfSize:15.0];
    countryField.backgroundColor = [UIColor redColor];
    countryField.keyboardType = UIKeyboardTypeDefault;
    countryField.enabled=NO;
    countryField.delegate = self;
    [countryField addTarget:self action:@selector(getCountry:) forControlEvents:UIControlEventEditingChanged];

    [myView addSubview:countryField];

-(void)getCountry:(id)sender { printf("\n hai i am in getCountry method"); }

A: 

You could add a target for the UIControlEventTouchDown event just like you did for the UIControlEventEditingChanged event.

hotpaw2
That was my first attempt too, but that doesn't work.
audience
+1  A: 

Your Code looks like you want your TextField disabled. The method is only called if you enable your UITextField.

But when you enable your UITextField the Keyboard appears and the User is able to edit the text. To prevent that you have to implement the - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField method (from the UITextFieldDelegate Protocol) and just return NO.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;
}

In short: Enable your UITextField, implement the method above and change your Control Event to UIControlEventTouchUpInside (I think this is what you want !).

audience
A: 

Change your code to

countryField=[[UITextField alloc]initWithFrame:CGRectMake(0,0,100,25)];
countryField.textColor = [UIColor blackColor];
countryField.font = [UIFont systemFontOfSize:15.0];
countryField.backgroundColor = [UIColor redColor];
countryField.keyboardType = UIKeyboardTypeDefault;
//   IF YOU WANT CERTAIN METHOD TO GET FIRE WHEN TAPPED ON TEXTFIELD YOU HAVE TO ENABLE THE TEXTFIELD
//   countryField.enabled=NO;


countryField.delegate = self;
//   [countryField addTarget:self action:@selector(getCountry:) forControlEvents:UIControlEventEditingChanged];

[myView addSubview:countryField];

Then in the delgate method of textfield call the function you want to call i.e.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self getCountry];

    return YES;
}

Where the function is

-(void)getCountry 
{ 
  printf("\n hai i am in getCountry method"); 
}

hAPPY cODING...

Suriya