UIALertView带有文本的多个按钮

时间:2013-01-11 17:50:39

标签: iphone xcode button alertview

我有一个带有3个按钮和文本的警报视图(标签而不是textView)如果我将它们全部卡在一起,这个微小的滚动文本和占据大部分空间的所有按钮都会变得难看。有谁知道如何解决这个问题?

4 个答案:

答案 0 :(得分:7)

为什么不尝试为此创建自定义视图。

您可以根据需要使用自定义,尺寸,颜色,背景等。

并将其显示为模态窗口/视图。


如果您仍想使用alertView,则可以将间距设为:

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Title" message:@"\n\n!\n\n\n\n" delegate:self cancelButtonTitle:@"Button 1" otherButtonTitles:@"Button 2",@"Button 3", nil];
[alert show];

答案 1 :(得分:5)

向下移动文本视图的一种简单方法是添加消息

[alertViewObject setMessage:@"\n"];

您的框架未生效的原因是-show在开始动画之前设置框架并创建视图层次结构。您还应该使文本视图成为第一个响应者,以便弹出键盘。

使用以下代码自定义您的AlertView。

// Customize Alert View
UIAlertView *alertView = [UIAlertView new];
alertView.title = @"Alert Title";


// Adding Your Buttons
[alertView addButtonWithTitle:@"Button1"];
[alertView addButtonWithTitle:@"Button2"];
[alertView addButtonWithTitle:@"Button3"];
[alertView addButtonWithTitle:@"Button4"];


// Add a Space for Text View
alertView.message = @"\n";


// View heirarchy, set its frame and begin bounce animation
[alertView show];


// Set the frame
CGRect frame = alertView.frame;
frame.origin.y -= 100.0f;
alertView.frame = frame;


// Adding TextField
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[alertView addSubview:text];
[text becomeFirstResponder]; 

我希望这会对你有所帮助。

答案 2 :(得分:0)

我建议您制作两个uiaertviews,然后使用

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

检查点击了哪个警报视图。如果你真的需要一个uialertview,否则下面的答案也很棒。

详细说明:

  1. 将uialertview委托添加到视图控制器:

    @interface ViewController : UIViewController <UIAlertViewDelegate>    
    
  2. 创建2个alertView时,它应如下所示:

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"the first UIAlertView" delegate: self cancelButtonTitle:@"Back" otherButtonTitle:@"More Options", nil];
    
  3. 和第二个:

        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"More Options" message:nil delegate:self cancelButtonTitle:@"Back" otherButtonTitle:@"Option 1", @"Option 2", @"Option 3", nil];
    
    1. 在ViewController.m中添加:

      - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
      
      if (alertView == alert1) {
        [alert2 show];
      }
      
      else if (alertView == alert2) {        
      
      NSString *string = [alertView buttonTitleAtIndex:buttonIndex];
      
      if ([string isEqualToString:@"Option 1"]) {
      
          //Do stuff
      
      }
      else if ([string isEqualToString:@"Option 2"]) {
      
          //Do something else
      }
      else if ([string isEqualToString:@"Option 3"]) {
      
          //Do something 3rd
      }
      }
      

答案 3 :(得分:0)

UIAlertView在iOS 9.0中已弃用。 改将UIAlertController与UIAlertControllerStyleAlert的preferredStyle一起使用。

    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    alertController.addAction(UIAlertAction(title: "AlertAction 0", style: .default, handler: { (alertAction: UIAlertAction) in
        print("0")
    }))

    alertController.addAction(UIAlertAction(title: "AlertAction 1", style: .default, handler: { (alertAction: UIAlertAction) in
        print("1")
    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertAction: UIAlertAction) in
        print("cancel")
    }))