Call function from other ViewController
You may use NSNotificationCentre
to accomplish this task.
In viewDidLoad
method of your SecondViewController
class register self as observer to receive notification broadcasts:-
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(showAlert), name: NSNotification.Name(rawValue: "callForAlert"), object: nil)
}
and in FirstViewController
's button action method you should fire the notification by writing :-
@IBAction func callFunctionInOtherClass(sender: AnyObject) {
//Call "func showAlert" in SecondViewController when clicking the UIButton in FirstViewController
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "callForAlert"), object: nil)
}
Don't forget to call removeObserver
in SecondViewController's viewDidUnload
method.
Swift: call a ViewController func from another viewcontroller
How about creating an extension of UIViewController
and passing the collectionTextFields
as an argument?
extension UIViewController {
func addDoneButton(collectionTextFields: [UITextField]) {
let keyboardToolbar = UIToolbar()
keyboardToolbar.sizeToFit()
let flexBarButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace,
target: nil, action: nil)
let doneBarButton = UIBarButtonItem(barButtonSystemItem: .Done,
target: view, action: #selector(UIView.endEditing(_:)))
keyboardToolbar.items = [flexBarButton, doneBarButton]
for textField in collectionTextFields {
textField.keyboardType = .DecimalPad
textField.inputAccessoryView = keyboardToolbar
}
}
}
I want to call my function from another ViewController
You can use the delegate pattern for this:
protocol PlayViewDelegate: class {
func playButtonPressed()
}
// Implement the protocol in your Main VC
extension MainViewController: PlayViewDelegate {
func playButtonPressed() {
// Play
}
}
// Add delegate to the alert like VC
class PlayViewController: UIViewController {
weak var delegate: PlayViewDelegate?
@IBAction
func playPressed() {
self.delegate?.playButtonPressed()
self.dismiss(animated: true, completion: nil)
}
}
Calling ViewController function from another .swift file
You can use delegation pattern
protocol DataDelegate {
func dataReceived()
}
class WebSocketLogic: WebSocketDelegate {
var delegate: DataDelegate?
var columns = 0
var rows = 0
...
func receiveData {
...
columns = map.count
rows = map[0].count
delegate?.dataReceived()
}
}
class ViewController: UIViewController, DataDelegate {
@IBOutlet weak var collectionView: UICollectionView!
var webSocket = WebSocketLogic(roomId: 1)
var columns = 0 {
didSet {
collectionView.reloadData()
print("columns number set to: \(columns)")
}
}
var rows = 0
override func viewDidLoad() {
super.viewDidLoad()
webSocket.delegate = self
webSocket.setupStarScream()
columns = webSocket.columns
rows = webSocket.rows
}
func dataReceived() {
collectionView.reloadData()
}
}
Related Topics
Google Analytics 3.08 iOS Idfa Class Missing, Won't Collect Idfa
Trouble Creating Xmpp Muc Room: Code 503 (Service Unavailable)
Push/Pop View Controller with Navigation Bar from View Controller Without Navigation Bar
Push View Programmatically in Callback, Swiftui
Ios11 Causing Cors Issues in All Mobile Browsers
Get Current iOS Device Orientation Even If Device's Orientation Locked
How to Properly Use Class Extensions in Swift
Core Data Store Included in App Bundle
iOS Swift Converting Calendar Component Int Month to Medium Style String Month
Reload Section Without Reloading Section Header
Always Stop in App Delegate After Enabling All Exceptions Break Point
Playing Video Inline in Ionic/Phonegap (Webkit-Playsinline Not Working)
How to Set the Tab Order in iOS
Override a Method in Objective C via Category