Jump to content
1 post in this topic

Recommended Posts

Hey, I feel like a noob asking this question, in fact I am a noob as far as Objective-c goes. How can I use UIKeyboardTypeDecimalPad with my iOS application? As in the exact code where to put it. Do I need to create a new file?

 

Here's my existing code:

 

SecondViewController.h:

 


#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController {
   IBOutlet UITextField *labelF;
   IBOutlet UITextField *labelC;
   IBOutlet UITextField *labelK;
}
- (IBAction) enterF;
- (IBAction) enterC;
- (IBAction) enterK;

@end

 

 

#import "SecondViewController.h"


@implementation SecondViewController

- (IBAction)enterF {
   //C = ( F - 32) / 1.8
   float c = ([labelF.text floatValue])-32/1.8;
   //K = ( F + 459.67) / 1.8
   float k = (([labelF.text floatValue])+459.67)/1.8;

   //Enter the conversions into labelC and labelK.
   labelC.text = [[NSString alloc] initWithFormat:@"%f", c];
   labelK.text = [[NSString alloc] initWithFormat:@"%f", k];
   labelF.keyboardType = UIKeyboardTypeDecimalPad;
}

- (IBAction)enterC {
   //F = C × 1.8 + 32
   float f = ([labelC.text floatValue])*1.8+32;
   //K = C + 273.15
   float k = ([labelC.text floatValue])+273.15;

   //Enter the conversions into labelF and labelK.
   labelF.text = [[NSString alloc] initWithFormat:@"%f", f];
   labelK.text = [[NSString alloc] initWithFormat:@"%f", k];
   labelC.keyboardType = UIKeyboardTypeDecimalPad;
}

- (IBAction)enterK {
   //F = K × 1.8 - 459.67
   float f = ([labelK.text floatValue])*1.8-459.67;
   //C = K - 273.15
   float c = ([labelK.text floatValue])-273.15;

   //Enter the conversions into labelF and labelC.
   labelF.text = [[NSString alloc] initWithFormat:@"%f", f];
   labelC.text = [[NSString alloc] initWithFormat:@"%f", c];
   labelK.keyboardType = UIKeyboardTypeDecimalPad;
}

- (void)dealloc
{
   [super dealloc];
}

- (void)didReceiveMemoryWarning
{
   // Releases the view if it doesn't have a superview.
   [super didReceiveMemoryWarning];

   // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/

- (void)viewDidUnload
{
   [super viewDidUnload];
   // Release any retained subviews of the main view.
   // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   // Return YES for supported orientations
   return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end

 

I have the actions and outlets connected to three text fields in the nib file.

 

As you can probably see, I'm trying to make a temperature converter.

People need to put in decimal places, hence the need of UIKeyboardTypeDecimalPad.

 

I'm a noob, and asking on Q/A sites is getting me nowhere. If you could just add it to the code or show me what the file would look like, I'll be able to figure out why.Like literally copy the code and put in the code. And then tell me if I need to make new files or connect anything in IB.

I just need those three text boxes, already made in IB, to have the Decimal Pads pop up when you touch into the text boxes.

Thanks. I would also appreciate code to dismiss the decimal pad and to make it so that if someone doesn't have 4.1 so that it will revert to a compatible keyboard, but that's not necessary. All I need is the code for the UIKeyboardTypeDecimalPad.

 

Thank you so much to anyone who considers answering! I've spent hours trying to figure this out and it would be great if anyone can solve. And please don't provide me a link to an explanation, only to solid code that I can see where it it.

Thank you if you even consider answering the question and saving me a few hours!

 

 

Problem solved, I feel even more stupid now. Just needed to add

- (void)viewDidLoad

{

[super viewDidLoad];

labelC.keyboardType=UIKeyboardTypeDecimalPad;

}

Lame. Well, I actually figured out something for myself for once!

Link to comment
https://www.insanelymac.com/forum/topic/255643-uikeyboardtypedecimalpad-help/
Share on other sites

×
×
  • Create New...