我的 xCode 应用程序有问题。
我的申请是关于记录结果的。因此项目值应该是正数。我尝试在选择项目时防止记录负值或零值,但我的代码不起作用。
这是我记录项目的代码
-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle"Outcome Item Value" message"lease enter an outcome item value" delegate:self cancelButtonTitle"Cancel" otherButtonTitles"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
-(void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex
{
if (buttonIndex == 0) {
}
else
{
OutcomeItem* newOutcomeItem = [NSEntityDescription insertNewObjectForEntityForName"OutcomeItem" inManagedObjectContext:self.managedObjectContext];
newOutcomeItem.outcomeName = self.selectedOutcomeItem.outcomeName;
newOutcomeItem.outcomeValue = [NSNumber numberWithDouble:[[alertView textFieldAtIndex:0].text doubleValue]];
if (newOutcomeItem.outcomeValue <= 0) {
NSLog(@"Invalid Value");
[self.navigationController popViewControllerAnimated:YES];
}
else
{
[self.delegate addOutcomeItem:newOutcomeItem];
[self.navigationController popViewControllerAnimated:YES];
}
}
}
我的 OutcomeItem.h 是
#import<Foundation/Foundation.h>
#import<CoreData/CoreData.h>
@class OutcomeRecord;
@interface OutcomeItem : NSManagedObject
@property (nonatomic, retain) NSString *outcomeName;
@property (nonatomic, retain) NSNumber *outcomeValue;
@property (nonatomic, retain) OutcomeRecord *outcomeRecord;
@end;
OutcomeItem.m 是
#import "OutcomeItem.h"
#import "OutcomeRecord.h"
@implementation OutcomeItem
@dynamic outcomeName;
@dynamic outcomeValue;
@dynamic outcomeRecord;
@end
添加结果项的方法是
-(void)addOutcomeItemOutcomeItem *)outcomeItem
{ [self.currentOutcomeRecord addOutcomeItemsObjectutcomeItem]; self.outcomeRecordItemList = [self.currentOutcomeRecord.outcomeItems allObjects];
[self.tableView reloadData];
NSError* error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Could not save outcome item insertion:\n%@", error.userInfo);
}
}
我发现了问题。您需要从 NSNumber 转换为 doubleValue。
NSNumber *outcomeValueX = [NSNumber numberWithDouble:[[alertView textFieldAtIndex:0].text doubleValue]];
if (outcomeValueX.doubleValue <= 0) {
NSLog(@"Invalid Value");
}
理想情况下,我会尝试使用 UITextFieldDelegate 对您的 UITextField 进行验证,并实现 textField:shouldChangeCharactersInRange:replacementString。
在验证值之前,我也不会添加新的结果项。
关于ios - 如何防止记录负数或零数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24243052/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |