多个UIAlertViews输入数据

时间:2013-11-24 00:20:02

标签: ios uialertview

我有两个按钮会弹出一个带文本字段的alertview来输入数据。但是,在两个文本字段中的每一个中仅允许某些字符。不知何故,如果我按下第二个按钮,则使用第一个按钮中的字符集。这里发生了什么?

此外,在不使用alertview的情况下输入数据的更优雅形式是什么?我可以使用模态视图吗?如果是这样,怎么样?

- (IBAction)editRate
{
    if(!self.powerOn) return;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Edit Jail Fee Rate"
                                                    message:@"Enter New Daily Rate"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Ok", nil];


    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];

    [alert show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != alertView.cancelButtonIndex)
    {

        UITextField *field = [alertView textFieldAtIndex:0];
        field.placeholder = @"Enter New Rate";

        NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

        if ([field.text rangeOfCharacterFromSet:set].location != NSNotFound)
        {


            UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Only numbers are allowed in this field."delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [errorAlert show];

            FeeRate.text=@"0.00";
        }
        else
        {

            FeeRate.text = field.text;
            [[NSUserDefaults standardUserDefaults] setObject:field.text forKey:RATE_KEY];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }

    else
    {

    }
}

- (IBAction)editDate
{
    if(!self.powerOn) return;

    UIAlertView *alertDate = [[UIAlertView alloc] initWithTitle:@"Edit Jail Fee Date"
                                                    message:@"Enter New Date"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Ok", nil];


    alertDate.alertViewStyle = UIAlertViewStylePlainTextInput;
    [[alertDate textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];

    [alertDate show];


}


- (void)alertDate:(UIAlertView *)alertDate clickedButtonAtIndex:(NSInteger)buttonIndex2
{
    if (buttonIndex2 != alertDate.cancelButtonIndex)
    {

        UITextField *fieldDate = [alertDate textFieldAtIndex:0];
        fieldDate.placeholder = @"Enter New Date";

        NSCharacterSet * setnew = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789/"] invertedSet];

        if ([fieldDate.text rangeOfCharacterFromSet:setnew].location != NSNotFound)
        {


            UIAlertView *errorAlert1 = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Only numbers and slashes are allowed in this field."delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [errorAlert1 show];

            FeeDate.text=@"00/00/0000";
        }
        else
        {

            FeeDate.text = fieldDate.text;
            [[NSUserDefaults standardUserDefaults] setObject:fieldDate.text forKey:DATE_KEY];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }

    else
    {

    }
}

1 个答案:

答案 0 :(得分:0)

UIAlertView被解雇时,会有一个名为

的函数
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

被召唤。

因此,两个警报视图都会调用此函数。

告诉哪个alertView调用它的一种方法,你可以创建一个ivar或更好的两个属性,如下所示:

@property (nonatomic, strong) UIAlertView *rateAlert;
@property (nonatomic, strong) UIAlertView *dateAlert;

你应该像这样初始化:

[self setRateAlert:[[UIAlertView alloc] initWithTitle...
[self.rateAlert show];

[self setDateAlert:[[UIAlertView alloc] initWithTitle...
[self.dateAlert show];

然后:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView==self.rateAlert) {
        //do whatever for rateAlert
    } else {
        //do whatever with dateAlert
    }
}