我想創建一個類似於郵件應用程序中找到的彈出式菜單,當您要回復郵件時。我已經在多個應用程序中看到過這個,所以我不確定是否有框架內置某些東西或者某些示例代碼。創建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. 創建類似於iOS7的菜單選擇菜單
- 2. OS X應用程序彈出菜單
- 3. Windows 10 UWP應用程序菜單彈出菜單項內
- 4. 如何創建彈出式應用程序菜單
- 5. 適用於iPhone應用程序的彈性菜單窗口
- 6. 如何用Java中的子菜單創建彈出式菜單
- 7. 如何創建彈出菜單的另一彈出菜單的菜單項的子菜單
- 8. Occipital 360或Facebook Iphone應用程序喜歡彈出菜單
- 9. 使用c創建菜單彈出框#
- 10. 創建一個類似於新的YouTube應用程序的菜單
- 11. 創建應用程序狀子菜單
- 12. 在jTree上創建一個右鍵菜單彈出菜單
- 13. 創建彈出菜單/側邊欄菜單的問題
- 14. 如何爲彈出菜單創建子菜單?
- 15. 創建菜單項的彈出窗口(或上下文)菜單
- 16. Android上下文菜單(類似於音樂應用程序)
- 17. 如何創建彈出菜單
- 18. 創建一個JQuery菜單彈出
- 19. WPF創建窗口彈出菜單
- 20. 創建平滑JavaScript彈出菜單
- 21. 如何創建SDL彈出式菜單?
- 22. 建議彈出菜單drracket
- 23. 彈出菜單禁用Mac上的Java應用程序的菜單
- 24. 如何在iPhone應用程序中創建輪播菜單欄
- 25. 如何在iPhone應用程序中創建下拉菜單?
- 26. 創建響應菜單和子菜單
- 27. iPhone SDK:創建菜單
- 28. Silverlight 4中的菜單控件類似於WPF菜單
- 29. 如何在WPF中創建類似於Windows 7開始菜單的菜單
- 30. jQuery的拖放創建菜單(類似於WordPress的自定義菜單)
我在這裏找到的最佳資源http://code.tutsplus.com/tutorials/uiactionsheet-and-uiactionsheetdelegate--mobile-11590 – Nepster 2014-08-30 07:14:45