我想创建一个类似于邮件应用程序中找到的弹出式菜单,当您要回复邮件时。我已经在多个应用程序中看到过这个,所以我不确定是否有框架内置某些东西或者某些示例代码。创建iPhone弹出菜单类似于邮件应用程序菜单
回答
查看Apple网站上的UICatalog示例。 “警报”部分提供了如何使用UIActionSheet来完成您想要做的事情的示例。
这是在iOS 8+一个UIAlertController
,并在早期版本UIActionSheet
。
您需要使用UIActionSheet。
首先,您需要将UIActionSheetDelegate添加到您的ViewController .h文件中。
然后你可以引用actionsheet有:
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];
然后,你必须处理每一个来电的。
- (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;
}
}
从iOS 8.x开始,这已被弃用。
大家谁正在寻找在斯威夫特的解决方案:
采用
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; } }
创建在夫特的动作表
代码已被更新的夫特3
由于iOS 8的,UIAlertController
与UIAlertControllerStyle.ActionSheet
组合使用。 UIActionSheet
已弃用。
这里是产生上述图像中的行动表上的代码:
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: UIAlertControllerStyle.actionSheet)
// blue action button
let blueAction = UIAlertAction(title: "Blue", style: UIAlertActionStyle.default) { (action) in
print("Blue action button tapped")
}
// red action button
let redAction = UIAlertAction(title: "Red", style: UIAlertActionStyle.default) { (action) in
print("Red action button tapped")
}
// yellow action button
let yellowAction = UIAlertAction(title: "Yellow", style: UIAlertActionStyle.default) { (action) in
print("Yellow action button tapped")
}
// cancel action button
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.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)
// support iPads (popover view)
myActionSheet.popoverPresentationController?.sourceView = self.showActionSheetButton
myActionSheet.popoverPresentationController?.sourceRect = self.showActionSheetButton.bounds
// present the action sheet
self.present(myActionSheet, animated: true, completion: nil)
}
}
仍然需要帮助?观看这个视频教程。我就是这样学习的。
- UIActionSheet example in Swift(相反的名称,它实际上是在利用新
UIAlertController
动作片,而不是UIActionSheet
。)
嗨@Suragch,我尝试你的代码。它适用于iPad和iPod。但是,取消按钮不会出现在iPad中。我不知道为什么。它在iPod中显示,我用不按钮。我使用图像并在其上添加水龙头功能。所以,我的代码是alertController.popoverPresentationController?.sourceView = self.imgPlay和 alertController.popoverPresentationController?.sourceRect = self.imgPlay.bounds。 – 2017-03-09 07:03:03
这是你如何做到这一点在Objective-C在iOS 8+:
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];
我尝试添加ActionSheet对我的看法。所以我一直在试图找到完美的解决方案,但一些答案让我感到困惑。因为关于操作表的大部分问题都是很久以前写的,所以还没有更新。 无论如何...我会写旧版本的ActionSheet和更新版本的ActionSheet。 我希望我的回答能够让你的大脑安宁。
----------更新版本---------
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];
-------老版本------
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;
}
}
- 1. OS X应用程序弹出菜单
- 2. Windows 10 UWP应用程序菜单弹出菜单项内
- 3. 如何用Java中的子菜单创建弹出式菜单
- 4. Occipital 360或Facebook Iphone应用程序喜欢弹出菜单
- 5. 创建一个类似于新的YouTube应用程序的菜单
- 6. 创建应用程序状子菜单
- 7. 创建菜单项的弹出窗口(或上下文)菜单
- 8. 创建弹出菜单/侧边栏菜单的问题
- 9. 创建平滑JavaScript弹出菜单
- 10. WPF创建窗口弹出菜单
- 11. 如何创建SDL弹出式菜单?
- 12. Silverlight 4中的菜单控件类似于WPF菜单
- 13. jQuery的拖放创建菜单(类似于WordPress的自定义菜单)
- 14. 如何在WPF中创建类似于Windows 7开始菜单的菜单
- 15. 如何在iPhone应用程序中创建下拉菜单?
- 16. 如何在iPhone应用程序中创建轮播菜单栏
- 17. iPhone SDK:创建菜单
- 18. 类似于iTunes的iOS泡泡弹出菜单
- 19. LinkedIn菜单iPhone应用程序
- 20. iPhone应用程序的滑动菜单
- 21. Android弹出菜单不加载菜单
- 22. CSS弹出菜单与滚动菜单
- 23. 使用弹出菜单创建基于%的警报
- 24. JavaFX的:“关于”菜单项,从“应用程序菜单”在OSX
- 25. Qt - 弹出菜单
- 26. android弹出菜单
- 27. android弹出菜单
- 28. Autocmd弹出菜单
- 29. 创建一个类似于FB和Gmail iPhone应用程序的隐藏菜单,单击按钮,页面滑动一半以显示菜单
- 30. 如何用SEMANTIC中的水平子菜单创建弹出菜单
我在这里找到的最佳资源http://code.tutsplus.com/tutorials/uiactionsheet-and-uiactionsheetdelegate--mobile-11590 – Nepster 2014-08-30 07:14:45