-2

I have a UIView with a TableView and a Button (Big Button). The TableView has a custom Cell. In this cell there is an "Add" button. I want to animate the first button when the user makes click on the Add button.

This is my schema:

enter image description here

This is my code:

class ProductsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!
    @IBOutlet var bigButton: UIButton! <- I WANT TO ANIMATE THAT BUTTON

}

ProductCell class

class ProductCell: UITableViewCell {

    @IBAction func addProduct(sender: AnyObject) {
       //I WANT TO ACCESS THE BIG BUTTON FROM HERE
    }
}

Screen example of my app

enter image description here

I've tried to get the parent controller or the superview to get the IBOutlet but the app is crashing allways

AstroCB
  • 12,337
  • 20
  • 57
  • 73
pekpon
  • 761
  • 1
  • 10
  • 24
  • 2
    Why wouldn't you just use an NSNotification within the didSelectCell... paradigm? Or some custom delegation... Plenty of samples on SO. There is a thing called property observation in Swift (willSet, et al) but I don't think it applies in your case. You could try a gesture recognizer with UIDynamics or UIView animation (all on SO) – Tommie C. Mar 19 '15 at 02:27

3 Answers3

0

Add block properties to your cells which lets them notify your view controller when they have been clicked. In your view controller block code, you can then access the big button.

See my answer to a similar question. Simply replace the switch example with your button. So replace UISwitch with UIButton.

How can I get index path of cell on switch change event in section based table view

So rather than have the cell try and talk to another cell/button, have the cell notify the controller which can then manage the big button changes.

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
0

Although I made a comment about using alternate methods you could also employ a strategy below based on updates to a property stored in the current view controller class. You could just as well use property observation on the ProductsViewController but I assume you'd like to keep OOP focused and reduce the size of your controller.

Subclass the ViewController

One could subclass an existing UIViewController and then create a property in the super class that deals with the value that was changed (row tapped). In that subclass you could then do some animation. Because you would be subclassing you continue to obtain all the benefits and methods defined in your existing controller. In your identity inspector point your Class to the new subclass and create any functional updates to your UI using animation.

class ProductsViewController:... {
    var inheritedProperty:UIView = targetView {
        willSet {newValue } // is the newValue
        didSet {oldValue} //is the old value
    }
}

class AnimatedProductsViewController:ProductsViewController {

    override var inheritedProperty:UIView {
        //do something interesting if the property of super class changed
        willSet {newValue } // is the newValue
        didSet {oldValue} //is the old value
        //you might want to call this method like so
        // didSet { animate(newValue) }
    }

    func animate (view: UIView){

        //do animation routine using UIView animation, UIDynamics, etc.

    }
}

Property Observation

Whenever the didSelectCell... method is called just set a value to the inheritedProperty. Then add the property observers (see sample code) and react when the property changes (maybe pass a reference to the view you want to animate).

For example: Within the property observer you can just take that view and pass it to your animator function (whatever is going to do the animation). There are many examples on SO of how to animate a view so just search for (UIView animation, UIDynamics, etc).

The normal benefits of separation are encapsulation of functionality and reuse but Swift also guarantees that each set of property observers will fire independently. You'd have to give some more thought to this as to its applicability in this use case.

Tommie C.
  • 12,895
  • 5
  • 82
  • 100
0

Do all this things in your viewController

Add target Method to cell's add button in cellForRowAtIndexPath Method Like This

cell.add.addTarget(self, action: "addProduct:", forControlEvents: UIControlEvents.TouchUpInside)

Define method

func addProduct(button:UIButton)
{
// do button animation here
}
iDhaval
  • 3,175
  • 1
  • 11
  • 21