Đây là một UIActionSheet
. Trên iPhone, nó hoạt hình từ phía dưới. Trên iPad nó xuất hiện trong một cửa sổ bật lên.
Giả sử bạn đang làm điều này trên báo chí của một nút:
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: @"Take Photo",
@"Choose Existing Photo", nil];
[actionSheet showFromRect: button.frame inView: button.superview animated: YES];
Trong iOS8 +, bạn nên sử dụng UIAlertController
lớp mới:
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
message: nil
preferredStyle: UIAlertControllerStyleActionSheet];
[alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Handle Take Photo here
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Handle Choose Existing Photo here
}]];
alertController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController * popover = alertController.popoverPresentationController;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
popover.sourceView = sender;
popover.sourceRect = sender.bounds;
[self presentViewController: alertController animated: YES completion: nil];
hoặc trong Swift
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
alertController.addAction(UIAlertAction(title: "Take Photo", style: .Default, handler: { alertAction in
// Handle Take Photo here
}))
alertController.addAction(UIAlertAction(title: "Choose Existing Photo", style: .Default, handler: { alertAction in
// Handle Choose Existing Photo
}))
alertController.modalPresentationStyle = .Popover
let popover = alertController.popoverPresentationController!
popover.permittedArrowDirections = .Up
popover.sourceView = sender
popover.sourceRect = sender.bounds
presentViewController(alertController, animated: true, completion: nil)
Nguồn
2012-05-24 19:56:13
Tôi chỉ cần thêm mục này vào chế độ xem Popover? – ManOx
Không, chỉ cần sử dụng một trong các phương thức hiển thị UIActionSheet showFrom .... Xem câu trả lời cập nhật của tôi cho một ví dụ –
OK và 1 câu hỏi khác, làm cách nào để tôi thiết lập trình xử lý sự kiện cho các nút? – ManOx