代码之家  ›  专栏  ›  技术社区  ›  Olie

如何移动UIAlertView中的按钮为插入的UIExtField腾出空间?

  •  7
  • Olie  · 技术社区  · 17 年前

    [编辑]嗯。也许这个问题的标题应该是“CoCoTouch中的默认用户输入对话框视图是什么?”我意识到我可以创建一个完全符合我需要的完整视图,并将其包装在视图控制器中并呈现ModalView——但我有点希望有一个标准的、正常的用户输入“对话框”与可可触摸一起出现的视图。“输入您的姓名”、“输入文本进行搜索”等,都是非常常见的事情!

    无论如何这是我最初问的问题:

    UIAlertView* find = [[UIAlertView alloc] init];
    [find setDelegate:self];
    [find setTitle:@"Find"];
    
    [find addButtonWithTitle:@"Cancel"];
    [find addButtonWithTitle:@"Find & Bring"];
    [find addButtonWithTitle:@"Find & Go"];
    [find addButtonWithTitle:@"Go To Next"];
    
    [find addSubview:_findText];
    
    CGRect frm = find.frame;
    int height = frm.size.height + _findText.frame.size.height + 100; // note how even 100 has no effect.
    [find setFrame:CGRectMake(frm.origin.x, frm.origin.y, frm.size.width, height)];
    
    [find setNeedsLayout];
    [find show];
    [find release];
    

    生成此警报视图:

    Find Alert http://www.publicplayground.com/IMGs/Misc/FindAlert.png

    (我从 this question by emi1Faber ,并按广告宣传的那样工作;但是,正如我在注释中所述,“取消”按钮覆盖了文本字段。)

    如何重新排列所有内容以使文本字段正确匹配?[findAlert setNeedsLayout]似乎什么也不做,即使在我[findAlert setFrame:tallerFrame]之后。提示?

    8 回复  |  直到 9 年前
        1
  •  25
  •   Glorfindel Doug L.    7 年前

    向下移动文本视图的最简单(也是最合适的方法)是添加消息

    [find setMessage:@"\n"];
    

    -show

    完整示例:

    // Create Alert
    UIAlertView* av = [UIAlertView new];
    av.title = @"Find";
    // Add Buttons
    [av addButtonWithTitle:@"Cancel"];
    [av addButtonWithTitle:@"Find & Bring"];
    [av addButtonWithTitle:@"Find & Go"];
    [av addButtonWithTitle:@"Go to Next"];
    // Make Space for Text View
    av.message = @"\n";
    // Have Alert View create its view heirarchy, set its frame and begin bounce animation
    [av show];
    // Adjust the frame
    CGRect frame = av.frame;
    frame.origin.y -= 100.0f;
    av.frame = frame;
    // Add Text Field
    UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    text.borderStyle = UITextBorderStyleRoundedRect;
    [av addSubview:text];
    [text becomeFirstResponder];
    

    Preview
    (来源: booleanmagic.com )

        2
  •  6
  •   Stephen Darlington    14 年前

    即使你能让它工作,它也不会是非常iPhone-y的。这个 UIAlertView presentModalViewController: 方法 UIViewController .

    编辑 alertViewStyle 财产)。

    我想如果你需要有四个按钮,然后使用自定义按钮 UIViewController

        3
  •  3
  •   sth    17 年前

    Zoul提出了最好的方法,捕捉用户输入只需做到:

    a) 将UITextFieldDelegate协议添加到类中。

        UIAlertView *insertScore = [UIAlertView new];
        [insertScore setDelegate:self];
        [insertScore setTitle:@"New Title!"];
        [insertScore addButtonWithTitle:@"Cancel"];
        [insertScore addButtonWithTitle:@"Ok"];
    
        insertScore.message = @"\n";
    
        [insertScore addTextFieldWithValue:@"Input" label:@"player"];
    
        [[insertScore textField] setDelegate:self];
    
        [insertScore show]; 
    
        [insertScore release];
    

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
      NSLog(@"%@",[[alertView textField] text]);
    }
    

    希望这有助于某人,因为我必须想一想才能把它做好。

        4
  •  1
  •   zoul    17 年前

    很可能你会想调查一下 addTextFieldWithValue 方法 UIAlertView ? 在类的顶部添加以下代码:

    @interface UIAlertView ()
    - (void) addTextFieldWithValue: (NSString*) val label: (NSString*) label;
    - (UITextField*) textField;
    @end
    

        5
  •  1
  •   oden    16 年前

    说明如何设置尚未测试的列数。

    http://iloveco.de/uikit-alert-types/

    setNumberOfRows:(int)n这将允许 您需要将最大行数设置为 在中显示按钮。用这个 我们需要添加自己的方法 添加到UIAlertView类。我们 为此,请为添加@接口 UIAlertView在我们的.m文件中。

    // extend the UIAlertView class to remove the warning caused
    // by calling setNumberOfRows.
    @interface UIAlertView (extended)
    - (void) setNumberOfRows:(int)num;
    @end
    

    这将允许我们调用该方法,而无需编译器向我们发出警告。

    [myAlert setNumberOfRows:2];
    
        6
  •  0
  •   lostInTransit    17 年前

    请尝试在UIAlertView初始化中的标题后插入一些(\n)。这将按下按钮。我同意斯蒂芬的观点。如果应用程序以不应该的方式使用控件,苹果可能会拒绝该应用程序。(人机界面指南中有相关条款!)

        7
  •  0
  •   zoul    16 年前

    这种更简单的方法适合我:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
        message:@"<Alert message>" delegate:self cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
    [alert addTextFieldWithValue:@"" label:@"Text Field"];
    

    [alert setNumberOfRows:3];
    

    干杯

        8
  •  0
  •   P i    15 年前

    https://github.com/TomSwift/TSAlertView

    这个库实际上是从头开始创建控件,而不是试图攻击UIAlertView,这通常是一个糟糕的计划(TM)