Uialertview文本到NSMutableArray,ios 4.3

时间:2012-02-08 01:31:31

标签: ios nsmutablearray uialertview

我一直在尝试将来自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 :(得分:0)

您甚至在显示警告对话框之前将nameField.text添加到a​​ddCustomStand数组。在将其添加到数组时,该值为空字符串。

相反,您需要在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];
        }
    }
}

答案 1 :(得分:0)

这是因为[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); } } 委托方法中更改您对数组的添加。

{{1}}