How to use requestSupplementaryLexiconWithCompletion - swift

I am not sure as to how to use the requestSupplementaryLexiconWithCompletion as outlined on the Apple Developer site here.
I have the following function
override func requestSupplementaryLexiconWithCompletion(completionHandler: ((UILexicon!) -> Void)!) {
appleLexicon = UILexicon
}
I am just lost as how to get a the UILexicon, then look at the pair values returned. E.g print them to the console to see its output.

You can try to use it like:
func handler(lexicon: UILexicon!) {
println(lexicon.description)
}
#IBAction func click(sender: AnyObject) {
let controller = UIInputViewController()
controller.requestSupplementaryLexiconWithCompletion(handler)
}
Or like:
#IBAction func click(sender: AnyObject) {
let controller = UIInputViewController()
controller.requestSupplementaryLexiconWithCompletion({
lexicon in
println(lexicon.description)
})
}
click method is just a UIButton tap event handler

Related

How to get Stripe's STPPaymentCardTextField Data programmatically?

I've successfully set my first view controller to STPAddCardViewController. I now need to get the user information in the STPPaymentCardTextField. Problem is, I'm used to using the storyboard to make outlets. How do I detect the STPPaymentCardTextField programmatically?
I've tried:
class ViewController: STPAddCardViewController, STPPaymentCardTextFieldDelegate {
let paymentCardTextField = STPPaymentCardTextField()
func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) {
print(paymentCardTextField.cardNumber)
//ERROR: printing nil in the console
}
}
But I'm getting nil as an output. Any help?
You should use either STPAddCardViewController, or STPPaymentCardTextField, not both. The SDK's ViewControllers are not designed to be extended. The intended use is:
class MyVC : STPAddCardViewControllerDelegate {
override func viewDidLoad() {
…
let addCardView = STPAddCardViewController()
addCardView.delegate = self
// Start the addCardView
self.navigationController.pushViewController(addCardView, animated: true)
}
…
func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreatePaymentMethod paymentMethod: STPPaymentMethod, completion: #escaping STPErrorBlock) {
// TODO: do something with paymentMethod
// Always call completion() to dismiss the view
completion()
}
func addCardViewControllerDidCancel(_ addCardViewController: STPAddCardViewController) {
// TODO: handle cancel
}
}
But rather than my partial example I'd recommend reading these docs and trying out this example iOS code. Best wishes!

Access a variable made in another IBAction in the same file

I've created a variable in one IBAction which saves a string, and I want to be able to call that variable in another IBAction in the same file. How do I define the variable globally so that the other IBAction can call it?
The variable message comes from a UIAlertController output, which appears when pressing a button on the View Controller.
#IBAction func EditMessage(_ sender: Any) {
let message = Message(message: text)
}
#IBAction func PostArticle(_ sender: Any) {
let parameters = ["title": "subheading", "content": "\(message)"]
}
Trying to call the message variable from the other IBAction will only give the error:
Use of unresolved identifier 'message'
class MyViewController : UIViewController {
var message = ""
#IBAction func EditMessage(_ sender: Any) {
message = Message(message: text)
}
#IBAction func PostArticle(_ sender: Any) {
let parameters = ["title": "subheading", "content": "\(message)"]
}
}
Hope it Helps.
Make your message variable be accessible to the two IBActions.
Your Message function should return a string
Message(message: String) -> String {
// Do something here
return "sample"
}
Read Swift Function

Can't get QuickLook to work when trying to preview files

I am writing a macOS application with Swift using story boards. I have a NSTableView which contains files that I want the user to be able to preview via QuickLook.
I seemingly have everything in place and my code looks very similar to what has been described here: QuickLook consumer as a delegate from an NSViewController, but I keep getting the error
-[QLPreviewPanel setDataSource:] called while the panel has no controller - Fix this or this will raise soon.
See comments in QLPreviewPanel.h for -acceptsPreviewPanelControl:/-beginPreviewPanelControl:/-endPreviewPanelControl:.
I've been trying to adapt the solution of above post to my situation with Swift and story boards.
The main pieces are:
import Quartz
class ViewController: NSViewController, QLPreviewPanelDataSource, QLPreviewPanelDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let windowNextResponder = self.view.window?.nextResponder
self.view.window?.nextResponder = self
self.nextResponder = windowNextResponder
}
// *** Quicklook stuff ***
#IBAction func quickLookButtonAction(_ sender: Any) {
guard qlPanel != nil else {
return
}
if qlPanel!.currentController == nil {
print ("No controller")
//qlPanel!.windowController = self.view.window?.windowController
// qlPanel!.updateController()
} else {
print (qlPanel!.currentController)
}
qlPanel!.delegate = self
qlPanel!.dataSource = self
qlPanel!.makeKeyAndOrderFront(self)
}
func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
return CSVarrayController.selectedObjects.count
}
func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
let file = CSVarrayController.selectedObjects[index] as! CSVfile
return file.url as NSURL
}
override func acceptsPreviewPanelControl(_ panel: QLPreviewPanel!) -> Bool {
return true
}
override func beginPreviewPanelControl(_ panel: QLPreviewPanel!) {
panel.dataSource = self
panel.delegate = self
}
override func endPreviewPanelControl(_ panel: QLPreviewPanel!) {
panel.dataSource = nil
panel.delegate = nil
}
}
With or without messing with the responder chain I get the error.
The delegate functions all get called as expected as well.
Remove
qlPanel!.delegate = self
qlPanel!.dataSource = self
in quickLookButtonAction, the viewcontroller isn't in control yet. Wait for beginPreviewPanelControl.
From the documentation for currentController:
You should never change the preview panel’s state (its delegate, datasource, and so on) if you are not controlling it.
From comments in QLPreviewPanel.h for -beginPreviewPanelControl::
Sent to the object taking control of the Preview Panel.
The receiver should setup the preview panel (data source, delegate, binding, etc.) here.

