2015-03-19 61 views
-2

我有一个带有TableView和Button(Big Button)的UIView。 TableView有一个自定义单元格。在这个单元格中有一个“添加”按钮。我想在用户点击添加按钮时为第一个按钮设置动画。Swift - 在其他类中访问IBOutlet

这是我的架构:

enter image description here

这是我的代码:

class ProductsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

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

} 

ProductCell类我的应用程序的

class ProductCell: UITableViewCell { 

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

屏幕例如

enter image description here

我试图让父控制器或上海华获得IBOutlet中,但应用程序崩溃八方通

+0

为什么downvotes?我是快速的小菜... – pekpon 2015-03-19 01:42:29

+2

为什么不在didSelectCell ...范例中使用NSNotification?或者一些自定义的代表团...在SO上有大量的样本。斯威夫特有一个叫做财产观察的东西(willSet,et al),但我认为它不适用于你的情况。你可以尝试使用UIDynamics或UIView动画的手势识别器(全部在SO上) – 2015-03-19 02:27:40

回答

0

添加块属性到你的细胞,它可以让他们通知你的视图控制器已经被点击时,他们。在你的视图控制器块代码中,你可以访问大按钮。

请参阅我对类似问题的回答。只需用您的按钮替换开关示例。所以用UIButton替换UISwitch。

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

因此而不是让细胞试图跟另一个单元/按钮,有小区通​​知管理员然后可以管理的大按钮的变化。

0

尽管我对使用替代方法发表了评论,但您也可以根据对当前视图控制器类中存储的属性的更新来使用以下策略。您也可以在ProductsViewController上使用属性观察,但我认为您希望保持OOP集中并减少控制器的大小。

子类中的ViewController

人们可以继承现有的UIViewController,然后创建超类,与已更改值有关系的属性(行挖掘)。在那个子类中,你可以做一些动画。因为您将继承子类,您将继续获得现有控制器中定义的所有优点和方法。在您的身份检查器中,将您的类指向新的子类,并使用动画为您的UI创建任何功能更新。

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. 

    } 
} 

地产观察

每当didSelectCell ...方法被称为只是设置一个值inheritedProperty。然后添加属性观察器(请参阅示例代码),并在属性更改时作出反应(可能会将引用传递给要设置动画的视图)。

例如:在属性观察者中,您可以将该视图传递给您的动画器函数(无论将要做什么动画)。有很多关于如何动画视图的例子,所以只需搜索(UIView动画,UIDynamics等)。

分离的正常好处是功能封装和重用,但Swift也保证每组属性观察者都会独立触发。在这个用例中,你必须对它的适用性给予更多的考虑。

0

不要在你的viewController

添加目标的方法这一切事情细胞在cellForRowAtIndexPath方法 添加按钮,像这样

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

定义方法

func addProduct(button:UIButton) 
{ 
// do button animation here 
}