How to set focus on TextField which is in NSScrollView

Hi,


I have NSTextField which is in ViewController1 in NSScrollView which is in ViewController2


I'm allocating ViewController1 in NSScrollView


I wrote following in ViewController1

textField1.window?.becomeFirstResponder()


But, nothing happens.


How to get textField1 to get focused.

Accepted Reply

Great.


That's because ViewDidAppear occurs later, when all responders are set.


Don't forget to mark the thread as closed.

Replies

Why not just call


textField1.becomeFirstResponder()


Where do you call this ?


Are you sure it is executed (breakpoint ojn instruction nto see if you get there)

I'm calling it in viewDidLoad()


Still not able focus it using


textField1.becomeFirstResponder() 

Is it an IOS App ?


If so, that should be a UITextField, not a NSTextField.


Can you explain the setup: I have NSTextField which is in ViewController1 in NSScrollView which is in ViewController2

Who is in who ?

ViewController1 contains NSTextField

ViewController1 in NSScrollView : What do you mean here ? How did you do it ? Or is it NSTextField in NSScrollView ?

ViewController2 contains NSScrollView


Could you also show the code for the 2 viewControllers ? That would ease analysis.

It's an MacOS App


Here is my firstNameTextField in AddContactViewController


class AddContactVeiwController: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource, NSPopoverDelegate {

    //MARK: Variables
    
    @IBOutlet weak var firstNameTextField: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        firstNameTextField.becomeFirstResponder()



Allocating AddContactViewController in AddContactScrollViewController

class AddContactScrollViewController: NSViewController {
    
    @IBOutlet weak var scrollView: NSScrollView!
    
    var vc: AddContactVeiwController!
    
    var contentView: NSView?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        contentView = NSView(frame: NSRect(x: 0, y: 0, width: self.view.frame.width, height: 1362))
        contentView!.wantsLayer = true
        contentView!.layer?.backgroundColor =  NSColor.clear.cgColor

        vc = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "addContact")) as! AddContactVeiwController
        vc.view.setFrameOrigin(NSPoint(x: 0, y: 0))
        vc.view.setFrameSize(NSSize(width: 1200, height: 1362))
        vc.view.wantsLayer = true


        contentView!.addSubview(vc.view)
        scrollView.documentView = contentView

Instead of calling

firstNameTextField.becomeFirstResponder()

in AddContactVeiwController viewDidLoad


could you try to call

vc.firstNameTextField.becomeFirstResponder()

in AddContactScrollViewController viewDidLoad


Note: you have a typo in AddContactVeiwController ; probably mean AddContactViewController

I have figured it out.


Instead of calling it in ViewDidLoad, I called it in ViewDidAppear

And, it worked!

Great.


That's because ViewDidAppear occurs later, when all responders are set.


Don't forget to mark the thread as closed.