Текст Uialertview для NSMutableArray, iOS 4.3 - PullRequest
0 голосов
/ 08 февраля 2012

Я пытался скопировать ввод текста из текстового поля alertview в NSMutableArray, который я буду использовать позже, всплывающее окно alertview выскакивает, и я ввожу ввод в текстовое поле, но когда я нажимаю OK, представление предупреждения исчезает, но не копирует текст в мой изменяемый массив

вот мой код

-(IBAction)add:(UIButton *)sender
{
    addCustomStand = [[NSMutableArray alloc] init];
    UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter a Stand Location"
                                                     message:@"  "   
                                                    delegate:self 
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:@"OK", nil];

    UITextField *nameField = [[UITextField alloc] 
                              initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    [nameField setBackgroundColor:[UIColor whiteColor]];
     nameField.text = @"";
    [dialog addSubview:nameField];

    if ([nameField text]){
        NSLog(@"Name Field %@ ",nameField.text);
        [addCustomStand addObject:nameField.text];
    }

    [nameField release];
    [dialog show];
    [dialog release];   
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"OK"])
    {
        NSLog(@"Button 1 was selected.");
        NSLog(@"StandLocations %@ ",addCustomStand);
    }
}

вот мой вывод на экране журнала

2012-02-07 20:26:57.315 Avicii[1399:b603] Name Field  
2012-02-07 20:26:59.720 Avicii[1399:b603] Button 1 was selected.
2012-02-07 20:26:59.721 Avicii[1399:b603] StandLocations (
    ""
)

кто-нибудь может помочь, что не так с этим кодом?

Ответы [ 2 ]

0 голосов
/ 08 февраля 2012

Это потому, что [nameField text] не имеет введенного пользователем значения, когда вы добавили его в [addCustomStand addObject:nameField.text];

поэтому измените добавление в массив в UIAlertView методе делегата.

-(IBAction)add:(UIButton *)sender
{
    addCustomStand = [[NSMutableArray alloc] init];
    UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter a Stand Location"
                                                     message:@"  "   
                                                    delegate:self 
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:@"OK", nil];

    UITextField *nameField = [[UITextField alloc] 
                              initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    [nameField setBackgroundColor:[UIColor whiteColor]];
    nameField.text = @"";
    // Note at this line
    nameField.tag = 100; 
    //
    [dialog addSubview:nameField];

    [nameField release];
    [dialog show];
    [dialog release];   
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"OK"])
    {
        // Note at this line
        UITextField* nameField = (UITextField *)[alertView viewWithTag:100];
        [addCustomStand addObject:nameField.text];
        //
        NSLog(@"Button 1 was selected.");
        NSLog(@"StandLocations %@ ",addCustomStand);
    }
}
0 голосов
/ 08 февраля 2012

Вы добавляете nameField.text в свой массив addCustomStand, прежде чем вы даже откроете диалоговое окно с предупреждением. Когда вы добавляете его в массив, значением является пустая строка.

Вместо этого вам нужно скопировать значение в ваш массив во время вашего clickedButtonAtIndex: метода, выполнив что-то вроде этого:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"OK"])
    {
        NSString *location;
        UIView *view;
        for (view in [alertView subviews]) {

            if ([view isKindOfClass:[UITextField class]]) {
                location = [(UITextField*)view text];
            }
        }

        if (location) {
            [addCustomStand addObject:location];
        }
    }
}
...