How to focus on a text field when opening an application? - swift

How to focus on a text field when opening an application?
Swift 4,
Xcode 10,
macOS
ANSWER:
Thanks to the advice by #Willeke in the comments, I did it this way:
import Cocoa
import AppKit
import Foundation
let defaults = UserDefaults.standard
class ViewController: NSViewController{
#IBOutlet weak var addDomain: NSTextField!
#IBOutlet weak var addSiteField: NSTextField!
#IBOutlet weak var tableView: NSTableView!
#IBOutlet weak var removeSite: NSSegmentedControl!
override func viewDidAppear() {
super.viewDidAppear()
addDomain.window?.makeFirstResponder(addDomain)
}
Because:
https://developer.apple.com/documentation/appkit/nsresponder/1526750-becomefirstresponder
Use the NSWindow makeFirstResponder(_:) method, not becomeFirstResponder() method, to make an object the first responder. Never invoke this method directly.

You seem to be not over-riding the viewDidAppear that belongs to your NSViewController, but you are adding a new function on it's own.
Try using:
override func viewDidAppear() {
// Though the default implementation does nothing as of now,
// it is always safe to have the call to the super function in place,
// in case you plan to add sub-classes in between.
super.viewDidAppear()
addDomain.window?.makeFirstResponder(addDomain)
}

In your viewController in viewDidAppear
yourTextField.becomeFirstResponder()
Update:
macOS implementation requires using NSWindow makeFirstResponder!

Related

global declaration of NSTextField array

I want to declare a global array of NSTextFields, but "Class ViewController has no initializers." Yes, global, but static.
class ViewController: NSViewController {
#IBOutlet weak var TextField01: NSTextField!
#IBOutlet weak var TextField02: NSTextField!
#IBOutlet weak var TextField03: NSTextField!
var fieldArray: [NSTextField]
override func viewDidLoad() {
super.viewDidLoad()
fieldArray = [TextField01, TextField02, TextField03]
}
I thought that the problem was the compiler not knowing about the main storyboard; that's why I didn't assign values to my fieldArray. It would appear that I need to declare (initialize?) fieldArray in some other way.

How to connect outlets and actions in Swift / MacOS programaticly

I have a simple example.
I connected the left button1 and label1 with ctrl-draging it to the contollerclass.
How can I do the same for the right button2 label2 programaticly (without ctrl-draging)
That´s my code:
class ViewController: NSViewController {
#IBOutlet weak var label1: NSTextField! //connected with ctrl-drag
#IBOutlet weak var button1: NSButton! //connected with ctrl-drag
#IBOutlet weak var label2: NSTextField! //not yet connected
#IBOutlet weak var button2: NSButton! //not yet connected
#IBAction func button1Pressed(_ sender: Any) //connected with ctrl-drag
{ label1.stringValue = "button-I"
button1.title = "pressed"
}
#IBAction func button2Pressed(_ sender: Any) //not yet connected
{ label2.stringValue = "button-II"
button2.title = "pressed"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
If you want to use storyboard and lay out all your elements there, you can't really avoid the ctrl drag (or just drag if you open the connections inspector on the right).
You could however create your second button programmatically in code and not use the storyboard at all for it. Then programmatically add your constraints too.
You can also add the action of the button programmatically (using addTarget) if you would like but that would require at least the IBOutlet to be setup in order to have a reference to the button.

Xcode 7 Swift 2 error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, SUBCODE=0X0)

Ello! I am working on a notes app for mac, using swift as the language. I am currently working on a save and load feature, and have run across an error I am not sure how to fix. Here is the problematic code:
import Cocoa
class NoteViewController: ViewController {
#IBOutlet weak var notefield: NSTextField!
#IBOutlet weak var savenamefield: NSTextField!
#IBOutlet weak var loadnamefield: NSTextField!
#IBAction func save(sender: AnyObject) {
// The line below gets the error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0
NSUserDefaults.standardUserDefaults().setObject(notefield.stringValue, forKey: savenamefield.stringValue)
}
#IBAction func load(sender: AnyObject) {
var name = loadnamefield.stringValue;
var lnote = NSUserDefaults.standardUserDefaults().objectForKey(name)
notefield.stringValue = lnote as! String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
What might I be doing wrong?
I figured it out. With some testing, I discovered that everything worked until I added notefield.stringValue into the code.I then realized that I had this is a different window. The code is covering 3 different windows, 2 of which are popups. notefield is the only element I had inside the code from the main window. I moved all of the elements to the same window, and recoded the entire thing. I got it working well using the following code:
import Cocoa
class NoteViewController: ViewController {
#IBOutlet weak var notefield: NSTextField!
#IBOutlet weak var savenamefield: NSTextField!
#IBOutlet weak var loadnamefield: NSTextField!
#IBAction func save(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().setObject(notefield.stringValue, forKey: savenamefield.stringValue)
}
#IBAction func load(sender: AnyObject) {
notefield.stringValue = NSUserDefaults.standardUserDefaults().objectForKey((loadnamefield?.stringValue)!) as! String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
Please note that I am supplying this code as a guideline only. Thanks for the help that was given.

Sinch on swift 2

I am new in develop app for IOS and i am still learning but i can not handle a incoming call.
I do not know how to delegate and override the method. Can someone write a little example in swift 2.
Thats what i have until now, how can override the method didReceiveIncomingCall?
import Foundation
import UIKit
class LoginView: UIViewController, SINCallClientDelegate, SINCallDelegate{
#IBOutlet weak var labelUsuario: UILabel!
#IBOutlet weak var nameUsername: UILabel!
#IBAction func buttonDesconectar() {
//Add Disconnect code for VOIP
let defaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("usuario")
self.performSegueWithIdentifier("segueLoginViewToInicio", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
let vozIP = Sinch.clientWithApplicationKey(<APP_KEY>, applicationSecret: <APP_SECRET>, environmentHost: "sandbox.sinch.com", userId: "Roke" )
vozIP.setSupportCalling(true)
vozIP.setSupportActiveConnectionInBackground(true)
vozIP.start()
vozIP.startListeningOnActiveConnection()
}
}

Creating IBOutlet in Swift results in 'class is not constructible with ()'

My code compiled fine before upgrading to beta 4, however I think they changed something with IBOutlets.
The old syntax was:
#IBOutlet var tableView: UITableView
The new syntax is:
#IBOutlet weak var tableView: UITableView!
This is default code generated by Xcode when I ctrl drag from my xib file to the class file.
However, with this new syntax, I'm unable to construct an instance of my class. Take the following example:
class TestViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
}
Then if I try to do either
var controller = TestViewController(nibName: nil, bundle: nil)
or
var controller = TestViewController()
I get an error:
TestViewController is not constructible with ()
What's the right way to create an instance of my controller then? The only way that currently works for me is to make the outlets optional, but I'd rather not do that.
Can you try this.
#IBOutlet var tableView: UITableView?
I know this is not the answer but the error is gone after this.
The solution seems to be to implement the init method in that view controller:
init()
{
super.init(nibName: nil, bundle: nil)
}
I'm not sure why that is.