2009-05-26 26 views
18

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ư

UIActionSheet example

+0

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

Trả lời

14

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.

9

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.

https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html

2

Để tất cả những người đang tìm kiếm một giải pháp trong Swift:

  1. Áp dụng UIActionSheetDelegate giao thức

  2. Tạ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) 
    
  3. 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; 
        } 
    } 
    
19

Tạo một tấm Action trong Swift

Mã đã được cập nhật cho Swift 3

enter image description here

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.)
+0

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

+0

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 –

5

Đâ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]; 
0

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

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