UIPickerview can not find the Delegate and DataSource Outlet in Interface Builder - ios

I try to make a UIPickerView show up when the user click on a UITextField, I should see the DataSource and the Delegate Outlets to link them with the picker, however, it doesn't exit when I open the nib file--> click on file owner--> inspector
the second problem is that the keyboard is not hidden when I click on the UItextField although I made a textFieldShouldReturn method which suppose to hide keyboard.
What am I missing here?
the .h file:
#interface RechercherViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate> {
IBOutlet UIPickerView *pickerTypesCarburants;
IBOutlet UIView *pickerViewTypesCarburants;
NSMutableArray *typesCarburantsArray;
IBOutlet UITextField *typeCarburantTextField;
}
-(IBAction)pickerTypeCarburantsShow;
-(IBAction)pickerTypeCarburantsDone;
#end
the .m file :
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [typesCarburantsArray count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component{
return [typesCarburantsArray objectAtIndex:row];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch;
touch=[touches anyObject];
CGPoint point=[touch locationInView:self.view];
if(CGRectContainsPoint([typeCarburantTextField frame],point))
{
[self pickerTypeCarburantsShow];
}
}
-(IBAction)pickerTypeCarburantsDone{
NSInteger selectedRow=[pickerTypesCarburants selectedRowInComponent:0];
NSString *item=[typesCarburantsArray objectAtIndex:selectedRow];
typeCarburantTextField.text=[NSString stringWithFormat:#"%#",item];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 480);
pickerViewTypesCarburants.transform=transform;
[UIView commitAnimations];
}
-(IBAction)pickerTypeCarburantsShow{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 240);
pickerViewTypesCarburants.transform=transform;
[self.view addSubview:pickerViewTypesCarburants];
[UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}

First of all, you probably shouldn't be trying to make a UIPickerView appear when you tap on a UITextField as it isn't standard behaviour (especially if you're suppressing the keyboard). It sounds like you need a standard UIButton that when pressed presents the UIPickerView as this would make more sense.
Regardless, if you aren't seeing Datasource and Delegate outlets in IB, try applying them manually in your code.
pickerTypesCarburants.delegate = self;
pickerTypesCarburants.dataSource = self;
To answer your second question, the keyboard will only hide when you press the return key with the textFieldShouldReturn method you have implemented. The UITextField will also have to have its delegate set (I'm assuming you're doing this in IB as not listed in your .m file). To make the keyboard hide as soon as you press the UITextField, you would to alter your pickerTypeCarburantsShow method:
-(IBAction)pickerTypeCarburantsShow{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 240);
pickerViewTypesCarburants.transform=transform;
[self.view addSubview:pickerViewTypesCarburants];
[UIView commitAnimations];
[typeCarburantTextField resignFirstResponder];
}
This will make sure that the keyboard is hidden immediately (rather than on pressing the return button). Again, I'd question why you would want to make a UIPickerView appear when tapping a UITextField as it probably goes against the Human Interface Guidelines.

Related

UIView automatically moving by uitextfield keyboard

So i have a really weird problem at my hands and hours of search has provided nothing.
I have a uiview containing a uitextfield. Now initially this view is outside of the visible screen with coordinates like x=-500,y=-500.
However in response to a button press this view animates and moves into the center of the screen.
Now whenever i tap on a uitextfield that is the subview of this view.This view moves back to its original coordinates outside the screen.
I have frantically checked my code and there is nothing that is moving the view outside again once its in. Any help in explaining this very unfamiliar behaviour would be really appreciated.
This code moves the view onto the screen
- (IBAction)Register:(id)sender {
//(self.view.frame.size.width/2)-(self.SignUp_Screen.frame.size.width/2);
//self.login_Screen.hidden = YES;
self.blurView.hidden = NO;
//self.SignUp_Screen.layer.zPosition = 5;
NSLog(#"Register");
self.SignUp_Screen.hidden = NO;
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.SignUp_Screen.frame = CGRectMake(35, 50,self.SignUp_Screen.frame.size.width , self.SignUp_Screen.frame.size.height);
} completion:^(BOOL finished) {
}];
}
and these are the delegate methods for the textfield
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(#"TextFieldEndEditing");
[textField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(#"textFieldShouldReturn");
[textField resignFirstResponder];
return YES;
}
As Wezly hints at, if you are using autolayout, you don't modify the frame directly anymore. That's the old world. You want to have an Outlet / property for the constraint and animate it.
[UIView animateWithDuration:0.25
animations:^{
SignUp_Screen.centerXConstraint.constant = ...;
SignUp_Screen.centerYConstraint.constant = ...;
[SignUp_Screen layoutIfNeeded];
}];
See here and here for more details.
You should not modify frame if you are using auto layout. You should animate view by animating constraint's constant. For example:
NSLayoutConstraint *viewY; //constraint from superview top to view top
viewY.constant = 100;
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.3f animations:^{
[self.view layoutIfNeeded];
}];
The way i solved this problem was by linking an IBOutlet to the constraint I wanted to change and then animating it's constant value.
.h
#interface ViewController : UIViewController {
IBOutlet NSLayoutConstraint *constraintHandle;
}
.m
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
constraintHandle.constant = self.view.center.x;
[SignUp_Screen layoutIfNeeded];
} completion:^(BOOL finished) {
}];
Don't forget to link the IBOutlet to your constraint in your storyboard or xib.

move UIScrollView up when UITextField is selected

subview assigned to the UIScrollView. when the UITextField is selected UIScrollView should move up. I tried with below code but its not working.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if( textField == citytextfield)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
//set Y according to keyBoard height
[_scrolling setFrame:CGRectMake(0.0,-220.0,320.0,460.0)];
[UIView commitAnimations];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[_scrolling setFrame:CGRectMake(0.0,0.0,320.0,460.0)];
[UIView commitAnimations];
}
Use setContentOffset method of UIScrollView to make it scroll up, yo will need to change the value 50 to your desired value.
[_scrolling setContentOffset:CGPointMake(0,50) animated:YES];
download the tpkeyboardavoidingscrollview.
give your scrollview's class name to tpkeyboardavoidingscrollview.
#property (weak, nonatomic) IBOutlet TPKeyboardAvoidingScrollView *scrollView;
set the property and give the connection to scrollview.
In your viewcontroller.h
#import "TPKeyboardAvoidingScrollView"
then check it.

How to change UITableView size when UIKeyboard is visible?

I have an application with a UITableView that has UITextFields inside UITableViewCells which the user will have either the virtual UIKeyboard or a Bluetooth keyboard connected to enter text into the UITextFields.
If the Bluetooth keyboard is connected I would like to keep the UITableView full height when selecting and entering text. When there is no Bluetooth keyboard connected if its visible I would like to reduce the UITableview to fit with the UIkeyboard is showing, if its not showing then I would like to make the UITableView full size again.
I have tried to do this by using the UIKeyboard delegate methods
- (void)keyboardDidShow:(NSNotification *)aNotification;
- (void)keyboardDidHide:(NSNotification *)aNotification;
For some reason keyboardDidShow is not accessed ever but keyboardDidHide is accessed when the UIKeyboard is removed from view and I cannot figure out why.
- (void)keyboardDidShow:(NSNotification *)aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.tblView.frame = CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height - 255); // or -216
[UIView commitAnimations];
}
- (void)keyboardDidHide:(NSNotification *)aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.2f];
self.tblView.frame = CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height - height);
[UIView commitAnimations];
}
And in UITextField Delegate Methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:self.tblView];
CGPoint pt = rc.origin;
pt.x = 0;
if(rc.origin.y > 200)
pt.y -= 150;
else
pt.y -= rc.origin.y;
[self.tblView setContentOffset:pt animated:YES];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
use optimized way like this
selectRowAtIndexPath:animated:scrollPosition:
Selects a row in the receiver identified by index path, optionally scrolling the row to a location in the receiver.
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
Special Considerations
Passing UITableViewScrollPositionNone will result in no scrolling, rather than the minimum scrolling described for that constant. To scroll to the newly selected row with minimum scrolling, select the row using this method with UITableViewScrollPositionNone, then call scrollToRowAtIndexPath:atScrollPosition:animated: with UITableViewScrollPositionNone.
NSIndexPath *rowToSelect; // assume this exists and is set properly
UITableView *myTableView; // assume this exists
[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone];
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNone animated:YES];
more understand Setting scroll position in UITableView

How to Show and Hide UIPickerView like iOS Keyboard?

