Call function from another swift class

Discussion in 'Mac Programming' started by abcdefg12345, Jul 6, 2016.

  1. abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    if have 2 swift classes AppDelegate.swift and Other.swift I'm trying to call a function thats in Other.swift from AppDelecage.swift but I'm getting an error
    this is what I'm doing

    Other.swift

    Code:
    import Foundation
    import Cocoa
    
    class Other: NSObject, NSTextFieldDelegate {
    
        @IBOutlet weak var mytextfield: NSTextField!
    
        func myfunction() {
            mytextfield.stringValue = "whatever"
        }
    }
    
    AppDelegate.swift

    Code:
    import Cocoa
    
    @NSApplicationMain
    
    class AppDelegate: NSObject, NSApplicationDelegate {
    
        @IBActionfunc CallFunction(sender: AnyObject) {
        Other().myfunction()
        }
    }
    
     
  2. jgaz macrumors member

    jgaz

    Joined:
    Dec 30, 2007
    Location:
    Colorado
    #2
    I think the problem is your myTextField outlet isn't connected to its corresponding NSTextField yet when you are calling the function. You might not have it actually connected or the view hasn't been created yet.

    So when you call Other().myfunction() it tries to access the NSTextField that is not yet there.

    I am guessing that your mytextfield: NSTextField! and the button you are using for your IBAction callFunction are in different views? It might be better to have them update your model and then have the responsible ViewControllers react to that change. Or you could use a parent controller that is responsible for changes in the views for both of your classes.

    I am not as familiar with with Mac programming as iOS but typically you wouldn't see an @IBAction in the AppDelegate, usually we find @IBOutlets and @IBActions in the ViewController responsible for the view the outlet or action is connected to.

    The other thing I am thinking it could be is that maybe your Other instance has already been created and the NSTextfield is there and connected; but Other().myfunction() is creating a brand new instance of Other. You might need to pass in a reference to the original instance of Other that you create somewhere else presumably. So you would have something more like this:

    Code:
    import Cocoa
    
    @NSApplicationMain
    
    class AppDelegate: NSObject, NSApplicationDelegate {
     
        var otherInstance: Other?
        @IBActionfunc CallFunction(sender: AnyObject) {
        otherInstance?.myfunction()
        }
    }
    
    You would need to set the otherInstance variable when you create it.

    I hope that helps give you a couple ideas of where to start looking
     
  3. abcdefg12345 thread starter macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #3

    Thanks for posting, everything was connected properly, I tried your code instead and still it didn't work, I ended up doing the following and it worked:

    AppDelegate.swift
    Code:
    import Cocoa
    
    @NSApplicationMain
    
    class AppDelegate: NSObject, NSApplicationDelegate {
    
        @IBOutlet weak var CallOther: Other! // connect outlet to Other object
    
        class CallOther {
            static let CallOther = Other()
        }
    
        @IBAction func Action(sender: AnyObject) {
            self.CallOther.myfunction()
        }
    }
    Other.swift
    Code:
    import Foundation
    import Cocoa
    
    class Other: NSObject {
    
        @IBOutlet weak var Field: NSTextField!
      
        func myfunction() {
            Field.stringValue = "whatever"
        }
    }
     

Share This Page