ios - 如何创建自定义委托(delegate)方法,如 tableView cellForRowAtIndexPath 方法,它有 tableView 的争论?

标签 ios objective-c swift uitableview delegates

如何使用自定义 View 的委托(delegate)方法创建自定义委托(delegate),并将其(自定义 View 的)对象作为该委托(delegate)方法中的参数,就像 tableView 的 cellForRowAtIndexPath: 方法一样?

详细说明问题:

到目前为止,我一直使用 UIKit 中提供的不同 View 的委托(delegate)方法来构建我的应用程序中的 View 。但是这次我需要制作一个自定义 View ,但带有自定义委托(delegate)和数据源。我已经做到了。但一个问题总是升起。也就是说,每当我的 ViewController 中需要多个相同的自定义 View 时,我将如何在我的委托(delegate)方法中分别识别它们。当然,我需要在我的委托(delegate)方法中传递自定义 View 的对象作为参数。例如。

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath;

在上面的委托(delegate)方法中,tableView 对象是一个参数,如果一个 ViewController 上有多个 tableView,我们可以使用它来识别 tableView,并且这个 View Controller 被设置为所有这些 tableView 的委托(delegate)。

这是真实场景:

  //
  //  ViewController.swift
  //  DemoCustomViewDelegate
  //
  //  Created by Shubham Ojha on 4/3/16.
  //  Copyright © 2016 Shubham Ojha. All rights reserved.
  //

  import UIKit

  protocol MyCustomViewDelegate {
  func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String)
  }

  class MyCustomView: UIView {
  var delegate: AnyObject?
  init(frame: CGRect, andViewId viewId: String, andDelegate delegate: AnyObject) {
    super.init(frame: frame)
    self.delegate = delegate
    self.doSomethingWithAString(viewId)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}
func doSomethingWithAString(aString: String) {
    // do something with a string


        delegate!.myCustomView(self, didSomethingWithAString: aString)

}
}
 class ViewController: UIViewController {

var myView: MyCustomView!
override func viewDidLoad() {
    super.viewDidLoad()

    myView = MyCustomView.init(frame: CGRectMake(0, 0, 50, 50), andViewId: "100", andDelegate: self)
    //set the delegate
    myView.delegate = self

}
func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String) {
    //this delegate method is called but the object of myCustomView I need could not get
    if(myCustomView.isEqual(myView)){
        print("Got the object")//**Does not prints, this is what I actually need to print...**

    }
}

}

您可以将代码复制粘贴到您的 ViewController 中并评估问题。

也欢迎用swift回答!

最佳答案

你是这个意思吗?

protocol MyCustomViewDelegate {
    func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String)
}

class MyCustomView: UIView {
    var delegate: MyCustomViewDelegate?

    func doSomethingWithAString(aString: String) {
        // do something with a string

        // inform the delegate
        delegate?.myCustomView(self, didSomethingWithAString: aString)
    }
}

关于ios - 如何创建自定义委托(delegate)方法,如 tableView cellForRowAtIndexPath 方法,它有 tableView 的争论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36385149/

相关文章:

iOS/objective-C : Store Decimal as NSNumber in CoreData

iphone - 如何使用 Google Custom Search 在 objective c 中进行图片搜索

ios - 在 Swift SpriteKit 中使用 SKLabelNode 作为父级创建一个按钮

ios - 如何在后台触发多步api交易?

ios - 如何从 SwiftUI 中的列表获取屏幕行?

ios - 创建具有相同属性重复条目的核心数据对象?

ios - 应如何为推送通知服务设置代码签名?

php - 在 App 中推送通知

iOS 4.3 和 CMYK JPG 图像

swift - 访问 Firebase Analytics 数据