Can't change text of a UITextField - ios

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");
}
}

Related

UITextField will not become first responder when touched, none of the usual mistakes apply

I have a UITextField that will not become the first responder when tapped. I can assign it to become first responder, which works find. But if it resigns first responder status and I try and tap it or tab back to it to make it become the first responder again, nothing happens. It appears as if the touch is being trapped somewhere, and yet I can't find anything in my code that could be causing that to happen. I've checked the usual suspects:
If the textfield the top view
is the textfield within the bounds of it's superview
is the textfield userEnabled.
I've also rewritten the code in several different ways, to no avail.
Can anyone help me with this problem. The textField at issue is the field titled answerTextField in the method createOneDoubleViewAtOrigin.
The relevant code is below.
-(instancetype)initForProblem:(NSString *)problem{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
self = [super init];
if (self) {
[self parseBasicFractionProblem:problem];
if (_problemType == fractDoubleWithPic) {
NSLog(#"placed the fract views");
UIView *firstProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(26, 30) withNumerator:_numerator1 denominator:_denominator1 forViewNumber:0];
UIView *secondProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(342,30) withNumerator:_numerator2 denominator:_denominator2 forViewNumber:1];
[self addSubview:firstProblemView];
[self addSubview:secondProblemView];
[self bringSubviewToFront:firstProblemView];
[self bringSubviewToFront:secondProblemView];
}
else if (_problemType == fractDoubleNoPicAns||_problemType == fractDoubleNoPicExtendedAns ){
}
}
self.tag = 800;
self.backgroundColor = [UIColor redColor];
NSLog(#"made to end");
return self;
}
-(UIView *)createOneDoubleViewAtOrigin:(CGPoint)viewOrigin withNumerator:(NSInteger)numerator denominator:(NSInteger)denominator forViewNumber:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UIView *containerView = [[UIView alloc] initWithFrame: CGRectMake(viewOrigin.x,viewOrigin.y, 310, 263)];
containerView.backgroundColor = [UIColor colorWithRed:178.0/255.0 green:222.0/255.0 blue:80.0/255.0 alpha:1.0];
containerView.layer.cornerRadius = 5.0;
UILabel *numeratorView = [self createSubview:CGRectMake(66, 23, 59, 47) text:[NSString stringWithFormat:#"%ld",(long)numerator] inView:containerView];
UILabel *divisorView = [self createSubview:CGRectMake(66, 40, 59, 47) text:#"___" inView:containerView];
UILabel *denominatorView = [self createSubview:CGRectMake(72, 82, 47, 47) text:[NSString stringWithFormat:#"%ld",(long)denominator] inView:containerView];
UILabel *equals = [self createSubview:CGRectMake(125, 50, 47, 47) text:#"=" inView:containerView];
/*
FFractSupportedTextField *answerField = [self createAnswerField:CGRectMake(173,50,82,47)];
*/
UITextField *answerTextField = [[UITextField alloc] initWithFrame:CGRectMake(173,50,82,47)];
//Inside
answerTextField.font = [UIFont fontWithName:#"Helvetica" size:30.0];
answerTextField.textAlignment = NSTextAlignmentCenter;
answerTextField.placeholder = #"?";
//border
answerTextField.layer.borderWidth = 1;
answerTextField.layer.borderColor = [[UIColor blackColor] CGColor];
answerTextField.layer.cornerRadius = 5.0;
answerTextField.userInteractionEnabled = YES;
[answerTextField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
containerView.tag = 820 + 6*viewNumber;
numeratorView.tag = 821 + 6*viewNumber;
divisorView.tag = 822 + 6*viewNumber;
denominatorView.tag = 823 + 6*viewNumber;
equals.tag = 824 + 6*viewNumber;
answerTextField.tag = 801 + viewNumber;
UIView *pictureView = [self createFractPictureForNumerator:numerator denominator:denominator number:viewNumber];
pictureView.tag = 825 + 6*viewNumber;
if (viewNumber == 0){
_answerTextField1 = answerTextField;
[containerView addSubview:_answerTextField1];
[containerView bringSubviewToFront:_answerTextField1];
_pictureView1 = pictureView;
[containerView addSubview:_pictureView1];
[_answerTextField1 becomeFirstResponder];
} else if (viewNumber == 1) {
_answerTextField2 = answerTextField;
[containerView addSubview:_answerTextField2];
[containerView bringSubviewToFront:_answerTextField2];
_pictureView2 = pictureView;
[containerView addSubview:_pictureView2];
}
return containerView;
}
-(UILabel *)createSubview:(CGRect)frame text:(NSString *)text inView:(UIView *)containerView{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UILabel *labelView = [[UILabel alloc] initWithFrame:frame];
labelView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelView.textAlignment = NSTextAlignmentCenter;
labelView.text = text;
[containerView addSubview:labelView];
return labelView;
}
-(FFractSupportedTextField *)createAnswerField:(CGRect)frame{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
FFractSupportedTextField *fieldView = [[FFractSupportedTextField alloc] initWithFrame:frame];
//Inside
fieldView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
fieldView.textAlignment = NSTextAlignmentCenter;
fieldView.placeholder = #"?";
//border
fieldView.layer.borderWidth = 1;
fieldView.layer.borderColor = [[UIColor blackColor] CGColor];
fieldView.layer.cornerRadius = 5.0;
fieldView.userInteractionEnabled = YES;
return fieldView;
}
-(UIView *)createFractPictureForNumerator:(NSInteger)numerator denominator:(NSInteger)denominator number:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
NSLog(#"numerator:%ld denominator:%ld",(long)numerator,(long)denominator);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(33, 165, 256, 78)];
containerView.backgroundColor = [UIColor whiteColor];
containerView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
containerView.layer.borderWidth = 1.0;
containerView.layer.cornerRadius = 3.0;
NSInteger smallViewCount = denominator;
if (denominator == 0) {
smallViewCount = 1;
}
float smallWidth = 245.0/smallViewCount;
for (int n = 0; n < smallViewCount; n++) {
NSLog(#"count %d",n);
UILabel *smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(8 + n*smallWidth, 8, smallWidth - 5, 29)];
smallLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
smallLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[smallLabel setAdjustsFontSizeToFitWidth:YES];
smallLabel.textAlignment = NSTextAlignmentCenter;
smallLabel.layer.cornerRadius = 3.0;
smallLabel.tag = 830+n + viewNumber*10;
[containerView addSubview:smallLabel];
}
UILabel *largeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 41, 240, 29)];
largeLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
largeLabel.text = [NSString stringWithFormat:#"= %ld",(long)numerator];
largeLabel.textAlignment = NSTextAlignmentCenter;
largeLabel.layer.cornerRadius = 3.0;
[containerView addSubview:largeLabel];
NSLog(#"end of createFractPictFor..");
return containerView;
}

Custom UIAlertView not resizing for keyboard after its 1st appearance

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.

Can't remove a label programmatically

I am printing a label programmatically but I can't remove it from the screen. I have tried removeFromSuperview and lbl1.hidden = YES; and lbl1= nil; but non of them work. It stays on the screen all the time while I can see in the debug it pass from ELSE as in the code below.
Where would be my problem?
-(void)reloadData
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];
if (result1 > result2 && al == YES)
{
lbl1.userInteractionEnabled = YES;
lbl1.text = #" Warning!! ";
lbl1.tag = 30;
lbl1.font = [UIFont fontWithName:#"Helvetica" size:18.0];
lbl1.textColor = [UIColor redColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.lineBreakMode = NSLineBreakByWordWrapping;
lbl1.numberOfLines = 2;
[self addSubview:lbl1];
[lbl1 release];
}
else{
//Non of them is removing the label.
[lbl1 removeFromSuperview];
lbl1= nil;
lbl1.hidden = YES;
}
Every time you go into reloadData, you are creating a new label, so if you go into reload and jump to the else, you are creating a label, and then removing it.
You need to save that label as an instance variable and remove it/add it in your reloadData.
#property(nonatomic, strong) UILabel *lbl1;
And in your code, do this only ONCE:
self.lbl1 = [[[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)] autorelease];
And in your reloadData do:
-(void)reloadData
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];
if (result1 > result2 && al == YES)
{
self.lbl1.userInteractionEnabled = YES;
//Etc...
}
else{
[self.lbl1 removeFromSuperview];
}
Try like this:
-(void)reloadData
if(!lbl1)
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)];
if (result1 > result2 && al == YES)
{
lbl1.userInteractionEnabled = YES;
lbl1.text = #" Warning!! ";
lbl1.tag = 30;
lbl1.font = [UIFont fontWithName:#"Helvetica" size:18.0];
lbl1.textColor = [UIColor redColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.lineBreakMode = NSLineBreakByWordWrapping;
lbl1.numberOfLines = 2;
[self addSubview:lbl1];
[lbl1 release];
}
else{
//Non of them is removing the label.
[lbl1 removeFromSuperview];
lbl1= nil;
lbl1.hidden = YES;
}
Try to remove like this....
if (result1 > result2 && al == YES)
{
lbl1.userInteractionEnabled = YES;
lbl1.text = #" Warning!! ";
lbl1.tag = 30;
lbl1.font = [UIFont fontWithName:#"Helvetica" size:18.0];
lbl1.textColor = [UIColor redColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.lineBreakMode = NSLineBreakByWordWrapping;
lbl1.numberOfLines = 2;
[self addSubview:lbl1];
[lbl1 release];
}
else{
//Non of them is removing the label.
[[self.view viewWithTag:30] removeFromSuperview];
lbl1= nil;
lbl1.hidden = YES;
}

How to create a UITextField on SpriteKit

I want to implement a Textfield to input your name on my SpriteKit Game, so I have something like this:
SKLabelNode* gameTitle = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster"];
gameTitle.text = #"Game Title";
gameTitle.name = #"gametitle";
gameTitle.fontSize = 44.0;
gameTitle.fontColor = [SKColor blackColor];
gameTitle.position = CGPointMake(self.size.width/2,self.size.height/2 + 150);
[self addChild:gameTitle];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = #"Enter your name here";
textField.backgroundColor = [SKColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self.delegate;
[self.view addSubview:textField];
so, the problem is that it's not showed on my scene, maybe the problem is when adding it into the scene, I use addSubview.
Is that wrong?
You can add objects that inherits UIView's inside the didMoveToView:(SKView *)view function
Just add that function to your SKScene and move your UITextField inside:
-(void)didMoveToView:(SKView *)view {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.center = self.view.center;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = #"Enter your name here";
textField.backgroundColor = [UIColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self.delegate;
[self.view addSubview:textField];
}
to remove it use the
[myTextField removeFromSuperView];
if you don't want to use the method didMoveToView that insert the textField at the scene presentation, you can use this method:
-(void)insertUI{
ttaAppDelegate *myDel = [[UIApplication sharedApplication] delegate];
UIViewController *vc = myDel.window.rootViewController;
self.textField= [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
self.textField.center = self.view.center;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.textColor = [UIColor blackColor];
self.textField.font = [UIFont systemFontOfSize:17.0];
self.textField.placeholder = #"Enter your name here";
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.autocorrectionType = UITextAutocorrectionTypeYes;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.textField.delegate = self;
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.button addTarget:self action:#selector(saveScore:) forControlEvents:UIControlEventTouchDown];
[self.button setTitle:#"Save" forState:UIControlStateNormal];
self.button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[vc.view addSubview:self.textField];
[vc.view addSubview:self.button];
vc.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
[vc requestInterstitialAdPresentation];
}

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