2017-01-13 12 views
1

parentコンテナUIViewには、1つのUITextViewと2つのボタン(ボタンはUITextViewの上にありますが、UIViewのサブビューとして含まれています)があります。親UIViewを通常UIViewControllerの内部に単に表示すると、ボタンは関連付けられたアクションメソッドを完全にトリガし、正しく機能します。しかし、私がUIViewをポップオーバーとなるView Controllerの中に表示すると、ビューの階層は正しく表示されますが、ボタンは関連するアクションメソッドを起動せず何も起こりません。 UIPopoverPresentationControllerとボタンが分かりません。 UIViewControllerを提示親UIViewのUIButtonは正常に動作しますが、popoverPresentationControllerにはありません

func layoutButtons(in parent: UIView) { 
    let speechButton = UIButton(type: .custom) 
    speechButton.frame = CGRect(x: parent.frame.width - 35, y: parent.frame.height - 35, width: 30, height: 30) 
    speechButton.setImage(UIImage(named: "sound_icon"), for: .normal) 
    speechButton.addTarget(self, action: #selector(Textual.textToSpeech), for: UIControlEvents.touchUpInside) 
    parent.addSubview(speechButton) 

    let fontSizeButton = UIButton(type: .custom) 
    fontSizeButton.frame = CGRect(x: textView.frame.width - 75, y: textView.frame.height - 35, width: 30, height: 30) 
    fontSizeButton.setImage(UIImage(named: "font_size_icon"), for: .normal) 
    fontSizeButton.addTarget(self, action: #selector(Textual.toggleFontSize), for: UIControlEvents.touchUpInside) 
    parent.addSubview(fontSizeButton) 

    // wrap text around the bottom buttons 
    let excludeSpeechButton = UIBezierPath(rect: speechButton.frame) 
    let excludeFontSizeButton = UIBezierPath(rect: fontSizeButton.frame) 
    self.textView.textContainer.exclusionPaths = [excludeSpeechButton, excludeFontSizeButton] 
} 

@objc func textToSpeech() { 
    if synth.isPaused { 
     synth.continueSpeaking() 
    } else if synth.isSpeaking { 
     synth.pauseSpeaking(at: .immediate) 
    } else { 
     let speechUtterance = AVSpeechUtterance(string: attributedText.string) 
     speechUtterance.rate = 0.5 
     synth.speak(speechUtterance) 
    } 
} 

@objc func toggleFontSize() { 

    if self.smallFontSizeMode == true { 
     self.smallFontSizeMode = false 
    } else { 
     self.smallFontSizeMode = true 
    } 

    // for each character, multiply the current font size by fontSizeModifier 
    // http://stackoverflow.com/questions/19386849/looping-through-nsattributedstring-attributes-to-increase-font-size 

    self.attributedText.beginEditing() 

    self.attributedText.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, self.attributedText.length), options: NSAttributedString.EnumerationOptions.reverse, using: { (value, range, stop) in 
     if let oldFont = value as? UIFont { 
      var newFont: UIFont? 
      if self.smallFontSizeMode == true { // was big and now toggling to small 
       newFont = oldFont.withSize(oldFont.pointSize/2) 
      } else { // was small and now toggling to big 
       newFont = oldFont.withSize(oldFont.pointSize * 2) 
      } 
      attributedText.removeAttribute(NSFontAttributeName, range: range) 
      attributedText.addAttribute(NSFontAttributeName, value: newFont!, range: range) 
     } 
    }) 

    self.attributedText.endEditing() 

    self.textView.attributedText = self.attributedText 

} 

にボタンを追加する

func present(viewController: UIViewController, at location: CGRect) { 

    viewController.modalPresentationStyle = .popover 
    parentViewController.present(viewController, animated: true, completion: nil) 

    let popController = viewController.popoverPresentationController 
    popController?.permittedArrowDirections = .any 
    popController?.sourceView = parentView 
    popController?.sourceRect = location 

} 

UPDATE - 私はfunc present(viewController:at:)の最後の行として、私がきたの下にpopController?.delegate = viewControllerを追加しました加えてextension UIViewController: UIPopoverPresentationControllerDelegate { }

答えて

0

あなたの質問に答えるために、あなたは委任するあなたの提示ビューコントローラを伝える必要があり、その下にこれを追加し、それが動作するかどうか私に知らせます。

popController?.delegate = viewController 
+0

私はあなたの提案を反映するために質問を更新しました。しかし、 'viewController'をデリゲートとして追加し、' UIViewController'を 'UIPopoverPresentationControllerDelegate'に拡張しても機能しません。 –

+0

あなたのpopoverPresentationControllerがうまく飛び出せば、すべてのUIButtonが動作するはずです。だから、あなたのポップオーバーのソースであるあなたの "viewController"を見ることなく、私はあなたの "viewController"クラスでかなり単純な間違いをしたかもしれないと推測しています。 – Giraffe

関連する問題