2014-10-11 3 views
0

오픈 소스 프로젝트에서 동시에 작업하는 동안 github 저장소를 https://github.com/istx25/schedules에서 사용할 수 있습니다. UIActionSheet에서 버튼을 누를 때마다 함수를 실행할 식별자를 케이스에 추가하려고합니다.스위프트 스위치 및 케이스 기능 처리

@IBAction func rightButton(sender : AnyObject) { 
    var sheet: UIActionSheet = UIActionSheet() 
    let title: String = "Please choose a block rotation" 
    sheet.title = title 
    sheet.delegate = self 
    sheet.addButtonWithTitle("Cancel") 
    sheet.addButtonWithTitle("Day Four") 
    sheet.addButtonWithTitle("Day Three") 
    sheet.addButtonWithTitle("Day Two") 
    sheet.addButtonWithTitle("Day One") 
    sheet.cancelButtonIndex = 0 
    sheet.showInView(self.view) 
} 

을 내가 같이하는 어떤 버튼을 연기하기 위해 스위치를 시작했습니다 :

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { 
     switch buttonIndex { 
     case 0: 
      print("Go Back") 
     case 1: 
      print("Day Four") 
      // Day Four Function 
     case 2: 
      print("Day Three") 
      // Day Three Function 
     case 3: 
      print("Day Two") 
      // Day Two Function 
     case 4: 
      print("Day One") 
      // Day One Function 
     default: 
      print("Something's broken") 
     } 
    } 

나는 그것의 푸시하는 각각의 경우를 꿔 다음과 같이 액션 시트의 코드는 자신의 func 방법을 사용하고 정확히 어떻게 접근 할 지 확신하지 못합니다. 아무도 도움이된다면 도움을주십시오. 이 질문을 이해하기 어려운 경우 알려주십시오. 그래서 Stackoverflow에 대한 도움을 청할 때 더 좋아질 수 있습니다! 미리 감사드립니다.

+0

대신에'print ...'호출 대신에 함수를 사용하고 싶습니까? –

답변

-1

별도의 func을 만들고 buttonIndex를 전달할 수 있습니다. 거기에서 당신은 또는 같은 스위치를 할 수 있습니다.

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { 
      switch buttonIndex { 
      case 0: 
       print("Go Back") 
       theFuction(buttonIndex) 
      case 1: 
       print("Day Four") 
       // Day Four Function 
       theFuction(buttonIndex) 
      case 2: 
       print("Day Three") 
       // Day Three Function 
       theFuction(buttonIndex) 
      case 3: 
       print("Day Two") 
       // Day Two Function 
       theFuction(buttonIndex) 
      case 4: 
       print("Day One") 
       // Day One Function 
       theFuction(buttonIndex) 
      default: 
       print("Something's broken") 
      } 
     } 

    func theFuction (btnIndex: Int) { 
     if btnIndex == 0 { 

     } else if btnIndex == 1 { 

     } else if btnIndex == 2 { 

     } else if btnIndex == 3 { 

     } else if btnIndex == 4 { 

     } 
    } 
+1

감사합니다 Eytan, This worked! 도와 주셔서 감사합니다. –

+2

if/elses에서 원하는 메서드를 호출하는 다른 메서드를 사용하는 것의 이점은 스위치의 경우에만 수행하는 것 이상입니까? 이것은 나에게 중복/중복 것처럼 보인다. –

+1

이것은 미친 듯이 ... 잘하면 OP는 배우는 더 좋은 방법을 찾습니다. – Rob

1

스위프트 (아직)에 익숙하지 않지만, 스위치/케이스가있는 이 아닌입니다. 대신 각 버튼은 작업과 연결됩니다. 콜백 일 수 있습니다. 여기에서 UIControl.sendAction 및 관련 코드는 시작할 위치와 비슷합니다.

+0

좋은 추측을했으나 정확하지 않습니다. 설명서를 확인하면 쉽게 찾을 수 있습니다. https://developer.apple.com/library/ios/Documentation/UIKit/Reference/UIModalViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIActionSheetDelegate/actionSheet:clickedButtonAtIndex : –

+0

@AnthonyKong 귀하의 링크는 비추천 API입니다. –

+0

OP 질문에 대한 맥락에서, 그는 'UIActionSheet' –

2

iOS 8을 타겟팅하는 경우 UIActionSheets은 더 이상 사용되지 않으므로 사용해서는 안됩니다. UIAlertController를 UIAlertControllerStyleActionSheet의 preferredStyle과 함께 사용하고 addAction() 메소드를 사용하여 액션을 추가하십시오.