gpt4 book ai didi

objective-c - 实时计算器

转载 作者:行者123 更新时间:2023-11-29 11:10:52 24 4
gpt4 key购买 nike

学校作业要求我制作一个计算器应用程序,与 Spotlight 计算器相同。它实时工作,没有用于开始操作的按钮。

到目前为止,这是我的代码。它写在带有事件 Editing Did End 的文本字段中。我很确定那是错误的,但我找不到替代解决方案。此外,我还没有得到实时的东西,所以我有点恢复到在按下文本字段时完成以下步骤。

- (IBAction)Didend_Action:(id)sender {
NSString *list = [Sum_TextField text];
NSArray *listItemsArray = [list componentsSeparatedByString:@" "];
float firstNumber = [[listItemsArray objectAtIndex: 0] floatValue];
NSString *symbol = [listItemsArray objectAtIndex: 1];
float secondNumber = [[listItemsArray objectAtIndex: 2] floatValue];
{
Calculator* calc = [[Calculator alloc] init];
[calc setNum1:firstNumber];
[calc setNum2:secondNumber];
if ([symbol isEqualToString:@"-"])
{
[calc minus];
}
else if ([symbol isEqualToString:@"+"])
{
[calc add];
}
if ([symbol isEqualToString:@"*"])
{
[calc multiply];
}
else if ([symbol isEqualToString:@"/"])
{
[calc divide];

}
[Answer_TextField setText:[NSString stringWithFormat:@"%d", [calc answer]]];
}
}

最佳答案

我认为更好的方法是实现 UITextViewDelegate 协议(protocol)方法,如 textViewDidChange:。例如,您可以这样做:

- (void)textViewDidChange:(UITextView *)textView {

NSString *currentText = [textview text];
NSArray *currentItems = [currentText componenetsSeparatedByString:@" "];
float result = 0.0;

//If a valid expression is in the text view
if([currentItems count] > 2) {

float num1 = [[currentItems objectAtIndex:0] floatValue];
float num2 = [[currentItems objectAtIndex:2] floatValue];
NSString *operator = [currentItems objectAtIndex:1];

if([operator isEqualToString:@"+"]) {

result = num1 + num2;
answerTextField.text = [NSString stringWithFormat:@"%f", result];
}
else if([operator isEqualToString:@"-"]) {

result = num1 - num2;
answerTextField.text = [NSString stringWithFormat:@"%f", result];
}
else if([operator isEqualToString:@"*"]) {

result = num1 * num2;
answerTextField.text = [NSString stringWithFormat:@"%f", result];
}
else if([operator isEqualToString:@"/"]) {

result = num1 / num2;
answerTextField.text = [NSString stringWithFormat:@"%f", result];
}
else{

answerTextField.text = @"Invalid Operation";
}
}
}

每次用户在 TextView 中编辑文本时都会调用此方法。它应该可以工作,但我没有测试过。确保在这段代码所在的任何文件的标题中,您都这样做:

@interface yourClassName : yourSuperclass <UITextViewDelegate> {

//Your instance variables
}

//Your method and property declarations

编辑:

假设我将 - (void)textViewDidChange:(UITextView *)textView 代码放在一个名为 MyClass.m 的文件中。文件 MyClass.m 将如下所示:

@implementation MyClass

- (void)textViewDidChange:(UITextView *)textView {

//All the above code goes here
}

- (void)viewDidLoad
{
[super viewDidLoad];
//INCLUDE THESE LINES
Sum_TextField.delegate = self;
Answer_TextField.delegate = self;
}

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

@end

在头文件 (MyClass.h) 中,我会这样写:

@interface MyClass : UIViewController <UITextViewDelegate> 

//Note: you don't declare - (void)textViewDidChange:(UITextView *)textView in the header file because you are implementing
//a protocol method.

//MAKE SURE SUM_TEXTFIELD AND ANSWER_TEXTFIELD ARE UITEXTVIEWS NOT UITEXTFIELDS
@property (strong, nonatomic) IBOutlet UITextView *Sum_TextField;
@property (strong, nonatomic) IBOutlet UITextView *Answer_TextField;

@end

希望这对您有所帮助!

关于objective-c - 实时计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11642800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com