I have this code to hide UIPickerView by default:
- (void)viewDidLoad
{
[super viewDidLoad];
[_memberList setAlpha:0];
}
and this code to show UIPickerView when a button tapped :
- (IBAction)buttonChooseMember {
[UIView animateWithDuration:0.6 delay:0. options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_memberList setAlpha:1];
} completion:nil];
}
and the last thing is this, to hide keyboard when user tap anywhere :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]]) {
[txt resignFirstResponder];
}else if ([txt isKindOfClass:[UIPickerView class]]) {
[UIView animateWithDuration:0.6 delay:0. options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_registerMLMList setAlpha:0];
} completion:nil];
}
}
}
but all of this just give me 'appear' animation, because it's only changing Alpha value from 0 to 1 (and vice versa). not slide-up or slide-down just like iOS keyboard.
I tried to use this animation below to have iOS keyboard look and feel on my UIPickerView :
- (IBAction)hidePicker {
UIPickerView *pickerView = [[UIPickerView alloc] init]; // default frame is set
float pvHeight = pickerView.frame.size.height;
float y = _screen.bounds.size.height - (pvHeight * -2); // the root view of view controller
[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.picker.frame = CGRectMake(0 , y, pickerView.frame.size.width, pvHeight);
} completion:nil];
}
- (IBAction)showPicker {
UIPickerView *pickerView = [[UIPickerView alloc] init]; // default frame is set
float pvHeight = pickerView.frame.size.height;
float y = _screen.bounds.size.height - (pvHeight); // the root view of view controller
[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.picker.frame = CGRectMake(0 , y, pickerView.frame.size.width, pvHeight);
} completion:nil];
}
I like this animation, it looks like iOS keyboard animation, but the problem with this animation is... when my app is loaded, the UIPickerView is already shows up. how to hide it when it loads up for the first time?
thank you.
All UIResponder objects have an inputView property. The inputView of a UIResponder is the view that will be shown in place of the keyboard when the responder becomes the first responder.
So if you want a UIPickerView to show up instead of the keyboard, you could simply do it by making your UIResponder (like a UITextField) have a UIPickerView as its inputView.
(As a caveat: you probably won't want a bare UIPickerView as the inputView, because you also need to account for when the keyboard would change size, like when you rotate. But this is the general idea.)
In viewDidLoad take one boolean variable and set it's value as TRUE and also set the UIPickerView's frame so that UIPickerView is invisible for first time.Based on the boolean value handle the frame animations to show or hide the picker view.
hidepicker and showpicker method idea is good, and the problem of "UIPicker is visible when the app is loaded" can be overcome by just setting the frame of UIPickerView while initiating it to the position such that it should not be visible...after that you can call the showpicker method to show the picker view.

Hiding the Keyboard on button click?

I am creating iPhone app which I show below.
At the end of screen I have text field. I have added a delegate for the same. As it is number pad, I have added button seperately so that when button is clicked, they keyboard is hidden.
Below is the code I have:
.h
#interface SearchViewController : UIViewController<UITextFieldDelegate>
#property (retain, nonatomic) IBOutlet UITextField *textField006;
#property (retain, nonatomic) IBOutlet UIButton *doneButton;
- (IBAction)doneAction:(id)sender;
.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(#"textFieldShouldReturn");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"textFieldDidBeginEditing");
// Ensure the relevant text field is visible
CGAffineTransform translation = CGAffineTransformIdentity;
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenHeight = screenSize.height;
if (screenHeight==480 || screenHeight==568) {
translation = CGAffineTransformMakeTranslation(0, -120);
doneButton.hidden = NO;
NSLog(#"line 3");
[UIView beginAnimations:nil context:nil];
self.view.transform = translation;
[UIView commitAnimations];
}
}
- (IBAction)doneAction:(id)sender {
doneButton.hidden = NO;
doneButton.hidden = YES;
[textField006 resignFirstResponder];
[UIView beginAnimations:nil context:nil];
self.view.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
[self.textField006 resignFirstResponder];
}
Why isn't the keyboard hiding? How can I hide it?
Keyboard == Decimal Pad Return key >> Go Auto-enable Return key =
Ticked
Be sure to use endEditing: if it's not currently hiding correctly.
About endEditing:
"endEditing causes the view (or one of its embedded text fields) to resign the
first responder status."
"This method looks at the current view and its subview hierarchy for
the text field that is currently the first responder. If it finds one,
it asks that text field to resign as first responder. If the force
parameter is set to YES, the text field is never even asked; it is
forced to resign."
So, the following should work (inside your button click action method):
[self.view endEditing:YES];
Swift Version of #lifetimes Answer
self.view.endEditing(true)
it worked perfectly for me
You can also make [self.textField006 resignFirstResponder]

Resources