English 中文(简体)
添加两个数字,使用文本领域
原标题:addition of two numbers using textfield

I am new to objective c,so plz me help with this... i have used three textfields in the interface builder textfield1 inputs the first number textfield2 inputs the second number

and i have created a button called sum which will calculate the sum and textfield(ans) will display the result

.h 档案编码

 {
      IBOutlet UITextfield txtfield1;
      IBOutlet UITextfield txtfield2;
      IBOutlet UITextfield ans;
 }
 -(IBAction)add;

m 文档代码

 -(IBAction)add
  {
      int result=[txtfield1.text intValue]+[txtfield2.text intValue];
      ans.text=[NSString StringWithFormat:@"%d",result];
  }

该方案实行罚款,但只想在文字领域输入立体值,如果是进入字母顺序,点击总吨数,即像 an子一样,那么用户在投入字母顺序时会发现错误。

最佳回答

-textField:shouldChangeCharactersInRange:replacementString: is a delegate method that let s you allow only certain characters to be in the text field.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
    NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
}

因此,只允许在小数体内<>而不是的特性。 DigitCharacter 3. 定性。

问题回答

这样做的最佳途径是只将钥匙板改为数字板,而且你可以行使从Interface Buildinger到UIText Field的产权,接近文本的底层,你将看到该选项的关键板,将其改为:Pad

你们必须使用UIText FieldDelegate 方法[文本是“SchouldReturn:]在你的控制人中。

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSDecimalNumber *n = [NSDecimalNumber decimalNumberWithString:textField.text];
    if( [n notANumber] ) {
       // This means the user entered an invalid number;
       return NO;
    }
    return YES; // Do something with the number it is valid.
}

这项工作将完成。

创建<条码>NSText FieldCell子级(或适用于OS;以下代码对纯可可有效100%),如:

<>Interface>

#import <AppKit/AppKit.h>

@interface decimalCell : NSTextFieldCell

@end

<>执行>

#import "decimalCell.h"
#import "NumberFormatter.h"

@implementation decimalCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void) awakeFromNib{

    NumberFormatter *formatter = [NumberFormatter withAllowedCharacters:@"0123456789"];
    [self setFormatter:formatter];

}

@end




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签