18

enter image description hereUIPopover Làm cách nào để tạo một cửa sổ bật lên với các nút như thế này?

Tôi tự hỏi làm thế nào tôi có thể tạo một cửa sổ bật lên với các nút như thế này.

ĐÁP:

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]; 

Một nơi nào đó khác trong lớp đối tượng ủy nhiệm của bạn ...

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     // take photo... 
    } 
    else if (buttonIndex == 1) { 
     // choose existing photo... 
    } 
} 

Trả lời

43

Đâ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) 
+0

Tôi chỉ cần thêm mục này vào chế độ xem Popover? – ManOx

+0

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ụ –

+0

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

2

Tương tự như các câu trả lời khác, nhưng điều này rất dễ thực hiện so sánh.

Làm cho lớp học của bạn sử dụng UIActionSheetDelegate.

Ví dụ:

@interface ExampleViewController : UIViewController <UIActionSheetDelegate> 

Sau đó thêm vào ExampleViewController.mm/m bạn

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //Get the name of the current pressed button 
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex]; 
if ([buttonTitle isEqualToString:@"Remove"]) { 
    NSLog(@"Remove this actionSheet"); } 
if ([buttonTitle isEqualToString:@"Button 1"]) { 
    NSLog(@"Button 1 pressed"); } 
if ([buttonTitle isEqualToString:@"Button 2"]) { 
    NSLog(@"Button 2 pressed"); } 
if ([buttonTitle isEqualToString:@"Button 3"]) { 
    NSLog(@"Button 3 pressed"); } 
if ([buttonTitle isEqualToString:@"Cancel"]) { 
    NSLog(@"Cancel clicked (anywhere away from it)"); } } 

Bây giờ trong một sự kiện nút bấm hoặc nơi/khi bạn muốn điều này để bật lên gọi như sau:

- (IBAction)aButtonPressed:(id)sender { 
    NSString *actionSheetTitle = @"Action Sheet"; // Title 
    NSString *destroyTitle = @"Destroy"; // Button titles 
    NSString *button1 = @"Button 1"; 
    NSString *button2 = @"Button 2"; 
    NSString *button3 = @"Button 3"; 
    NSString *cancelTitle = @"Cancel"; 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:actionSheetTitle 
                                  delegate:self 
                                   cancelButtonTitle:cancelTitle 
                                  destructiveButtonTitle:destroyTitle 
                                  otherButtonTitles:button1, button2, button3, nil]; [actionSheet showInView:self.view]; 
} 

Và biết thêm thông tin về điều này @: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html

+1

Đừng quên thêm [actionSheet showFromRect: [(UIButton *) frame người gửi] inView: self.view animated: YES]; để đính kèm cửa sổ bật lên vào nút người gửi. –

Các vấn đề liên quan