NSComboBox getGet value on change

I am new to OS X app development. I manage to built the NSComboBox (Selectable, not editable), I can get it indexOfSelectedItem on action button click, working fine.
How to detect the the value on change? When user change their selection, what kind of function I shall use to detect the new selected index?
I tried to use the NSNotification but it didn't pass the new change value, always is the default value when load. It is because I place the postNotificationName in wrong place or there are other method should use to get the value on change?
I tried searching the net, video, tutorial but mostly written for Objective-C. I can't find any answer for this in SWIFT.
import Cocoa
class NewProjectSetup: NSViewController {
let comboxRouterValue: [String] = ["No","Yes"]
#IBOutlet weak var projNewRouter: NSComboBox!
#IBAction func btnAddNewProject(sender: AnyObject) {
let comBoxID = projNewRouter.indexOfSelectedItem
print(“Combo Box ID is: \(comBoxID)”)
}
#IBAction func btnCancel(sender: AnyObject) {
self.dismissViewController(self)
}
override func viewDidLoad() {
super.viewDidLoad()
addComboxValue(comboxRouterValue,myObj:projNewRouter)
self.projNewRouter.selectItemAtIndex(0)
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(
self,
selector: “testNotication:”,
name:"NotificationIdentifier",
object: nil)
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: projNewRouter.indexOfSelectedItem)
}
func testNotication(notification: NSNotification){
print("Found Combo ID \(notification.object)")
}
func addComboxValue(myVal:[String],myObj:AnyObject){
let myValno: Int = myVal.count
for var i = 0; i < myValno; ++i{
myObj.addItemWithObjectValue(myVal[i])
}
}
}
You need to define a delegate for the combobox that implements the NSComboBoxDelegate protocol, and then use the comboBoxSelectionDidChange(_:) method.
The easiest method is for your NewProjectSetup class to implement the delegate, as in:
class NewProjectSetup: NSViewController, NSComboBoxDelegate { ... etc
Then in viewDidLoad, also include:
self.projNewRouter.delegate = self
// self (ie. NewProjectSetup) implements NSComboBoxDelegate
And then you can pick up the change in:
func comboBoxSelectionDidChange(notification: NSNotification) {
print("Woohoo, it changed")
}

Swift - call #IBAction method in viewDidLoad without parameter

#IBAction func getNewPhotoAction(sender: AnyObject) {
println("getNewPhotoAction")
}
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhotoAction(sender: AnyObject) // Error
}
I just want to call the getNewPhotoAction IBAction method in viewDidLoad.
Which parameter to enter in this line -> self.getNewPhotoAction(?????) ?
I don't have any parameter. I just need to call.
I used in Objective-C style:
[self getNewPhotoAction:nil]
but I don't know Swift style.
The parameter sender indicates who are calling the action method.
When calling from viewDidLoad, just pass self to it.
override func viewDidLoad() {
super.viewDidLoad()
getNewPhotoAction(self)
}
By the way, if the sender parameter of the getNewPhotoAction method wasn’t used, the parameter name can be omitted.
#IBAction func getNewPhotoAction(AnyObject) {
println("getNewPhotoAction")
}
You could always create a separate func that you call on in your viewDidLoad or in your IBAction
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhoto()
}
func getNewPhoto(){
//do whatever you want here.
println("getnewphotoaction")
println("whatever you want")
}
#IBAction func getNewPhotoAction(sender: AnyObject) {
self.getNewPhoto()
}
Swift 4.2
#IBAction func getNewPhotoAction(sender: Any) {
println("getNewPhotoAction")
}
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhotoAction(AnyObject.self)
}
If you still need to reference the UIButton or whatever is sending the action and want to call it from code at the same time - you can do this too:
onNext(UIButton())
Wasteful, but less code.
#IBAction func getNewPhotoAction(sender: AnyObject?){
......
}
**AnyObject** means that you have to pass kind of Object which you are using, nil is not a AnyObject.
But **AnyObject?**, that is to say AnyObject is Optional, nil is a valid value.
meaning the absence of a object.
self .getNewPhotoAction(nil)
You actually don't need to pass any object at all. If you have no need to use the sender, then declare the function without it like this:
#IBAction func getNewPhotoAction() { ... }
And use it like this:
self.getNewPhotoAction()
You may need to re-connect the outlet in interface builder when you make this change (remove it and then add it back) if this method is connected to an event in interface builder.
#IBAction func getNewPhotoAction(sender: AnyObject? = nil) {
print("getNewPhotoAction")
}
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhotoAction(nil)
}
Since you don't have any sender, hand over nil.
self.getNewPhotoAction(nil)