【发布时间】:2013-02-05 16:09:43
【问题描述】:
我有一个应用程序,它在某种程度上循环遍历 NSSet 的内容,并为集合中找到的每个项目显示一个 UIAlertView。当集合中只有一个项目时, UIAlertView 会正常运行。但是,如果有多个,第一个视图会闪现(通常显示集合中最后一项的内容),然后在没有任何用户干预的情况下消失。然后将显示 NSSet 中的第一项并等待响应,然后再显示 NSSet 中的下一项,依此类推。
这与此未解决问题中描述的体验相同:IPHONE: UIAlertView called twice in a custom function/IBAction
代码如下:
#import "CalcViewController.h"
@interface CalcViewController()
@property (nonatomic) int variablesCount;
@property (nonatomic, strong) NSMutableDictionary *variablesSet;
@end
@implementation CalcViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.variablesSet = [[NSMutableDictionary alloc] init];
}
- (IBAction)variablePressed:(UIButton *)sender
{
[[self calcModel] setVariableAsOperand:sender.titleLabel.text];
self.expressionDisplay.text = [[self calcModel] descriptionOfExpression:self.calcModel.expression];
}
- (IBAction)solveExpressionPressed:(UIButton *)sender {
self.variablesCount = 0;
[self.variablesSet removeAllObjects];
NSSet *variablesCurrentlyInExpression = [[NSSet alloc] initWithSet:[CalcModel variablesInExpression:self.calcModel.expression]];
self.variablesCount = [variablesCurrentlyInExpression count];
if (variablesCurrentlyInExpression){
for (NSString *item in variablesCurrentlyInExpression) {
UIAlertView *alertDialog;
alertDialog = [[UIAlertView alloc] initWithTitle:@"Enter value for variable"
message:item
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
alertDialog.alertViewStyle=UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alertDialog textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[alertDialog show];
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
if ([[alertView textFieldAtIndex:0] text]){
self.variablesSet[alertView.message] = [[alertView textFieldAtIndex:0] text];
}
}
if ([self.variablesSet count] == self.variablesCount){
NSLog(@"time to solve");
[[self calcDisplay] setText:[NSString stringWithFormat:@"%g", [CalcModel evaluateExpression:self.calcModel.expression usingVariableValues:self.variablesSet]]];
}
}
我检查了触发solveExpressionPressed 方法的按钮后面的IBActions,这是唯一存在的。我还在 [alertDialog show] 之前放置了一些日志记录;行,它只在 variablesCurrentlyInExpression NSSet 包含两个值时被调用两次,而 UIAlertView 出现了 3 次(闪烁一次)。
最后,我尝试了没有以下代码:
UITextField * alertTextField = [alertDialog textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
问题依然存在,所以我认为不是这样。
我已经被困了一段时间,还没有弄清楚(因此发帖!!),所以任何帮助都将不胜感激。
谢谢
【问题讨论】:
-
当条件被多次满足时,你想要什么样的警报视图行为?
-
对于表达式中的每个变量,我都会提示用户输入需要分配给该变量的值。然后,我会将这些变量:值对添加到字典中,并将它们传递给模型以求解表达式。有没有更好的方法从用户那里获取一组值(在设计时你不知道表达式中有多少变量)?我会考虑一下你的建议,然后回来看看我是多么成功。干杯
标签: objective-c cocoa-touch ios5