我有一个应用程序部分循环遍历NSSet的内容,并为该集合中的每个项目显示一个UIAlertView。当集合中只有一个项目时,UIAlertView正常运行。但是,如果有多个视图,则第一个视图会闪烁(通常是集合中最后一个项目的内容),然后在没有任何用户干预的情况下消失。 NSSet中的第一项将显示并等待响应,然后显示NSSet中的下一个项目,依此类推。UIAlertView显示两次
这是相同的经验,在这个悬而未决的问题被描述: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显示]之前放置了一些日志记录;行,并且它只在variablesCurrentlyInExpression NSSet包含两个值时调用两次,但UIAlertView出现三次(闪烁一次)。
最后,我已经试过了没有下面的代码:
UITextField * alertTextField = [alertDialog textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
和问题仍然存在,所以我不认为这是。
我一直卡在这一段时间,还没有想通了(因此后!),所以任何帮助将不胜感激。
由于
当不止一次满足条件时,您想要什么警报视图行为? – danh
对于表达式中的每个变量,我提示用户需要将该值分配给该变量。然后,我将这些变量:值对添加到字典中,并将它们传递给模型以解决表达式。有没有更好的方法从用户那里获取一组值(在设计时你不知道表达式中有多少变量)?我会给你的建议一个去,回到我有多成功。欢呼声 – cullener