Tôi muốn tạo menu bật lên tương tự như menu được tìm thấy trong ứng dụng thư khi bạn muốn trả lời thư. Tôi đã nhìn thấy điều này trong nhiều hơn một ứng dụng vì vậy tôi đã không chắc chắn nếu có cái gì đó được xây dựng trong khuôn khổ cho nó hoặc một số mã ví dụ ra khỏi đó.Tạo Menu bật lên iPhone Tương tự như Menu ứng dụng thư
Trả lời
Kiểm tra ví dụ UICatalog trên trang web của Apple. Phần "Cảnh báo" có các ví dụ về cách sử dụng UIActionSheet để thực hiện những gì bạn đang cố gắng làm.
Nó là một UIAlertController
trên iOS 8+, và một UIActionSheet
trên các phiên bản trước đó.
Bạn cần sử dụng UIActionSheet.
Trước tiên, bạn cần phải thêm UIActionSheetDelegate vào tệp ViewController .h của bạn.
Sau đó, bạn có thể tham khảo một actionsheet với:
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];
Sau đó, bạn phải xử lý mỗi cuộc gọi.
- (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;
}
}
Điều này đã không được chấp nhận như iOS 8.x.
Để tất cả những người đang tìm kiếm một giải pháp trong Swift:
Áp dụng
UIActionSheetDelegate
giao thứcTạo và hiển thị các 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)
Các ủy nhiệm functi vào lúc:
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; } }
Tạo một tấm Action trong Swift
Mã đã được cập nhật cho Swift 3
Kể từ iOS 8, UIAlertController
kết hợp với UIAlertControllerStyle.ActionSheet
được sử dụng . UIActionSheet
không còn được dùng nữa.
Đây là đoạn mã để tạo ra hành động Tấm trong hình trên:
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)
}
}
Vẫn cần giúp đỡ? Xem video hướng dẫn này. Đó là cách tôi đã học nó.
- UIActionSheet example in Swift (Trái ngược với cái tên, nó thực sự không sử dụng tấm
UIAlertController
hành động mới chứ không phải làUIActionSheet
.)
Xin chào @Suragch, tôi thử mã của bạn. Nó hoạt động cho tôi trong iPad và iPod. Tuy nhiên, nút Cancel không xuất hiện trong iPad. Tôi không biết tại sao. Nó cho thấy trong iPod và tôi không sử dụng nút. Tôi sử dụng hình ảnh và thêm chức năng chạm vào nó. Vì vậy, mã của tôi là alertController.popoverPresentationController? .sourceView = self.imgPlay và alertController.popoverPresentationController? .sourceRect = self.imgPlay.bounds. –
Nó hoạt động rất tốt, nhưng chúng ta có thể tùy chỉnh các nút văn bản @Suragch –
Đây là cách bạn muốn làm điều đó trong Objective-C trên 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];
Tôi cố gắng thêm ActionSheet trên quan điểm của tôi. Vì vậy, tôi đã cố gắng tìm giải pháp hoàn hảo nhưng một số câu trả lời khiến tôi bối rối. Bởi vì hầu hết các câu hỏi về tờ Hành động đã được viết quá lâu rồi. Ngoài ra, nó chưa được cập nhật. Dù sao ... Tôi sẽ viết phiên bản ActionSheet cũ và phiên bản cập nhật của ActionSheet. Tôi hy vọng câu trả lời của tôi có thể làm cho bộ não của bạn yên bình.
---------- Phiên bản cập nhật ---------
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;
}
}
- 1. Cách tạo Menu bật lên có Menu phụ trong Java
- 2. Tắt/bật mục menu ứng dụng
- 3. Menu bật lên tự động hoàn tất Emacs bị hỏng
- 4. Thực hiện menu bật lên từ NSButton
- 5. Tạo menu bật lên trong Qt cho QTableView
- 6. Cách thêm các mục trong menu bật lên?
- 7. Hiển thị menu bật lên trên mục `ActionBar` nhấp
- 8. Bật/Tắt mục menu ActionBar
- 9. Tạo menu phụ theo trình tự cho Menu hiện có được tạo từ XML
- 10. GNU emacs tương đương với func-menu
- 11. Cách tạo thanh bên tương tự như ứng dụng facebook cho iOS?
- 12. bật menu trong Wordpress 3 chủ đề
- 13. Tạo một nút menu trong Windows của
- 14. Cách tạo menu thả xuống như "ngon" và "google"?
- 15. Android - Menu trượt với menu phụ
- 16. Bật lên ứng dụng
- 17. Tự động tạo menu trong Tkinter. (biểu thức lambda?)
- 18. Menu chuột phải trong ứng dụng Console
- 19. Cách tạo menu xoay
- 20. Tự động thêm menuitem vào menu mà không đóng menu
- 21. Cách tạo menu bên
- 22. Cách tạo menu phụ tùy chỉnh như ứng dụng Google Chrome
- 23. Làm cách nào để tạo một menu thả xuống như thế này trong ứng dụng Evernote?
- 24. Tạo thanh menu trong WPF?
- 25. Bật mục menu tệp đang mở trong ứng dụng không phải tài liệu
- 26. Tìm kiếm thành phần menu bật lên xuyên tâm cho Delphi
- 27. Tìm hàng JTable mà trên đó một menu bật lên đã được gọi
- 28. Cài đặt mục menu ứng dụng Mac OSX Menu thành khác với "Python" trong ứng dụng Qt trăn của tôi
- 29. Tạo menu thả xuống ActionBar
- 30. Cách làm cho menu tab Vim GUI trông giống như menu tab Console
tài nguyên tốt nhất tôi tìm thấy tại đây http://code.tutsplus.com/tutorials/uiactionsheet-and-uiactionsheetdelegate--mobile-11590 – Nepster