-3

I have a UIView where will i write actions for buttons. In XIB file I have like a bar that is above the keyboard with those buttons. How to connect these buttons to ViewController that has UITextView and when button is clicked action should be done in UITextView (example: When i click the "Bold" button, selected text in UITextView should be bold) enter image description here [![enter image description here][2]][2]

import UIKit

protocol NoteViewDelegate {

    func didUpdateNoteWithTitle(newTitle : String, andBody newBody : String)
}

class NewNoteViewController: UIViewController , UITextViewDelegate {

    var delegate : NoteViewDelegate?


    @IBOutlet weak var textBody: UITextView!

    var strBodyText : String!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.textBody.text = self.strBodyText

        self.textBody.becomeFirstResponder()

        self.textBody.delegate = self

        NotificationCenter.default.addObserver(self, selector: #selector(NewNoteViewController.updateTextView(notification:)) , name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(NewNoteViewController.updateTextView(notification:)) , name: Notification.Name.UIKeyboardWillHide, object: nil)

        textBody.inputAccessoryView = Bundle.main.loadNibNamed("CustomAccessoryView", owner: self, options: nil)?.first as? UIView
}

    func updateTextView (notification:Notification) {
        let userInfo = notification.userInfo!

        let keyboardEndFrameScreenCoordinates = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let keyboardEndFrame = self.view.convert(keyboardEndFrameScreenCoordinates, to: view.window)

        if notification.name == Notification.Name.UIKeyboardWillHide {
            textBody.contentInset = UIEdgeInsets.zero

        }else{
            textBody.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardEndFrame.height, right: 0)

            textBody.scrollIndicatorInsets = textBody.contentInset
    }
        textBody.scrollRangeToVisible(textBody.selectedRange)
}

1 Answer 1

1

How to connect these buttons to ViewController that has UITextView

The view controller has a reference to the bar in the .xib file at the time the bar is loaded. Therefore it has references to the buttons. Therefore it can call addTarget(_:action:for:) to configure the button actions to call a method of the view controller, which in turn can boldify the text in the text view.

5
  • I don't know where to call "addTarget(_:action:for:)". I made this bar with buttons in .xib file Jan 4, 2017 at 22:10
  • You have not shown your code for how you are loading the .xib you talked about, so I cannot tell you beyond what I said: at the time you load it, you have the reference you need. You showed a completely pointless screen shot. The code is what's important.
    – matt
    Jan 4, 2017 at 22:22
  • Hahaha yeah I know, I wanted to show the buttons, and here is the code Jan 4, 2017 at 22:34
  • Don't show screen shots of code. Don't show pictures of code. Show code. Copy and paste.
    – matt
    Jan 4, 2017 at 22:40
  • Good. So, right at the point where you set textBody.inputAccessoryView, that is a view containing the buttons. If you were, say, to give each button a different tag in the .xib file, you could use viewWithTag to find each button in turn and assign it an action-target pair as I said in my answer.
    – matt
    Jan 4, 2017 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.