2014-06-11 29 views
10

Tôi tạo ra một tờ hành động, nhưng vấn đề là phương pháp đại biểu không được gọiUIActionSheet với nhanh chóng

myActionSheet = UIActionSheet() 
     myActionSheet.addButtonWithTitle("Add event") 
     myActionSheet.addButtonWithTitle("close") 
     myActionSheet.cancelButtonIndex = 1 
     myActionSheet.showInView(self.view) 

/// UIActionSheetDelegate

func actionSheet(myActionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){ 
     if(myActionSheet.tag == 1){ 

      if (buttonIndex == 0){ 
       println("the index is 0") 
      } 
     } 
} 

tôi đã sử dụng một cách khác mà làm việc tốt với iOS 8 nhưng không hoạt động với iOS 7:

var ActionSheet = UIAlertController(title: "Add View", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet) 

ActionSheet.addAction(UIAlertAction(title: "Add event", style: UIAlertActionStyle.Default, handler:nil)) 

self.presentViewController(ActionSheet, animated: true, completion: nil) 

Bất kỳ ý tưởng nào để khắc phục sự cố?

+4

tờ hành động và xem cảnh báo được khấu hao trong iOS 8 và alertController được giới thiệu. – nikhil84

+3

Vâng nó không được chấp nhận nhưng nếu bạn hỗ trợ ứng dụng của bạn cho iOS 7 thì alertController sẽ không hoạt động. Vì vậy, hãy kiểm tra phiên bản iOS tốt hơn và gọi mã thích hợp cho iOS 8 và iOS 7 –

+0

Hãy xem câu trả lời này - http://stackoverflow.com/questions/27787777/how-to-create-uiactionsheet-actions/27798750#27798750 – Kampai

Trả lời

8

Bạn không bao giờ đặt đại biểu bảng hành động của:

myActionSheet = UIActionSheet() 
myActionSheet.delegate = self 
20

UIActionSheet trong ngôn ngữ nhanh chóng: -

Action tấm với cancelButton và destructiveButton

thiết lập UIActionSheetDelegate.

 let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done") 
     actionSheet.showInView(self.view) 

Action tấm với cancelButton, destructiveButton và otherButton

 let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "Yes", "No") 
     actionSheet.showInView(self.view) 

tạo hàm tấm Action

func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) 
{ 
    switch buttonIndex{ 

    case 0: 
     NSLog("Done"); 
     break; 
    case 1: 
     NSLog("Cancel"); 
     break; 
    case 2: 
     NSLog("Yes"); 
     break; 
    case 3: 
     NSLog("No"); 
     break; 
    default: 
     NSLog("Default"); 
     break; 
     //Some code here.. 

} 
+0

Tôi đã viết một phiên bản Swift không dựa trên các chỉ mục nút cứng được mã hóa cho một câu trả lời khác: http://stackoverflow.com/a/29272140/37168 – stone

+0

Goooood Gooooood: P –

14

UIActionSheet bị phản đối từ iOS8, tôi sẽ khuyên bạn sử dụng UIAlertController nếu bạn không phải hỗ trợ phiên bản bên dưới:

private func presentSettingsActionSheet() { 
    let settingsActionSheet: UIAlertController = UIAlertController(title:nil, message:nil, preferredStyle:UIAlertControllerStyle.ActionSheet) 
    settingsActionSheet.addAction(UIAlertAction(title:"Send Feedback", style:UIAlertActionStyle.Default, handler:{ action in 
    self.presentFeedbackForm() 
    })) 
    settingsActionSheet.addAction(UIAlertAction(title:"Tell Me a Joke!", style:UIAlertActionStyle.Default, handler:{ action in 
    self.presentRandomJoke() 
    })) 
    settingsActionSheet.addAction(UIAlertAction(title:"Cancel", style:UIAlertActionStyle.Cancel, handler:nil)) 
    presentViewController(settingsActionSheet, animated:true, completion:nil) 
} 

Đây là những gì nó trông giống như trình bày:

                                                                          AlertViewController in Swift

0

Cập nhật cho Swift 3:

Nếu bạn muốn hiển thị/mở UIActionSheet trên nút nhấp chuột, sử dụng dưới đây mã đơn giản và được cập nhật trong yourViewController:

// Phương thức xác định:

func showPaymentModeActionSheet() { 

    // 1 
    let optionMenu = UIAlertController(title: nil, message: "Choose Payment Mode", preferredStyle: .actionSheet) 

    // 2 
    let fullAction = UIAlertAction(title: "FULL", style: .default, handler: { 
     (alert: UIAlertAction!) -> Void in 
     self.mPaymentModeTextField.text = "FULL" 

    }) 
    let addvanceAction = UIAlertAction(title: "ADVANCE", style: .default, handler: { 
     (alert: UIAlertAction!) -> Void in 
     self.mPaymentModeTextField.text = "ADVANCE" 
    }) 

    // 
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { 
     (alert: UIAlertAction!) -> Void in 
    }) 


    // 4 
    optionMenu.addAction(fullAction) 
    optionMenu.addAction(addvanceAction) 
    optionMenu.addAction(cancelAction) 

    // 5 
    self.present(optionMenu, animated: true, completion: nil) 
} 

// Method Gọi:

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