Original question:
I have 3 UITextFields (nameField, locationField, and discriptionField) I need to have a if statement trigger on discriptionField only, however the way I have tried it (configured below) all 3 textfields perform setViewMovedUp. I have also tried
if([sender isEqual: discriptionField])
but I'm getting the same problem, all 3 textfields preform the method.
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if (sender == discriptionField)
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
My Solution:
Beginner mistake, I was calling that same method from another method without realizing it. Probably a poor way to solve the problem but here is my solution.
BOOL onlyDiscription = NO;
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:discriptionField])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
onlyDiscription = YES;
}
}
}
-(void)keyboardWillShow {
if (onlyDiscription) {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
}
-(void)keyboardWillHide {
if (onlyDiscription) {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
con = YES;
[self setViewMovedUp:NO];
}
onlyDiscription = NO;
}
}
Better to use tag value and compare with tag value.
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if (sender.tag == discriptionField.tag)
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
(or)
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if([sender isEqual:discriptionField]){
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
Please check like this
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:discriptionField])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
Related
I have a Scrolleview and i had its container View a UIView and UIVew contains textfields but Keyboard is not Poping..
I had use this methods...
-(void)scrollViewAdaptForEditingtext:(UITextField*)textfield {
CGPoint point = CGPointMake(0, textfield.frame.origin.y - 1.5 * textfield.frame.size.height);
[self.scrollView setContentOffset:point animated:YES];
}
-(void)scrollViewEndEditingtext:(UITextField*)textfield {
CGPoint point = CGPointMake(0, 0);
[self.scrollView setContentOffset:point animated:YES];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self scrollViewAdaptForEditingtext:textField];
return YES;
}
#pragma TextFieldDelegateProtocol
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.txtFieldFirstName) {
[self.txtFieldLastName becomeFirstResponder];
} else if (textField == self.txtFieldLastName) {
[self.txtFieldEmail becomeFirstResponder];
} else if (textField == self.txtFieldEmail) {
[self.txtFieldMobNumber becomeFirstResponder];
} else if (textField == self.txtFieldMobNumber) {
[self.txtFieldGender becomeFirstResponder];
} else
[textField resignFirstResponder];
[self scrollViewEndEditingtext:textField];
return YES;
}
can I include uiview also
I have several custom buttons in my view and made them a custom focus animation with didUpdateFocusInContext ,and I added a UITapGestureRecognizer to the view that when user taps twice a method will run ! but the problem is when didUpdateFocusInContext prevents to double tap action until the focused context reaches its end (it means riches to the last button) then method will fire when there is no focusable context !!! how can prevent such thing ? Here is my code :
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator: (UIFocusAnimationCoordinator *)coordinator {
//Setup focausing for main buttons
for (UIButton *mainButton in _nextPrevButton){
if (context.nextFocusedView == mainButton) {
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.40 initialSpringVelocity:.60 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
context.nextFocusedView.transform = CGAffineTransformMakeScale(1.2, 1.2);
context.nextFocusedView.layer.shadowOffset = CGSizeMake(0, 0);
context.nextFocusedView.layer.shadowOpacity = 1;
context.nextFocusedView.layer.shadowRadius = 15;
context.nextFocusedView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
context.nextFocusedView.layer.shadowOpacity = 1;
} completion:nil];
} else {
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.40 initialSpringVelocity:.60 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
context.previouslyFocusedView.transform = CGAffineTransformMakeScale(1, 1);
context.previouslyFocusedView.layer.shadowOpacity = 0;
} completion:nil];
}
}
if (context.nextFocusedView == _button1Focused) { [self buttonFocused:context]; } else if (context.previouslyFocusedView == _button1Focused) { [self buttonNotFocused:context]; }
if (context.nextFocusedView == _button2Focused) { [self buttonFocused:context]; } else if (context.previouslyFocusedView == _button2Focused) { [self buttonNotFocused:context]; }
if (context.nextFocusedView == _button3Focused) { [self buttonFocused:context]; } else if (context.previouslyFocusedView == _button3Focused) { [self buttonNotFocused:context]; }
}
Tapping methods :
- (void)viewDidLoad {
UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightTap:)];
[rightTap setAllowedPressTypes:#[#(UIPressTypeRightArrow)]];
[rightTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:rightTap];
}
- (void)handleRightTap:(UITapGestureRecognizer *)sender {
//or put the code here !
if (sender.state == UIGestureRecognizerStateRecognized)
{
NSLog(#"Right Arrow");
}
}
I tried other states :
if (sender.state == UIGestureRecognizerStateBegan)
{
// handling code
NSLog(#"UIGestureRecognizerStateBegan ");
}
if (sender.state == UIGestureRecognizerStateEnded)
{
// handling code
NSLog(#" UIGestureRecognizerStateEnded ");
}
if (sender.state == UIGestureRecognizerStateRecognized)
{
// handling code
NSLog(#"UIGestureRecognizerStateRecognized");
}
but no success !
Right, an easy one for most.
So with in a health App i have several text field for calculating a users BMI. I have the following code actions ALL EACH linked to a button to calculate the BMI. The idea being that if all the height fields and weight fields are empty then the app disables the button. the fields are linked to cmDisable and feetDisable etc... Can anyone think of a MUCH simpler way of doing this?
Many Thanks
- (IBAction)cmDisable:(id)sender {
if (((UITextField*)sender).text.length > 0) {
[_calculateButton setEnabled:YES];
} else {
[_calculateButton setEnabled:NO];
}
}
- (IBAction)feetDisable:(id)sender {
if (((UITextField*)sender).text.length > 0) {
[_calculateButton setEnabled:YES];
} else {
[_calculateButton setEnabled:NO];
}
}
- (IBAction)inchesDisable:(id)sender {
if (((UITextField*)sender).text.length > 0) {
[_calculateButton setEnabled:YES];
} else {
[_calculateButton setEnabled:NO];
}
}
- (IBAction)kgDisable:(id)sender {
if (((UITextField*)sender).text.length > 0) {
[_calculateButton setEnabled:YES];
} else {
[_calculateButton setEnabled:NO];
}
}
- (IBAction)stoneDisable:(id)sender {
if (((UITextField*)sender).text.length > 0) {
[_calculateButton setEnabled:YES];
} else {
[_calculateButton setEnabled:NO];
}
}
- (IBAction)poundsDisable:(id)sender {
if (((UITextField*)sender).text.length > 0) {
[_calculateButton setEnabled:YES];
} else {
[_calculateButton setEnabled:NO];
}
}
So, if I am understanding correctly, you want to disable a button when all fields are empty.
What you could do is add all the buttons to a collection from Interface Builder. So you would have
IBOutletCollection(UITextField) NSArray *fields; //Linked to all fields
Then you would connect all fields to the "Editing Ended" action for UITextFields. The method would like the following:
- (IBAction) fieldsValueChanged:(id) sender {
BOOL shouldDisable = NO;
for(UITextField *next in fields) {
if(next.text.length > 0) {
shouldDisable = YES;
break;
}
}
[_calculateButton setEnabled:shouldDisable];
}
I am creating a custom delegate UIAlertView's alert:buttonClickedAtIndex: method, but it is not working properly. I am subclassing a UIView, and I have two buttons that are tagged as 0 and 1. This still does not work when I go to check the delegate for my custom view. Here the code that I did.
Custom View
- (void) buttonTouchedWithIdentifier:(NSInteger)identifier
{
if (identifier == 0) {
[self.delegate alert:self didClickButtonWithTagIdentifier:0];
}
if (identifier == 1) {
[self.delegate alert:self didClickButtonWithTagIdentifier:1];
}
}
* in my showInViewMethod *
[self.dismissButton addTarget:self action:#selector(buttonTouchedWithIdentifier:) forControlEvents:UIControlEventTouchDown];
[self.continueButton addTarget:self action:#selector(buttonTouchedWithIdentifier:) forControlEvents:UIControlEventTouchDown];
self.dismissButton.tag = 0;
self.continueButton.tag = 1;
* in my view controller *
nextLevelAlert = [[ARAlert alloc] init];
nextLevelAlert.delegate = self;
[nextLevelAlert showInView:self.view
withMessage:[NSString stringWithFormat:#"Congratulations, you have completed level %i.\nWould you like to continue?", levelNumber]
dismissButtonTitle:#"Menu"
continueButtonTitle:#"Next Level"];
- (void)alert:(ARAlert *)alert didClickButtonWithTagIdentifier:(NSInteger)tagId
{
if (alert == nextLevelAlert) {
if (tagId == 0) {
NSLog(#"User does not want to continue.");
}
}
}
Now, nextLevelAlert has the delegate set to self, and I do have the delegate declared in my view controller's class. Also, when i do the showInView... for nextLevelAlert, it DOES appear, it is recognizing what button is being pressed.
My guess is your param is not a NSInteger but the button, you should change buttonTouchedWithIdentifier like this :
- (void) buttonTouchedWithIdentifier:(id)sender
{
UIButton *button = (UIButton*)sender;
NSLog(#"buttonTouchedWithIdentifier %#",#(button.tag));
if (button.tag == 0) {
[self.delegate alert:self didClickButtonWithTagIdentifier:0];
}
if (button.tag == 1) {
[self.delegate alert:self didClickButtonWithTagIdentifier:1];
}
}
Also when comparing two objects use isEqual: instead of ==
- (void)alert:(ARAlert *)alert didClickButtonWithTagIdentifier:(NSInteger)tagId
{
if ([alert isEqual:nextLevelAlert]) {
if (tagId == 0) {
NSLog(#"User does not want to continue.");
}
}
}
How can I do it with out resize view frame (or tableView)?
I use this code and I have resized view then I scroll down/up
I use code, not storyboard
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer class] == [UIPanGestureRecognizer class])
{
UIPanGestureRecognizer *panGestureRec = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint distance = [panGestureRec translationInView:self.tableView];
if (distance.y > 0 || distance.y < 0)
{
if (distance.y > 0) // down
{
//NSLog(#"user swiped down");
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else if (distance.y < 0) //up
{
//NSLog(#"user swiped up");
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
return NO;
} else {
return YES;
}
}
return YES;
}
Try this code . This will give smooth animation (Used UIView Animation property)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer class] == [UIPanGestureRecognizer class])
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
UIPanGestureRecognizer *panGestureRec = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint distance = [panGestureRec translationInView:self.tableView];
if (distance.y > 0 || distance.y < 0)
{
if (distance.y > 0) // down
{
//NSLog(#"user swiped down");
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else if (distance.y < 0) //up
{
//NSLog(#"user swiped up");
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
return NO;
} else {
return YES;
}
[UIView commitAnimations];
}
return YES;