2017-12-17 24 views
0

テキストビューにNSMutableAttributedStringを追加しています.tappableStringをクリックすると新しいVCを表示します。 VCは提示されていないので、どこで間違っているのか分からない。Swift iOS -NSMutableAttributedString ViewControllerが表示されませんか?

私はTextViewのデリゲートを設定するとのTextViewの...shouldInteractWithURLを使用して、そこの内側に私は、URLの絶対的な文字列が「doSomethingの」ですが、VCが表示されないNSLinkAttributeName's値と一致することを確認してください。

どこが間違っていますか?

class FirstController: UIViewController, UITextViewDelegate { 

    fileprivate let textView: UITextView = { 
     let textView = UITextView() 
     textView.translatesAutoresizingMaskIntoConstraints = false 
     textView.isEditable = false 
     textView.isSelectable = true 
     textView.textAlignment = .center 
     textView.isScrollEnabled = true 
     return textView 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     textView.delegate = self 

     configureTextView() 
    } 

    override func viewDidLayoutSubviews() { 
     textView.setContentOffset(.zero, animated: false) 
    } 

    func configureTextView(){ 

     //textView anchors are set 

     let firstPlainSent = "Read this first.\n" 
     let secondPlainSent = "Read this second.\n" 

     plainSentAttr = [NSFontAttributeName: UIFont(name: "Helvetica", size: 17)!, NSForegroundColorAttributeName: UIColor.black] 

     let firstSent = NSAttributedString(string: firstPlainSent, attributes: plainSentAttr) 
     let secondSent = NSAttributedString(string: secondPlainSent, attributes: plainSentAttr) 

     let mutableAttributedString = NSMutableAttributedString() 
     mutableAttributedString.append(firstSent) 
     mutableAttributedString.append(secondSent) 

     let tappableString = NSMutableAttributedString(string: "Click here to present the SecondVC\n\n") 
     tappableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 17)!, range: NSMakeRange(0, (tappableString.length))) 
     tappableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSMakeRange(0, (tappableString.length))) 
     tappableString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSMakeRange(0,tappableString.length)) 
     tappableString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.blue, range: NSMakeRange(0, tappableString.length)) 
     tappableString.addAttribute(NSLinkAttributeName, value: "doSomething", range: NSMakeRange(0, (tappableString.length))) 

     mutableAttributedString.append(tappableString) 

     textView.attributedText = mutableAttributedString 
    } 


    func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool { 

     // I also tried URL.scheme 
     if URL.absoluteString == "doSomething"{ 

      let secondVC = SecondController() 
      let navVC = UINavigationController(rootViewController: secondVC) 
      present(navVC, animated: true, completion: nil) 
      return false 
     } 

     return true 
    } 
} 
+0

新しいプロジェクトにコードを追加しましたが、私の場合は新しいVCが表示されます!コードの他の部分に何か問題がありますか? –

+0

@AndréSlottaは助けてくれてありがとう。私が間違っていることは分かっていません。私はテキストビューとテキストを見ることができるので、アンカーは問題ありません。それはあなたのために、私のためにはうまくいかないのですか?もう一度もう一度チェックします。 –

+0

@AndréSlotta問題は、私はtextViewのメソッドの古いバージョンを使用していました。助けてくれてありがとう! –

答えて

0

問題は、私はTextViewののデリゲートメソッドの古いメソッドシグネチャ使用した:私はオートコンプリートを使用し、いったん

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool{ 

} 

をより新しいメソッドシグネチャがすべて正常に機能しているように見えました:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 

} 
0

私は「メイン」あなたがここに

をそのストーリーボードの名前を使用してビューコントローラをインスタンス化することを忘れだと思うがストーリーボードの名前です。

let storyboard = UIStoryboard(name: "Main", bundle: nil) 

let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondController") as! SecondController 

ので、最終的なコードは次のようになり、

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondController") as! SecondController 

let navVC = UINavigationController(rootViewController: secondVC) 
present(navVC, animated: true, completion: nil) 
+0

ありがとう、しかしそれはプログラム的です。私はストーリーボードを使用していません –

+0

btwあなたは定数の名前としてUIStoryboardを使うべきではありません。それは小文字でUIから始めるべきではありません。このようなものがもっと適しています:storyboard = UIStoryboard(名前: "Main"、バンドル:nil)、secondVC = storyboard.instantiateViewController ... –

+0

ya right、ありがとう –

関連する問題