2

When a button is pressed in FirstViewController, I would like for it to call a func pressedButton() in another class (SecondViewController). How do I do this?

I've tried the following, and it does not work right:

SecondViewController().pressedButton()

Here's the code for pressedButton:

func pressLearnButton() {
        let screenSize: CGRect = UIScreen.mainScreen().bounds
        let screenWidth = screenSize.width
        self.scrollView.setContentOffset(CGPoint(x: screenWidth, y: 0), animated: true)
    }
smecperson
  • 301
  • 1
  • 4
  • 15
  • Could you be more specific about what you're trying to implement? It may be helpful in answering your question. For example, do you use a segue to go between FirstViewController (FVC) and SVC? Or does SVC live inside FVC? – kbpontius Apr 15 '15 at 22:55
  • This is possible (the answers given below are workarounds, which may have some merit, but I don't think that's what you want), but we'd have to know your view hierarchy to help you. What is `SecondViewController`'s relationship to `FirstViewController`? – AstroCB Apr 15 '15 at 22:57
  • Seems to me like you don't understand some of the concepts of Object Oriented Programming yet. That's not bad, everybody starts somewhere. I suggest you read the basics of OOP to understand the different between classes and objects, how to reference other objects and send messages to them. – EmilioPelaez Apr 15 '15 at 22:57
  • You are not providing enough information. How did the instance of FirstViewController get on screen? Is there an existing instance of SecondViewController on screen already (perhaps further down on a navigation controller stack, or did a modal `presentViewController:animated:completion:` from an instance of SecondViewController to an instance of FirstViewController?) Or do you need to create a new instance of SecondViewController? – Duncan C Apr 15 '15 at 23:41
  • On storyboard, all the views are not connected. The SecondViewController has a UIScrollView, where I add the FirstViewController and others into it's subview. I'm trying to make a button pressed on the FirstViewController (the scrollview's first controller) make the scrollview move to a certain offset. The scrollview is in SecondViewController. – smecperson Apr 16 '15 at 20:31

3 Answers3

4

Syntactically, it seems like you're trying to accomplish what a static method normally does. Unless this is what you're trying to accomplish, and I'm guessing it's not, you need to instantiate your SecondViewController before you call a method on it, assign it to a variable, and call your desired method as follows:

let otherViewController: SecondViewController = SecondViewController()
let result = otherViewController.doSomething()

If you're trying to transition (segue) to another view controller when you click on the button you should be using the prepareForSegue() method to make the transition to the next view controller. Also, don't forget to set the segue identifier on the Storyboard.

Hopefully this is helpful.

Community
  • 1
  • 1
kbpontius
  • 3,867
  • 1
  • 30
  • 34
0

You really call it. But, in your case, it depends on many factors. Seems it must show anything by calling pressedButton method. Need to declare variable of you controller, add it view to view of your current controller, and you will see visual data.

dimpiax
  • 12,093
  • 5
  • 62
  • 45
0

First of all, you have to keep a reference to an instance of SecondViewController in your FirstViewController. Then, in order to call a function foo() in SecondViewController from your FirstViewController, just call secondInstance.foo(). If foo() is class function, you can call with SecondViewController.foo().

class A : UIViewController {
    var myB  = B()

    func callBFoo() {
        myB.foo()
    }

    func callStaticBFoo() {
        B.staticFoo()
    }
}

class B : UIViewController {
    func foo() {
        println("foo")
    }

    class func staticFoo() {
        println("static foo")
    }
}
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
  • That won't really work because I need to use a scrollView within the SecondViewController – smecperson Apr 15 '15 at 23:08
  • You said "That won't really work because I need to use a scrollView within the SecondViewController". Sorry, that doesn't make much sense. – Duncan C Apr 15 '15 at 23:36