I have 2 pages registration and login, When i come back to registration page from login by pressing back button i want to make focus to text field by default. How to do that in viewWillAppear method? I have tried methods updateFocusIfNeeded, setNeedsFocusUpdate , canBecomeFocused , but nothing is working.
Override preferredFocusedView in the view controller and return your text field from it.
override weak var preferredFocusedView: UIView? {
return textField
}
Then call setNeedsFocusUpdate and updateFocusIfNeeded when you want the text field to become focused (in viewWillAppear as you described).
Related
I have a toolbar on my macOS app, developed in Swift. The toolbarItem is dragable onto the NSWindowController, and I can setup an IABAction function, I just have a print in the function at the moment. And when I click on the button nothing happen the click does not seem to be recognised as an action ?
I had a few more line of code in the function but deleted it and now have just the print("test") line.
#IBAction func exportCsvClicked(_ sender: NSToolbarItem) {
print("test") }
No output observed, so I'd love to get "test" in the console when I click on this button.
Here is a list of the connections associated with the toolbarItem.
I found a way to get around the fact that the IBAction from an NSToolbarItem does not recognise the click on this item:
1/I made a customSegue from the item to the main window controller (it can go anywhere)
2/The prepare for segue function posts a notification to the notification saying that the item has been clicked.
3/The main view controller observes the notification and presents, either has a popup or a sheet (I got two buttons), the view that I have setup in the storyboard (referencing with the storyboardID). I found that you need to pass on all the necessary variable to setup the view from the main view Controller, and that there was issue of code in the viewDidLoad function of the sheet/popup view not running, I am suspecting that they might be different instances.
I have 6 tabs in my application.
Each tab has a text field.
There is one button in the sixth tab, which when pressed, should reset the text fields in all the other 5 tabs to blank.
I am unable to figure out any direction in which I should look.
I am pretty new to Xcode, so please pardon my ignorance on this topic.
One thing I have tried so far is to set global variable.
When I press the button, I update the global variable to ""
But then I go to the other tabs, the value has not been updated.
My understanding is that I an not writing the code in the right place.
Currently, I have written this code in viewDidLoad function of each tab's viewController class:
override func viewDidLoad() {
super.viewDidLoad()
telephone.text = ViewLine.GlobalVariable.phone1
timerData.text = ViewLine.GlobalVariable.concept1
}
phone1 and concept1 are my global variables.
Would anyone be able to suggest where should I write this code? or is there a better way to do this?
Thanks
Write code in viewWillAppear
override func viewWillAppear() {
super.viewWillAppear()
telephone.text = ViewLine.GlobalVariable.phone1
timerData.text = ViewLine.GlobalVariable.concept1
}
I think that what you want is the first 5 view controllers to react to a tap on a button in the 6th view controller.
In that case - don't use global variables.
Use notifications:
When the button did tap, send a notification. The first 5 view controllers should subscribe to this notification channel. When each of the first 5 view controllers receive a notification they should change the text to "".
Here is a link on notifications:
https://www.raywenderlich.com/160653/design-patterns-ios-using-swift-part-22
I have a (subclassed) NSCollectionView open, containing multiple text views. Each of the text views is mapped to a (subclassed) NSDocument object. (The idea is to use the document architecture's save functions but not its windowing functions, because I need multiple documents in the same window and the traditional document architecture doesn't allow that.)
Now, there's a function I'd like the user to be able to call from the main menu that will affect their currently selected document. That is: the document is currently visible in a text view with current focus, and the menu command should make an alteration to that document. But the sender of the menu command is just the menu. When the window controller handles the command from the menu, how can I tell it what the currently selected document is?
This is what the responder chain is for.
Since you're using NSCollectionView, you probably already have a subclass of NSCollectionViewItem. If not, create one. Implement your action method in this subclass. Example:
class DocumentItem: NSCollectionViewItem {
var document: MyDocument? {
return representedObject as? MyDocument
}
#IBAction func doThatThing(sender: AnyObject?) {
Swift.print("This is where I do that thing to \(document)")
}
// #IBOutlets and whatnot here...
}
You may need to set this as the custom class of your NSCollectionViewItem in your xib or storyboard.
Next, if your cell view (the view owned by your NSCollectionViewItem) isn't a custom subclass of NSView already, you should make it a custom subclass. You must override acceptsFirstResponder to return true:
class DocumentCellView: NSView {
override var acceptsFirstResponder: Bool { return true }
// #IBOutlets and whatnot here...
}
Make sure you set this as the custom class of your cell view in your storyboard or xib.
Finally, connect the action of your menu item to doThatThing: on First Responder:
Here's how it works:
Because the cell view now returns true for acceptsFirstResponder, when the user clicks a cell view in the collection view, the system will make it the first responder (the start of the responder chain).
When a view has a view controller, it makes that view controller the next responder after itself in the responder chain (if you are on OS X 10.10 Yosemite or later). Your cell view has a view controller: the item object you return from outlineView:itemForRepresentedObjectAtIndexPath:. (NSCollectionViewItem is a subclass of NSViewController, so your custom item is a view controller.)
When the user clicks the menu item, the menu item asks NSApplication to send its action along the responder chain, starting with the first responder. The first responder is the cell view, but it doesn't respond to the doThatThing: message. So NSApplication asks the view for its nextResponder, which is an instance of your NSCollectionViewItem subclass. That object does respond to doThatThing:, so NSApplication sends doThatThing: to your item object (with the NSMenuItem object as the sender argument) and doesn't check the rest of the responder chain.
I can't figure out how, from an interfaceController that is currently displayed in a page based view, to change text on another interfaceController page not currently shown.
It seems you can only change the labels of the interfaceController that is currently displayed.
Anyone know how to modify another controllers label?
var timerControllers = Array<TimerDetailController>()
class TimerDetailController: WKInterfaceController {
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
timerControllers.append(self)
}
func setLabel(){
timerControllers[0].clientName.setText("my new label")
}
}
I want to be able to call setLabel() from any timer page. But instead this only works if the controller displayed is the same as timerControllers[0].
Instead of changing a label that doesn't currently exist, you need to change the value of a data source that will be used to set the label value whenever the view is created.
i want to show my keyboard on screen , whenever a view is shown i.e my login screen..
and i also want to change the name of enter button to Login button.
please help
To change the return key value, change the UIReturnKeyType enum (UITextInputTraits).
If you want the keyboard to show on the screen when it loads, you need to hook up your UITextField to the delegate and include UITextFieldDelegate in your header file. (That is assuming you are using a UITextField.)
Then call:
[textField becomeFirstResponder]
in your viewWillAppear method to give focus to the UITextField which then raises the keyboard.