I have been trying to get the variable for the keyboardType
self.titleField.keyboardType
When i use nslog it returns about 10 values that are either 0 or 4
I have implemented custom behavior for the number pad keyboard so i need to check if it's the number pad or default keyboard that is showing up.
I have also tried to use self.view.tag to set it by view instead of keyboard.
The nslog for that returns all the tags of the previous view controllers that the application
has been through.
I think this may be because i'm reusing a single view controller class for each separate
controller. I am using a switch statements to determine what
data to display based on the tag.
Is their a way to reset the tag in viewWillDisappear?
Try assigning tag value to textfields and check which textfield became first responder in TF's delegate method -textFieldDidBeginEditing:
The UIKeyboardType enumeration gives names to the keyboard type numbers:
switch (self.titleField.keyboardType) {
case UIKeyboardTypeDefault:
NSLog(#"default keyboard");
break;
case UIKeyboardTypeNumberPad:
NSLog(#"number pad keyboard");
break;
default:
NSLog(#"unexpected keyboard type %d", self.titleField.keyboardType);
break;
}
Related
I have set up a UIPageViewController. I therefore have two UIViewControllers and one UIPageViewController.
I am using this code and I would like to decrease or increase a variable called "character" when the user swipes forward or back.
class PageItemController: UIViewController {
/* As we’ll have only one class for all content controllers, we need to somehow identify different content items. In order to do it, each PageItemController should have its index and image name.*/
var itemIndex: Int = 0 {
didSet {
if itemIndex == 0 {
character = 1
}
if itemIndex == 1 {
character = 2
}
}
}
...
}
What I am doing is:
Tap on the Check button to check the value of the variable "character"
"Character 1" is being displayed
Swipe to the right
Tap on the Check button to check the value of the variable "character" once again
"Character 2" is being displayed
Swipe to the left
Tap on Check button
"Character 2" is being displayed. But wait, that variable character should contain "Character 1" instead!
So why is it happening? It should display character 1 instead of character 2.
If you need more details, check out this video: https://www.dropbox.com/s/gd6c7p0xvclixcr/IMG_0886.mov?dl=0
Check this answer:
https://stackoverflow.com/a/27934069/1135714
You need to workaround your way in by setting up multiple variables and edit their values when some UIPageControllers methods are activated.
I want to have a textfield where the user can input a number and then set a label to that value. Is there any "texfield.value" or something that can fetch the value in numbers?
This is the only piece i heve done. should i put it here?:
- (IBAction)set:(id)sender;
{
}
Thanks in advance
In Xcode, select the textField or textView and change the keyboard type to Numeric.
Then you can get the value of whatever the user inputs into that field by doing:
int value = [[textField.text] intValue];
You can grab the integer value of a textfield using the following snippet.
[textField.text intValue]
Hope this helps.
You can get by textfield.text this will return the text value of that text field , now to convert it to number
NSNumber * mynumber = [NSNumber numberWithInt:[[textfield. text] intValue]];
nslog(#"%#",mynumber);
Here's an example of something I just used:
[self.phoneNumberTextField.text intValue];
You have to be careful with this one. Here are the steps to get this to work
In storyboard, select the textfield
Open the attributes tab for the textfield in Interface Builder
Change keyboard type to "Number Pad" (This guarantees that the user will only be able to enter ints into the textfield)
Now, in your code, type [self.myTextField.text intValue]; (set this equal to a local variable)
Now, as far as WHERE in the code you'll put this, it depends. If you're trying to log in a user, I'd recommend putting this in an IBAction like 'loginButtonPressed'. Or before you transition to a new screen in 'viewWillDisappear' or even 'prepareForSegue'.
Good luck!
This is from 3 years ago, I tried using that and didn't work and so I find this working for me
int i = textfield.text.intValue;
I have created a bar over the keyboard for textfields with previous/next/done button selections. In doing so, I noticed an odd occurance with my tags that I used to navigate between the textfields. I am creating my interface programmatically with a loop, and as such, just set the tag values to the loop variable i.
I started the i variable at 0 so the very first text field created had a tag of zero. Basically what was happening is the 'previous' button functionality would only go so low as 1. It wouldn't even go back to the text field with the 0 tag. The only way to fix this was to increase all tag values by 1 so the first text field started at 1 instead of zero.
Here is my code. Is there a bug in my code that I cannot see? or is this a weird issue with tags?
-(void)gotoPrevTextfield{
// If the active textfield is the first one, can't go to any previous
// field so just return.
UITextField *textField = (UITextField *)[inputsView viewWithTag:0];
NSLog(#"%i",textField.tag);
NSLog(#"%i",txtActiveField.tag);
if (txtActiveField == textField) {
NSLog(#"returning at previous");
return;
}
else {
NSLog(#"set responder");
// Otherwise if a different textfield has the focus, the operation
// of "previous" button can be done and set the previous as the first
// responder.
textField = (UITextField *)[inputsView viewWithTag:txtActiveField.tag - 1];
NSLog(#"%i",textField.tag);
NSLog(#"%i",txtActiveField.tag);
[textField becomeFirstResponder];
}
}
Note that unset tags default to 0 so that is almost a poor choice. You may be getting another view that you don't expect.
A fairly good practice is to add some constant such as 100, consider making the constant a const int or #define for clarity.
I have defined a custom cell with an UISwitch control, is the GetCell method the correct place and correct way to get the values of the cell's control and assign it to a more persistent object than an object from the view? (GetCell method example).
if (indexPath.Section == 0)
{
switch (indexPath.Row)
{
case 0:
TVCellTwoColWBool cell = tableView.DequeueReusableCell(_cIDTwoColWBool) as TVCellTwoColWBool;
if(cell==null)
cell = new TVCellTwoColWBool("Date Filtering", MappedList.DateFilter, _cIDTwoColWBool);
cell.DataView.SWData.ValueChanged += (sender, e) => {MappedList.DateFilter = cell.DataView.SWData.On;};
return cell;
When you create or re-initialize the cell it is a good time to bind the state of any controls in the cell with the actual column/row that you want to attach the behavior to.
Your approach is correct, because it would update the values that you want. But sadly, because you are using ValueChanged as an event, you will be adding a new event handler every time the cell is dequeued.
So you would need to first remove the old event handler, and then add a new event handler. This means that you need to use a helper method for it to allow ValueChanged += FOO and ValueChanged -= FOO
I think this is not GetCell method,this is cellForRowAtIndexPath.
And yes its a correct place to get and paste the values in the cell.
I'm looking for a non-hackish solution for this, so basically -inputView. The part that I'm not sure about is how to make it look like the regular keyboards, from the background to the keys. I realize that I could photoshop an apple keyboard, but this seems like it is a little hackish, especially if apple (probably not but still possible) decides to change the look of their keyboards. I know Numbers has done an excellent job of making extra keyboards that look like the standard system ones, and I would like to do it like those (although obviously they have access to the same resources that made the system keyboards, including possible private frameworks, etc.)
I used the following:
tenDigitKeyboard.m
-(IBAction)pressedKey:(UIButton *)sender
{
[delegate pressedKey:sender.tag];
}
where delegate is defined as `id delegate;
then in the delegate i do...
-(void)pressedKey:(NSInteger)key
{
NSString * bufferString = model.string;
if (key == -1) {//delete
model.string = [bufferString substringWithRange:NSMakeRange(0, [bufferString length]-1)];
}else{
//will need to change the following to lookup key value based on a lookup of the button.tag
model.string = [bufferString stringByAppendingFormat:#"%i",key];
}
[self update];//updates the view
}
I got the keyboard button artwork from: http://www.teehanlax.com/blog/iphone-gui-psd-v4/
Create a view controller and xib. The xib should have 1-9,0 and delete buttons mapped to IBOutlets in your controller. Store and retain the return value string as a property. You can add decimals, etc. if you wish. In the header, store an edition block closure with a property (or alternatively create a delegate or use notification).
#property (copy) void(^valueChangedBlock)(NSString* string);
On touch up, each button sends an event to a method like this:
- (IBAction) pressKey:(id)sender
{
NSString *toAppend;
// Instead of this switch you can store the values in a dictionary mapped by sender.
switch(sender)
{
case oneButton: toAppend=#"1"; break;
case twoButton: toAppend=#"2"; break;
...
}
returnValue = [returnValue appendString:toAppend];
valueChanged(returnValue);
}
Obviously the delete key should remove a character from the end of the string instead of appending. Other than creating the controller and adding this view as the inputView, you should add the valueChangedBlock and set it to update the text field. You may want to put a clear custom button over the text field set to make the field first responder so it doesn't appear as if the user can edit at any point in the string.