Calling Function from Another Viewcontroller in Swift

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



Leave a reply



Submit