Uialertview 文本到 NSMutableArray,ios 4.3

发布于 2025-01-03 15:41:57 字数 1640 浏览 4 评论 0原文

我一直在尝试将文本输入从alertview文本字段复制到我稍后将使用的NSMutableArray,alertview弹出,我将输入输入到文本字段,但是当我按“确定”时,警报视图消失,但不将文本复制到我的可变数组,

这是我的 这里的代码

-(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 (
    ""
)

任何人都可以帮助该代码有什么问题吗?

i have been trying to copy text input from alertview textfield to a NSMutableArray that i will use later, alertview pops up and i enter the input to text field but when i press OK alert view disappears but doesnt copy text to my mutable array

here is my code

-(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);
    }
}

here is my output on log screen

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 (
    ""
)

anyone can help whats wrong with that code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2025-01-10 15:41:57

这是因为当您将其添加到 [addCustomStand addObject:nameField.text]; 中时,[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);
    }
}

That's because [nameField text] doesn't have user entered value yet when you added it in your [addCustomStand addObject:nameField.text];

so change your adding into array in UIAlertView delegate method.

-(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);
    }
}
沩ん囻菔务 2025-01-10 15:41:57

在显示警报对话框之前,您需要将 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];
        }
    }
}

You are adding nameField.text to your addCustomStand array before you even show the alert dialog. At the time you add it to the array the value is an empty string.

Instead you need to copy the value into your array during your clickedButtonAtIndex: method, by doing something like this:

- (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];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文