NSSet のコンテンツを部分的にループし、セット内で見つかった各アイテムの UIAlertView を表示するアプリケーションがあります。セットに項目が 1 つしかない場合、UIAlertView は適切に動作します。ただし、複数ある場合は、最初のビューがフラッシュアップし (通常はセット内の最後のアイテムの内容で)、ユーザーの介入なしに消えます。NSSet の最初の項目が表示され、応答を待ってから、NSSet の次の項目が表示されます。
これは、この未解決の質問で説明されているのと同じエクスペリエンスです: IPHONE: カスタム関数/IBAction で UIAlertView が 2 回呼び出されました
コードは次のとおりです。
#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 に 2 つの値が含まれている場合に 2 回だけ呼び出されますが、UIAlertView は 3 回表示されます (1 回点滅します)。
最後に、次のコードなしで試しました:
UITextField * alertTextField = [alertDialog textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
問題はまだ発生しているので、そうではないと思います。
私はこれにしばらく立ち往生していて、それを理解していませんでした(したがって、投稿!!)ので、どんな助けも大歓迎です.
ありがとう