Custom UIAlertView not resizing for keyboard after its 1st appearance - ios

I have a custom UIAlertVIew with 2 textFields that I am using to input values into. The first time that the customUIAlertVIew is displayed, the view is moved up to make room for the keyboard.
This is unfortunately, not the case for each subsequent display. Each subsequent time, the alertView isn't moved up for the keyboard.
I am looking for what is causing this problem, and how can I get around it in the future.
EDIT: Here is the code for how the custom UIAlertVIew is created.
CustomUIAlertView.m
-(id)initWithTitle:(NSString *)title {
if (self = [super init]){
self.title = title;
self.message = #"\n\n\n\n\n\n";
[self addButtonWithTitle:#"Cancel"];
[self addButtonWithTitle:#"Done"];
[self setDelegate:self];
}
return self;
}
-(void)didPresentAlertView:(UIAlertView *)alertView{
NSArray *subViews = [UIApplication sharedApplication].keyWindow.rootViewController.
presentedViewController.view.subviews;
if (subViews.count >1){
UIView *presentedView = [subViews objectAtIndex:1];
UILabel *player1Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 55, 130, 21)];
player1Label.text = [NSString stringWithFormat:#"Player 1 Score"];
// player1Label.textInputMode = UIKeyboardTypeNumberPad;
player1Label.tag = 42;
[presentedView addSubview:player1Label];
UILabel *player2Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 75, 130, 21)];
player2Label.text = [NSString stringWithFormat:#"player 2 Score"];
player2Label.tag = 43;
[presentedView addSubview:player2Label];
self.p1ScoreField = [[UITextField alloc]initWithFrame:CGRectMake(150, 55, 80, 21)];
self.p1ScoreField.placeholder= #"score";
self.p1ScoreField.tag = 44;
self.p1ScoreField.keyboardType = UIKeyboardTypeNumberPad;
self.p1ScoreField.borderStyle = UITextBorderStyleRoundedRect;
self.p1ScoreField.delegate = self;
[self.p1ScoreField addTarget:self action:#selector(UITextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[presentedView addSubview:self.p1ScoreField];
self.p2ScoreField = [[UITextField alloc]initWithFrame:CGRectMake(150, 75, 80, 21)];
self.p2ScoreField.tag = 45;
self.p2ScoreField.placeholder = #"score";
self.p2ScoreField.keyboardType = UIKeyboardTypeNumberPad;
self.p2ScoreField.borderStyle = UITextBorderStyleRoundedRect;
self.p2ScoreField.delegate = self;
[self.p2ScoreField addTarget:self action:#selector(UITextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[presentedView addSubview:self.p2ScoreField];
}
}
View presented in a separate view Controller. Alloc/Init is called on alertView at the viewDidAppear method.
- (IBAction)addNewRoundButtonPressed:(id)sender {
[alertView show];
}

Adding subviews to UIAlertView is not supported by Apple and is strongly discouraged.
You're better off using a regular view controller with custom modal presentation (look up UIViewControllerAnimatedTransitioning) or Googling a 3rd-party replacement.

Related

How to Show the Pickerview value in Textfield and hide the pickerView using barbuttonitem?

I am Having 2 Textfields stateText and providerText.If i click the first textfield means,it shows the pickerView.After That i will Select the pickerView Value.Also i have added the toolbar barbuttonitem in that pickerView.Then I have to show the pickerView value in the Textfields and hide the pickerView after selecting the value in the PickerView.Also Pointer should moves to next TextField connectionNoText after getting Values in TextFields.
1.How to hide the PickerView and show the value in TextField?
2.How to show the pickerview in the Bottom of the View irrespective of phone size?
This is my Source Code:
NewConnectionviewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
//ScrollView
self.view.backgroundColor = [UIColor grayColor];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height + 510.0)];
[self.view addSubview:scrollView];
//statePickerView
_statePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view. frame.size.height, self.view.frame.size.width, 216)];
_statePickerView.delegate = self;
_statePickerView.dataSource = self;
_statePickerView.showsSelectionIndicator = YES;
[_statePickerView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:_statePickerView];
//providerPickerView
_providerPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view. frame.size.height, self.view.frame.size.width, 216)];
_providerPickerView.delegate = self;
_providerPickerView.showsSelectionIndicator = YES;
[_providerPickerView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:_providerPickerView];
//ToolBar
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(hidePickerView:)];
toolBar.items = #[barButtonDone];
barButtonDone.tintColor=[UIColor lightTextColor];
[barButtonDone setEnabled:YES];
[_statePickerView addSubview:toolBar];
//[_providerPickerView addSubview:toolBar];
//GettingView
[self stateView];
[self providerView];
[self connectionNoView];
[self addTestView];
}
-(void)hidePickerView:(UIBarButtonItem *)sender
{
NSLog(#"clicked");
[self.selectState resignFirstResponder];
}
-(void)stateView
{
UIView *stateView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, 80)];
stateView.backgroundColor = [UIColor blueColor];
[scrollView addSubview:stateView];
//stateLabel
UILabel *stateLabel = [[UILabel alloc]initWithFrame:CGRectMake(stateView.frame.origin.x+20,35, 70, 20)];
[stateLabel setText:#"State"];
[stateLabel setTextColor:[UIColor whiteColor]];
[stateView addSubview:stateLabel];
//TextField
NSLog(#"stateViewSize==>>%f",stateView.frame.origin.y);
stateText = [[UITextField alloc]initWithFrame:CGRectMake(stateView.frame.origin.x+110, 30, 150, 40)];
stateText.textColor = [UIColor blackColor];
stateText.backgroundColor = [UIColor clearColor];
[stateText setBorderStyle:UITextBorderStyleRoundedRect];
[stateText setPlaceholder:#"select State"];
[stateText setTag:0];
[stateView addSubview:stateText];
[stateText setDelegate:self];
}
-(void)providerView
{
UIView *providerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+80, self.view.frame.size.width, 80)];
providerView.backgroundColor = [UIColor orangeColor];
[scrollView addSubview:providerView];
NSLog(#"providerViewsize==>>%f",providerView.frame.origin.y);
//providerLabel
UILabel *providerLabel = [[UILabel alloc]initWithFrame:CGRectMake(providerView.frame.origin.x+20, 35, 70, 20)];
[providerLabel setText:#"Provider"];
[providerLabel setTextColor:[UIColor whiteColor]];
[providerView addSubview:providerLabel];
//TextField
providerText = [[UITextField alloc]initWithFrame:CGRectMake(providerView.frame.origin.x+110,30, 150, 40)];
providerText.textColor = [UIColor blackColor];
[providerText setPlaceholder:#"select Provider"];
providerText.backgroundColor = [UIColor clearColor];
[providerText setTag:1];
[providerText setBorderStyle:UITextBorderStyleRoundedRect];
[providerView addSubview:providerText];
[providerText setDelegate:self];
}
-(void)connectionNoView
{
UIView *connectionNoView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+160, self.view.frame.size.width, 150)];
connectionNoView.backgroundColor = [UIColor greenColor];
[scrollView addSubview:connectionNoView];
NSLog(#"connectionNoViewsize==>>%f",connectionNoView.frame.origin.y);
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
if (textField.tag ==0)
{
//stateText.inputView = _statePickerView;
[UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
_statePickerView.frame = CGRectMake(0, self.view.frame.size.height-216,self.view.frame.size.width, 216);
}
completion:^(BOOL finished){
}];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#""
message:#"Please select state first."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
return NO;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;//Or return whatever as you intend
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([thePickerView isEqual:_statePickerView])
{
return [STATE_ARRAY count];
}
else
{
return [PROVIDER_ARRAY count];
}
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if([thePickerView isEqual:_statePickerView])
{
return [STATE_ARRAY objectAtIndex:row];
}
else
{
return [PROVIDER_ARRAY objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *firstPickerViewValue;
NSString *secondPickerViewValue;
if ([pickerView isEqual:_statePickerView])
{
firstPickerViewValue = [STATE_ARRAY objectAtIndex:row];
[_providerPickerView selectRow:row inComponent:0 animated:YES];
secondPickerViewValue = [PROVIDER_ARRAY objectAtIndex:row];
[_providerPickerView reloadAllComponents];
[stateText setText:[NSString stringWithFormat:#"%#",firstPickerViewValue]];
[providerText setText:[NSString stringWithFormat:#"%#",secondPickerViewValue]];
}
I have read through the code and to be honest Im pretty confused by your approach as I tend to avoid magic numbers when laying out the UI. Anyway I think both your questions can be solved by assigning the inputView of your slate textField and assigning the inputAccessoryView to the toolbar you created in the code.
So for example,
stateText.inputView = _statePickerView
Well that is Swift so you would have
[stateText setInputView:_statePickerView];
Then you set the Accessory View so
[stateText setInputAccessoryView: toolBar];
Although toolbar is only in scope inside your viewDidLoad method so you should fix that. This should be enough to get you back on the right path. To dismiss the pickerView just resignFirstResponder of the selected textField inside your hidePickerView method.
Note that your textfields are also confined to the methods they are created in so you should fix that too.
Finally i Found a Solution.PickerView itself a ViewController so we can't hide the pickerView.Alternative solution is,
1.Create the SubView with Height 260 and add that View to the Self.View.
(set Y Value as Self.View.Frame.Size.height).
2.Add the PickerView(default Height - 216) and ToolBar(default Height - 44) to that SubView.
3.In BarButtonItem action Move that view to the bottom.(ex.Set Frame like this) CGRectMake(0,Self.View.Frame.Size.Height,Self.View.Frame.Size.Width,216)

How to delete UITextfield and entered data from it in iOS

I have develop one application and in that I have two UIButton named [+] and [-] and one UITextField default.
Now what I want is when I click on plus [+] button it should add one UITextField in UIView that is work perfectly to me but when I click on minus [-] button it should delete last created UITextField that is not working now for me.
How to manage that?
here is my code to plus[+] button.
- (IBAction)btnPlusClicked:(id)sender
{
scrollViewSpecialitiesTxtFields.hidden = FALSE;
float y;
y = (35 * txtSpecialityFieldCounter);
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, y, 120, 30)];
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
[arraySpecialitiesTxtFields insertObject:textField atIndex:arraySpecialitiesTxtFields.count];
scrollViewSpecialitiesTxtFields.contentSize = CGSizeMake(125, y+30);
[scrollViewSpecialitiesTxtFields addSubview:textField];
txtSpecialityFieldCounter++;
NSLog(#"txtSpecialityFieldCounter : %d",txtSpecialityFieldCounter);
}
here is my minus [-] button code
-(IBAction)btnMinusClicked:(id)sender
{
[scrollViewSpecialitiesTxtFields.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
txtSpecialityFieldCounter--;
NSLog(#"txtSpecialityFieldCounter : %d",txtSpecialityFieldCounter);
float y;
y = (35 * txtSpecialityFieldCounter);
for(int i = 0 ; i < txtSpecialityFieldCounter; i++)
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, y, 120, 30)];
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
[arraySpecialitiesTxtFields insertObject:textField atIndex:arraySpecialitiesTxtFields.count];
scrollViewSpecialitiesTxtFields.contentSize = CGSizeMake(125, y-30);
[scrollViewSpecialitiesTxtFields addSubview:textField];
}
}
I know minus[-] button code is wrong but let me know changes
Create a global variable as NSMutableArray *myAddedTextfields and in viewDidLoad-> myAddedTextFields = [NSMutableArray array];
- (IBAction)plusAction:(id)sender
{
UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 100, 20)];
textField1.text = #"hey how r u";
textField1.backgroundColor = [UIColor grayColor];
textField1.tag = 1;
[self.view addSubview:textField1];
[myAddedTextFields addObject:textField1];
}
- (IBAction)minusAction:(id)sender
{
UITextField *txtField = [myAddedTextFields objectAtIndex:myAddedTextFields.count -1]; //for deleting from the last textfield onwards.
[txtField removeFromSuperView];
[myAddedTextFields removeObjectIdenticalTo:txtField];
}
I have edited your method.. hope this helps.
Whenever you want to add an UITextField on a superview, set the tag of that component (UITextField).
And when you want to remove it from superview then get the reference of that component (UITextField) using the tag and remove it from the superview.
Here is an example that may be helpful to you:
-(IBAction)plusAction:(id)sender
{
UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 100, 20)];
textField1.text = #"hey how r u";
textField1.backgroundColor = [UIColor grayColor];
textField1.tag = 1;
[self.view addSubview:textField1];
}
-(IBAction)minusAction:(id)sender
{
UITextField * textfield = (UITextField *)[self.view viewWithTag:1];
[textfield removeFromSuperview];
}
Yes, you could use the tag option. Or you could use an NSArray holding your UITextFields.
#interface MyTextFieldViewClass ()
#property (nonatomic, retain) NSMutableArray *textFields;
#end
#implementation MyTextFieldViewClass
- (void)viewDidLoad {
[super viewDidLoad];
self.textFields = [NSMutableArray array];
}
- (IBAction)addTextField:(id)sender {
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 100, 20)];
[self.textFields addObject:tf];
[self.view addSubview:tf];
}
- (IBAction)removeTextField:(id)sender {
UITextField *tf = [self.textFields lastObject];
[tf removeFromSuperview];
[self.textFields removeLastObject];
}
#end
This would be a little better since you do not have to iterate over all the views/ids that exist in self.view.subviews. Also, using the tag might not be reliable since you always want to remove the last item, so you would either need to keep the last item number in memory or you need to iterate over all the subviews in order to find the UITextField with the highest tag and in order to remove it.

incompatible pointer types assigning to uilabel from label view

i've been doing a thing here using objective-c, but i'm a beginner in this language, and that's what i did:
I've created two UIView class and named them as LabelView and ButtonView. What i wanted to do is when i touch the button the label text changes. I have given the code below which i did.
I get an error when i touch the button i've created.
Error: "-[LabelView setText:]: unrecognized selector sent to instance 0x7b66cf80"
Warning on the line: self.cslbl_ = lblView; -
"incompatible pointer types assigning to uilabel from label view".
- (void)viewDidLoad {
LabelView *lblView = [[LabelView alloc] init];
[lblView setFrame:CGRectMake(10, 100, 300, 50)];
//lblView.backgroundColor = [UIColor redColor];
self.cslbl_ = lblView;
[self.view addSubview:lblView];
ButtonView *btnView = [[ButtonView alloc] initWithFrame:CGRectMake(110, 200, 100, 50)];
SEL target = #selector(changeText:);
[btnView setSelector:self selector:target];
[self.view addSubview:btnView];
}
-(void) changeText:(UIButton *)btn {
static int k = 0;
if (k%2 == 0)
{
[self.cslbl_ setText:#"Apple is a healthy fruit"];
}
else
{
[self.cslbl_ setText:#"Apple"];
}
NSLog(#"Click value is %d",k);
k++;
}
Custom Label Class
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UILabel *myLbl = [[UILabel alloc] init];
myLbl.frame = CGRectMake(0, 0, 300, 50);
//myLbl.backgroundColor = [UIColor blueColor];
myLbl.text = #"Apple";
self.lbl_ = myLbl;
[self addSubview:myLbl];
}
return self;
}
Custom Button Class
-(id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIButton *myBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[myBtn setTitle:#"Touch" forState:UIControlStateNormal];
[myBtn setTitle:#"Touched" forState:UIControlStateHighlighted];
myBtn.backgroundColor = [UIColor grayColor];
self.btn_ = myBtn;
[self addSubview:myBtn];
}
return self;
}
- (void)setSelector:(UIViewController *)vc selector:(SEL)changeText
{
[btn_ addTarget:vc action:changeText forControlEvents:UIControlEventTouchUpInside];
}
Though question is not clear but it seems that your self.cslbl_ is a UILabel and you are assigning lblView to it which of type UIView, thats the reason you are getting warning.
And you are trying to setText to LabelView which is again not a UILabel type that why you are getting crash.
First of all you LabelView should be a subclass of UILabel not UIView but still if its your requirement then you should use below code.
Custom Label Class
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UILabel *myLbl = [[UILabel alloc] init];
myLbl.frame = CGRectMake(0, 0, 300, 50);
[myLb1 setTag:1];
//myLbl.backgroundColor = [UIColor blueColor];
myLbl.text = #"Apple";
self.lbl_ = myLbl;
[self addSubview:myLbl];
}
return self;
}
in viewdidload
LabelView *lblView = [[LabelView alloc] init];
[lblView setFrame:CGRectMake(10, 100, 300, 50)];
self.cslbl_ = (UILabel*)[lblView viewWithTag:1];
[self.view addSubview:lblView];
Now you can set text to your self.cslbl_

Can't change text of a UITextField

I have created 2 UITextFields and a UIButton programmatically, when I click the UIButton I want it to change the text of the highlighted UITextField but it's changing the text of the second UITextField not the highlighted one.
-(void)viewDidLoad
{
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
txtf.borderStyle = UITextBorderStyleNone;
txtf.backgroundColor = [UIColor clearColor];
txtf.font = [UIFont systemFontOfSize:17];
txtf.autocorrectionType = UITextAutocorrectionTypeNo;
txtf.keyboardType = UIKeyboardTypeDefault;
txtf.returnKeyType = UIReturnKeyDone;
txtf.textAlignment = NSTextAlignmentLeft;
txtf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf];
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 345)];
txtf.borderStyle = UITextBorderStyleNone;
txtf.backgroundColor = [UIColor clearColor];
txtf.font = [UIFont systemFontOfSize:17];
txtf.autocorrectionType = UITextAutocorrectionTypeNo;
txtf.keyboardType = UIKeyboardTypeDefault;
txtf.returnKeyType = UIReturnKeyDone;
txtf.textAlignment = NSTextAlignmentLeft;
txtf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf];
}
- (IBAction)change:(id)sender
{
txtf.text = #"the text was changed"
}
is there any way to solve this problem?
Change your code to
-(void)viewDidLoad{
txtf1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
txtf1.borderStyle = UITextBorderStyleNone;
txtf1.backgroundColor = [UIColor clearColor];
txtf1.font = [UIFont systemFontOfSize:17];
txtf1.autocorrectionType = UITextAutocorrectionTypeNo;
txtf1.keyboardType = UIKeyboardTypeDefault;
txtf1.returnKeyType = UIReturnKeyDone;
txtf1.textAlignment = NSTextAlignmentLeft;
txtf1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf1];
txtf2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 345)];
txtf2.borderStyle = UITextBorderStyleNone;
txtf2.backgroundColor = [UIColor clearColor];
txtf2.font = [UIFont systemFontOfSize:17];
txtf2.autocorrectionType = UITextAutocorrectionTypeNo;
txtf2.keyboardType = UIKeyboardTypeDefault;
txtf2.returnKeyType = UIReturnKeyDone;
txtf2.textAlignment = NSTextAlignmentLeft;
txtf2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf2];
}
- (IBAction)change:(id)sender {
txtf2.text = #"the text was changed"
}
And add txtf2 to class members.
UPDATE:
To change highlighted UITextField modify - (IBAction)change:(id)sender to
- (IBAction)change:(id)sender
{
if ( txtf1.isHighlighted ) {
txtf1.text = #"the text 1 was changed";
}
else if ( txtf2.isHighlighted ) {
txtf2.text = #"the text 2 was changed";
}
else {
NSLog(#"none of the textField is Highlighted");
}
}
UPDATE 2
When you have a lot of text fields you can try following
for (id subview in self.view.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = subview;
if (textField.isHighlighted) {
textField.text = #"the text was changed";
}
}
}
txtf always points to second textField. so thats the problem.
When you created the first UItextFied, txtf points to it. But when you create a new UITextField and make txtf point to it, then reference with first textField is lost.
And in your change method, you are always getting the reference of second textfield and changing it.
Two options,
1) use txtf1 and txtf2, two class properties
2) use tags. [UIView viewWithTag:tagValue];
Using Tag's->
Assuming numberOfTextFields you using is 3.
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=1;
[self.view addSubview:txtf];
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=2;
[self.view addSubview:txtf];
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=3;
[self.view addSubview:txtf];
Modify change method to
- (IBAction)change:(id)sender
{
NSInteger numberOfTextFields=3;
UITextField *textField;
for (int i=1; i<=numberOfTextFields; i++) {
textField=(UITextField*)[self.view viewWithTag:i];
if(textField.isHighlighted==YES)
break;
else
textField=nil;
}
if(textField==nil){
NSLog(#"none is highlightes");
}
else{
textField.text= #"new text for highlighted textField";
}
}
Because your both UITextField has same name (txtf), So first change the name of either first or second UITextField. Other wise you code is correct. :)
EDITED :
Just make more correct #Avt's updated answer.
- (IBAction)change:(id)sender
{
if ( txtf1.isHighlighted ) {
txtf1.text = #"the text 1 was changed";
}
else if ( txtf2.isHighlighted ) {
txtf2.text = #"the text 2 was changed";
}
else {
NSLog(#"none of the textField is Highlighted");
}
}

textFieldShouldReturn does not get called (textField loaded programmatically)

I'm currently loading a -UITextField programmatically and trying to make the keyboard go down once I click on 'Search'.
I declare the -UITextFeild inside the .m file as
UITextField *searchTextField;
Inside the .h file I do indeed declare the -UITextField's delegate
#interface firstChannel : UIViewController<LBYouTubePlayerControllerDelegate, UITableViewDelegate,UITableViewDataSource, NSXMLParserDelegate, UITextFieldDelegate, AVPlayerItemOutputPullDelegate>{
This is how I add the UITextField programmatically.
searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, -26, 300, 30)];
searchTextField.borderStyle = UITextBorderStyleRoundedRect;
searchTextField.font = [UIFont fontWithName:#"Heiti TC" size:13];
searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
searchTextField.placeholder = #"Search on the Channel";
searchTextField.borderStyle = UITextBorderStyleNone;;
searchTextField.textAlignment = UITextAlignmentCenter;
searchTextField.center = CGPointMake(160, 43);
searchTextField.textColor = [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:(100.0/100.0)];
searchTextField.clearButtonMode = UITextFieldViewModeNever;
searchTextField.returnKeyType = UIReturnKeySearch;
[searchTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.view.window addSubview:searchTextField];
[searchTextField becomeFirstResponder];
I added the delegate inside -viewDidLoad
searchTextField.delegate = self;
However when I click on "Search" on the keyboard nothing happens. I'm unsure why that's happening I've even debugged it by logging something once you hit returns but nothing happens.
Call the searchTExtField.delegate after you create and setup the text field.
Try moving all the code in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad]; searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, -26, 300, 30)];
searchTextField.borderStyle = UITextBorderStyleRoundedRect;
searchTextField.font = [UIFont fontWithName:#"Heiti TC" size:13];
searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchTextField.delegate = self;
searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
searchTextField.placeholder = #"Search on the Channel";
searchTextField.borderStyle = UITextBorderStyleNone;;
searchTextField.textAlignment = UITextAlignmentCenter;
searchTextField.center = CGPointMake(160, 43);
searchTextField.textColor = [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:(100.0/100.0)];
searchTextField.clearButtonMode = UITextFieldViewModeNever;
searchTextField.returnKeyType = UIReturnKeySearch;
[searchTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.view.window addSubview:searchTextField];
[searchTextField becomeFirstResponder];
}
Also, if you are using search bar then you need to use search bar delegate. If you are just using UITextfield, then you should be okay.
Have you implemented the delegate method (this method gets called when the user taps the return key):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
//Perform search with text:
[self performSearch:[textField text]];
return NO;
}
This will resign the responder. Here you can also call your search method.

Resources