ios 创建类似于邮件应用程序菜单的 iPhone 弹出菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/911137/
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
Creating iPhone Pop-up Menu Similar to Mail App Menu
提问by LeeMobile
I'd like to create a pop-up menu similar to the one found in the mail app when you want to reply to a message. I've seen this in more than one application so I wasn't sure if there was something built into the framework for it or some example code out there.
当您想回复消息时,我想创建一个类似于邮件应用程序中的弹出菜单。我在不止一个应用程序中看到过这种情况,所以我不确定框架中是否内置了一些东西或一些示例代码。
采纳答案by Mike
Check out the UICatalog example on Apple's website. The "Alerts" section has examples of how to use UIActionSheet to accomplish what you're trying to do.
查看 Apple 网站上的 UICatalog 示例。“警报”部分提供了如何使用 UIActionSheet 完成您要执行的操作的示例。
回答by Suragch
Creating an Action Sheet in Swift
在 Swift 中创建操作表
Code has been tested with Swift 5
代码已经过 Swift 5 测试
Since iOS 8, UIAlertController
combined with UIAlertControllerStyle.ActionSheet
is used. UIActionSheet
is deprecated.
从 iOS 8 开始,UIAlertController
结合UIAlertControllerStyle.ActionSheet
使用。UIActionSheet
已弃用。
Here is the code to produce the Action Sheet in the above image:
这是生成上图中操作表的代码:
class ViewController: UIViewController {
@IBOutlet weak var showActionSheetButton: UIButton!
@IBAction func showActionSheetButtonTapped(sender: UIButton) {
// Create the action sheet
let myActionSheet = UIAlertController(title: "Color", message: "What color would you like?", preferredStyle: UIAlertController.Style.actionSheet)
// blue action button
let blueAction = UIAlertAction(title: "Blue", style: UIAlertAction.Style.default) { (action) in
print("Blue action button tapped")
}
// red action button
let redAction = UIAlertAction(title: "Red", style: UIAlertAction.Style.default) { (action) in
print("Red action button tapped")
}
// yellow action button
let yellowAction = UIAlertAction(title: "Yellow", style: UIAlertAction.Style.default) { (action) in
print("Yellow action button tapped")
}
// cancel action button
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) { (action) in
print("Cancel action button tapped")
}
// add action buttons to action sheet
myActionSheet.addAction(blueAction)
myActionSheet.addAction(redAction)
myActionSheet.addAction(yellowAction)
myActionSheet.addAction(cancelAction)
// present the action sheet
self.present(myActionSheet, animated: true, completion: nil)
}
}
Still need help? Watch this video tutorial. That's how I learned it.
还需要帮助吗?观看此视频教程。我就是这样学会的。
- UIActionSheet example in Swift(Contrary to the name, it actually does use the new
UIAlertController
action sheet rather thanUIActionSheet
.)
- Swift 中的 UIActionSheet 示例(与名称相反,它实际上确实使用了新的
UIAlertController
操作表而不是UIActionSheet
.)
回答by Rémy
It is a UIAlertController
on iOS 8+, and a UIActionSheet
on earlier versions.
它适用UIAlertController
于 iOS 8+,适用UIActionSheet
于早期版本。
回答by Apollo SOFTWARE
You need to use a UIActionSheet.
您需要使用 UIActionSheet。
First you need to add UIActionSheetDelegate to your ViewController .h file.
首先,您需要将 UIActionSheetDelegate 添加到您的 ViewController .h 文件中。
Then you can reference an actionsheet with:
然后,您可以使用以下内容引用操作表:
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Select Sharing option:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:
@"Share on Facebook",
@"Share on Twitter",
@"Share via E-mail",
@"Save to Camera Roll",
@"Rate this App",
nil];
popup.tag = 1;
[popup showInView:self.view];
Then you have to handle each of the calls.
然后你必须处理每一个调用。
- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (popup.tag) {
case 1: {
switch (buttonIndex) {
case 0:
[self FBShare];
break;
case 1:
[self TwitterShare];
break;
case 2:
[self emailContent];
break;
case 3:
[self saveContent];
break;
case 4:
[self rateAppYes];
break;
default:
break;
}
break;
}
default:
break;
}
}
This has been deprecated as of iOS 8.x.
从 iOS 8.x 开始,这已被弃用。
回答by Stunner
This is how you'd do it in Objective-C on iOS 8+:
这是你在 iOS 8+ 上的 Objective-C 中的做法:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Directions"
message:@"Select mode of transportation:"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *drivingAction = [UIAlertAction actionWithTitle:@"Driving" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// this block runs when the driving option is selected
}];
UIAlertAction *walkingAction = [UIAlertAction actionWithTitle:@"Walking" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// this block runs when the walking option is selected
}];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:drivingAction];
[alert addAction:walkingAction];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
回答by Apfelsaft
To everybody who is looking for a solution in Swift:
致所有在 Swift 中寻找解决方案的人:
Adopt
UIActionSheetDelegate
protocolCreate and show the ActinSheet:
let sheet: UIActionSheet = UIActionSheet() sheet.addButtonWithTitle("button 1") sheet.addButtonWithTitle("button 2") sheet.addButtonWithTitle("button 3") sheet.addButtonWithTitle("Cancel") sheet.cancelButtonIndex = sheet.numberOfButtons - 1 sheet.delegate = self sheet.showInView(self.view)
The delegate function:
func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){ switch buttonIndex{ case 0: NSLog("button1"); case 1: NSLog("button2"); case 2: NSLog("button3"); case actionSheet.cancelButtonIndex: NSLog("cancel"); break; default: NSLog("blub"); break; } }
采用
UIActionSheetDelegate
协议创建并显示 ActinSheet:
let sheet: UIActionSheet = UIActionSheet() sheet.addButtonWithTitle("button 1") sheet.addButtonWithTitle("button 2") sheet.addButtonWithTitle("button 3") sheet.addButtonWithTitle("Cancel") sheet.cancelButtonIndex = sheet.numberOfButtons - 1 sheet.delegate = self sheet.showInView(self.view)
委托函数:
func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){ switch buttonIndex{ case 0: NSLog("button1"); case 1: NSLog("button2"); case 2: NSLog("button3"); case actionSheet.cancelButtonIndex: NSLog("cancel"); break; default: NSLog("blub"); break; } }
回答by User18474728
I tried to add ActionSheet on my view. So I've been trying to find perfect solution but some answers made me confused. Because most of questions about Action sheet were written so long time ago.Also it has not been updated. Anyway...I will write old version ActionSheet and updated version ActionSheet. I hope my answer is able to make your brain peaceful.
我试图在我的视图中添加 ActionSheet。所以我一直试图找到完美的解决方案,但有些答案让我感到困惑。因为Action sheet的大部分问题都是很久以前写的,而且一直没有更新。无论如何......我会写旧版本的ActionSheet和更新版本的ActionSheet。希望我的回答能让你的大脑平静下来。
----------Updated Version---------
- - - - - 更新后的版本 - - - - -
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:@"writeMessageOrsetAsNil" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* actionSheet01 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { NSLog(@"OK click");}];
UIAlertAction* actionSheet02 = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {NSLog(@"OK click");}];
UIAlertAction* actionSheet03 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
NSLog(@"Cancel click");}];
[browserAlertController addAction:actionSheet01];
[browserAlertController addAction:actionSheet02];
[browserAlertController addAction:actionSheet03];
[self presentViewController:browserAlertController animated:YES completion:nil];
-------Old Version------
- - - -旧版本 - - -
UIActionSheet *actionSheet= [[UIActionSheet alloc] initWithTitle:@"Select Sharing option:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@“OK”, @“NO”,@“Cancel”,
nil];
actionSheet.tag = 100;
[actionSheet showInView:self.view];
- (void)actionSheet:(UIActionSheet *)actionShee clickedButtonAtIndex:(NSInteger)buttonIndex {
if( actionSheet.tag == 100){
switch (buttonIndex) {
case 0:
[self doSomething];
break;
case 1:
[self doAnything];
break;
case 2:
[self doNothing];
break;
default:
break;
}
break;
}
}