如何从UIView到ViewController获取按钮?

时间:2017-09-16 16:31:03

标签: ios swift uiview uiviewcontroller

我在自定义视图上创建了包含一个标签和按钮的视图 我在UIView类

中创建了两个插座

enter image description here

我有一个UIViewController,其中我有自定义视图的子视图 现在我想更改标签名称,并希望在点击按钮时执行某些操作。

  #comment.rb
 class Comment < ApplicationRecord
   validates :description, presence: true, length: {minimum: 4, maximum: 140}
   belongs_to :chef
   belongs_to :recipe
   validates :chef_id, presence: true
   validates :recipe_id, presence: true
   default_scope -> {order(updated_at: :desc)}
  end

 #recipe.rb
class Recipe < ApplicationRecord   
    validates :name, presence: true
    validates :description, presence: true, length: {minimum: 5, maximum:500}
    belongs_to :chef
    validates :chef_id, presence: true
    default_scope-> { order(updated_at: :desc) }
    has_many :recipe_ingredients
    has_many :ingredients, through: :recipe_ingredients
    has_many :comments, dependent: :destroy
    end

 #chef.rb
class Chef < ApplicationRecord
     before_save {self.email = email.downcase}
     validates :name, presence: true,
              length: {maximum: 30}
     VALID_EMAIL_REGEX = /\A[\w+-.]+@[a-z\d\-.]+\.[a-z]+\z/i
     validates :email, presence: true ,length:{maximum: 255},
              format:{with: VALID_EMAIL_REGEX},
              uniqueness:{case_sensitive: false}
     has_many :recipes, dependent: :destroy
     has_secure_password
     validates :password, presence: true, length:{minimum: 5}, allow_nil: true
     has_many :comments, dependent: :destroy
   end

 #recipe_ingredient.rb
class RecipeIngredient< ApplicationRecord
    belongs_to :ingredient
    belongs_to :recipe

   end

#ingredient.rb
class Ingredient <ApplicationRecord
    validates :name, presence: true, length: {minimum: 3, maximum: 25}
    validates_uniqueness_of :name
    has_many :recipe_ingredients
    has_many :recipes, through: :recipe_ingredients
   end


rails -v =>  Rails 5.1.3
ruby -v => ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin15]

我可以更改标签文本但是如何在UIViewcontroller中获取按钮操作事件?

3 个答案:

答案 0 :(得分:6)

使用协议

在customView中声明协议

protocol CustomViewProtocol : NSObjectProtocol{
    func buttonTapped()
}

现在在自定义视图中创建一个变量

weak var delegate : CustomViewProtocol? = nil

在viewController中确认协议

extension ViewController : CustomViewProtocol {
    func buttonTapped() {
        //do whatever u waana do here
    }
}

在视图控制器中将self设置为委托

let commonView = UINib(nibName: "CommonView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CommonView
        firstView.addSubview(commonView)
commonView.delegate = self

最后在自定义视图中的按钮的IBAction触发器委托

self.delegate?.buttonTapped()

答案 1 :(得分:0)

在Swift 4中:

如果您希望在UIViewControllerviewDidLoad()中执行此操作,请在nib实例化视图(例如,名为aView)之后执行此操作:

let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
aView.addGestureRecognizer(tap)

低于viewDidLoad声明以下函数并在其正文中执行任何操作:

@objc func tapAction() {

    //Some action here

}

注释:在Swift 4中,@objc注释是强制性的,因为它表示您希望该函数对Obj-C运行时可见, UIKit通过其消息传递系统处理(以及其他)用户交互。)

由于此UIView实例属于此UIViewController的视图层次结构,因此处理此UIViewController中的抽头是有意义的。这使您能够在任何其他UIView内重复使用此UIViewController,并从相应的UIViewController处理其抽头逻辑。

如果有一个特殊原因你想在UIView中处理抽头,你可以使用委托模式,闭包,或者在更高级的实现中使用Reactive框架(ReactiveSwift或RxSwift / RxCocoa)

答案 2 :(得分:0)

使用RxSwift / RxCocoa执行此操作的方法:

view.rx
.tapGesture()
.when(.recognized)
.subscribe(onNext: { _ in
 //react to taps
})
.disposed(by: stepBag)

更多管理手势的方法,可以在RxSwift github页面找到:https://github.com/RxSwiftCommunity/RxGesture