xcode UIALert查看带文本的多个按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14283857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIALertView multiple buttons with text
提问by user1970792
I have an alertview with 3 buttons and text(label and NOT textView) If I jam it all together, it'll become ugly with this tiny scroll-text and all the buttons taking up most of the space. Does anyone know how to fix this?
我有一个带有 3 个按钮和文本(标签而不是文本视图)的警报视图,如果我将它们全部塞在一起,这个微小的滚动文本和所有按钮占据大部分空间会变得丑陋。有谁知道如何解决这一问题?
回答by Anoop Vaidya
Why don't you try to create a custom View for this.
为什么不尝试为此创建自定义视图。
You can use as you want to customize, size, color, background etc.
您可以根据需要自定义大小、颜色、背景等。
And show it as a modal window/view.
并将其显示为模态窗口/视图。
If you still want to use alertView then you can give spacing as:
如果您仍然想使用 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];
回答by Satish Azad
A simple way to move the text view down is to add a message
向下移动文本视图的一种简单方法是添加一条消息
[alertViewObject setMessage:@"\n"];
THe reason for your frame isn't taking effect is that -show sets the frame and creates the view hierarchy before starting the animation. You should also make the text view the first responder so the keyboard pops up.
您的框架未生效的原因是 -show 在开始动画之前设置框架并创建视图层次结构。您还应该使文本视图成为第一响应者,以便弹出键盘。
Customize your AlertView by using following Code.
使用以下代码自定义您的 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];
I hope this will help you.
我希望这能帮到您。
回答by user3761851
UIAlertView was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.
UIAlertView 在 iOS 9.0 中被弃用。使用 UIAlertController 和首选样式 UIAlertControllerStyleAlert 代替。
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")
}))
回答by JomanJi
I suggest that you make two uiaertviews and then use
我建议你制作两个uiaertviews然后使用
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
to check which alert view was clicked. If you really need an uialertview, else the answer below is also great.
检查点击了哪个警报视图。如果您真的需要 uialertview,否则下面的答案也很棒。
Detailed:
详细的:
Add a uialertview delegate to your view controller:
@interface ViewController : UIViewController <UIAlertViewDelegate>
When creating the 2 alertViews it should look like this:
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"the first UIAlertView" delegate: self cancelButtonTitle:@"Back" otherButtonTitle:@"More Options", nil];
将 uialertview 委托添加到您的视图控制器:
@interface ViewController : UIViewController <UIAlertViewDelegate>
创建 2 个 alertViews 时,它应该如下所示:
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"the first UIAlertView" delegate: self cancelButtonTitle:@"Back" otherButtonTitle:@"More Options", nil];
and the second:
第二个:
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"More Options" message:nil delegate:self cancelButtonTitle:@"Back" otherButtonTitle:@"Option 1", @"Option 2", @"Option 3", nil];
In your ViewController.m add:
- (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 } }
在您的 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 } }