Create Custom IBAction in Controller of my Custom View

感情迁移 提交于 2020-01-04 07:09:22

问题


firstly I cannot post any Code but I´ll try to describe my problem as best as I can.

Ok. I created a Custom Swift View File as view.xib and loading it in my view.swift I loaded that Xib. The Xib is a simple label above a imageview. I can create my IBOutlet in my view.swift file and e.g. switch the image or change the label. view.swift simply extends UIView.

So now I integrated this view in my Storyboard which has a Controller (lets call it otherVC.swift). Now otherVC.swift wants to get info that the view.swift is touched. I tried setting a IBAction but there no option to set on, just on IBOutlet. After some Googe I found that the Class UiControl has those mechanics to set a IBAction.

So I view.swift no longer extends UIView but now UIControl. After that I Could create an IBAction with a touch event. (like touch down or touch inside) When running the app with the view in the simulator I can click the view but the IBAction wont be callled.

So my question is how can I set a IBAction for my View in a other VC or maybe create my own IBActions for the specific view.

again ..not able to post code. would welcome snippets or links with working example

Thanks!


回答1:


I think it will be hard to create custom IBAction(if not impossible), but your problem has many other solutions like delegate, Notifications, and closers, so I recommend to use closers, It is most simple way in my opinion. below is code how can you do that with closers

class View {
    var someButtonTapped: (UIButton) -> Void = {_ in }

    @IBAction private func someButtonTappedAction(_ sender: UIButton){
        someButtonTapped(sender)
    }
}

And in your controller

objectOfYourCustomView.someButtonTapped = {sender in
//handle action
}



回答2:


What i understood i am answering on that basis. Create the public property of your views u want to access in the viewcontroller. You will be able to access customview. propertyName. Hope this may help.



来源:https://stackoverflow.com/questions/44845562/create-custom-ibaction-in-controller-of-my-custom-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!