Update text field values in multiple tabs with single button - swift

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

Related

How to detect a click from an NSToolbarItem inside an NSWindowController?

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.

How to display a modal window during a drag operation?

In the performDragOperation(sender: NSDraggingInfo) method of the receiver of my drag operation, I want to display a modal window that requires user input. The URL of a file that is dropped will populate one field in the modal window, and the user will have to populate another NSTextField.
The problem is that the modal window does not respond to keyboard events during the drag operation, which effectively causes a deadlock until the drag times out. I cannot find a reliable way to deal with this. I have two possible workarounds, neither solves this completely:
Use dispatch_after() to tell the main thread to show the modal window 0.25 sec after the drag operation completes. This works, but is dependent on a race condition.
Instead of a modal window, simply show a regular window. Or use beginModalSession(). Less desirable than a modal window in this case.
Code:
override func performDragOperation(sender: NSDraggingInfo) -> Bool {
// Do some stuff here to get data from the sender
// Load a view controller and display it modally
let vc = MyViewController(nibName: "MyViewController", bundle: nil) {
self.presentViewControllerAsModalWindow(vc)
}
return true
}
I've tried using dispatch_async(dispatch_get_main_queue()) {} to present the view controller. I thought that would allow performDragOperation to return true, but that does not appear to be sufficient to end the drag operation completely.
I've also tried showing the view controller from concludeDragOperation(), but that does not help either.
What is the best practice to deal with this situation?

How to focus textfield by default in tvOS application, not first responder?

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).

Segue moving too quickly while passing data?

I am making an app where the user inputs some numbers into a text field and it shoots out an answer on a label. I have the answer being displayed on another view controller. Using the same prepare for segue methods on all of the screens that I have only some of them work. The others will pass whatever text I have in the first view controller label (I have checked this by inputting all sorts of stuff and it shows up on the other side)... I have also deleted the segue and made sure that the calculation works and the label in the first view controller is getting the information.
This leads me to believe that the way it is set up is fine, but the segue happens so fast that the label on the first view controller will still have the original text when the segue is performed, sending that information over as a string instead of the answer...
Is it possible to change this? It would be the only logical thing that makes sense to me write at the moment. If it is possible how could this be fixed?
Any help would be appreciated. Thanks
EDIT:
This is the whole code I was Asked To post to help figure this out
So if in the first view controller I put a '?' for the text in the label just to hold the label, and in the second view controller I put a '!' in the answer label to hold the label.. The second view controller after I run the simulator the second view controller will show a ? the text from the 1st label... so it does show that it is pushing the data over, it is just not pushing the information in the label.
I think you have a made a segue to another view controller from the same button , with which you are performing the #IBAction func usednroicalc(sender: AnyObject), what you have to do is make a segue from view controller to another view controller in storyboard , give your segue an identifier and inside the #IBAction write at the place where you want to perform the segue , that is after your calculations write , self.performseguewithIdentifier("your identifier") . you can give identifier to segue by clicking on it and than find the identifier label in the inspector.
EDIT:
Here I have dragged from ViewController the one Highlighted as yellow in ViewController Scene to TableViewController the one above first responder in TableView Controller Scene.
Just wanted to answer my own question as I have found a way to push the info over finally...
You have to set up the calculations of the code in the prepare for segue and not in the button action. The button action is now only setup for the segue to the new view controller. not the way I wanted everything thing to go but it works.
Thanks everyone for the feedback... hope this helps someone else in the future.

Pass data from one view controller to the next WITHOUT a segue in swift

So I have an app that where you choose a button in the first screen it will bring you to another screen depending on which one you chose on the first one. Because I have these possible different layouts I can't just use a segue from one button to the next view controller because there is a middle screen involved. SO it is like this:
1 ---> 2 ---> 3a || 3b || 3c || 3d etc...
So your choice on the first screen dictates what your third screen will look like.
So instead I was thinking to have a value associated with each button in the first view, have it sent to the second view via a segue, and then sent again to the third view and tell it to use the proper segue to the proper third view Controller based on the value associated with the first view. But I can't do that because I can't set the second screen to segue to all the possible layouts for the third screen...At least i think I can't.
Bottom line question: is there anyway to go to another view controller after clicking a button without a segue while also sending data to that next view controller, again, without a segue to store it in?
Please help mehhh
Follow these steps...
1: Create a Separate file called Manager.swift and place this code in it...
//manager.swift
import Foundation
struct Manager {
static var messageText = String()
}
2: Clean your project by pressing Shift+Command+K.
3: In the first view controller set the messageText to the data you want to pass...
Manager.messageText = self.dataToPass
4: In the second view controller retrieve the data and set the content to the message text...
self.dataToReceive = Manager.messageText
5: Your Finished!!
So instead I was thinking to have a value associated with each button
in the first view, have it sent to the second view via a segue, and
then sent again to the third view and tell it to use the proper segue
to the proper third view Controller based on the value associated with
the first view. But I can't do that because I can't set the second
screen to segue to all the possible layouts for the third screen...At
least i think I can't.
You can do this with segues; you just have to wire them up properly. Instead of hooking the segues up to buttons, you can create them by control-dragging from the ViewController icon at the top to the next ViewController.
Once you have created such a segue, click on the resulting segue arrow and give it an identifier in the Attributes Inspector (something like "segueToVC3a").
So, you would do this multiple times for each segue from VC2 to VC3a, VC2 to VC3b, etc.
Assuming in your first viewController you decide the value of whereToGo and you pass that to your second viewController with a segue. Then in your second viewController, you could then do something like this:
var identifier: String = ""
switch whereToGo {
case "VC3a": identifier = "segueToVC3a"
case "VC3b": identifier = "segueToVC3b"
default: break
}
if identifier != "" {
performSegueWithIdentifer(identifier, sender: self)
}