如何使用自定义视图的委托方法创建自定义委托,并将其(自定义视图)对象作为该委托方法中的参数,就像tableView的cellForRowAtIndexPath:方法一样?
使用确切问题详细阐述问题:
到目前为止,我一直使用UIKit中提供的不同视图的委托方法在我的应用程序中构建视图。但是这次我需要使用自定义委托和数据源创建自定义View。我已经做到了。但一个问题总是会提升。也就是说,每当我在ViewController中需要多个相同的自定义视图时,我将如何在我的委托方法中单独识别它们。当然,我需要在我的委托方法中传递自定义View的对象作为争论。例如。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath;
在上面的委托方法中,tableView对象是一个参数,使用它可以识别tableView,如果一个ViewController上有多个tableViews,并且这个View Controller被设置为所有这些tableViews的委托。
这是真实的情景:
//
// 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中复制粘贴代码并评估问题。
欢迎快速回答!
答案 0 :(得分:2)
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)
}
}
答案 1 :(得分:1)
您发布的代码是正确的。
在您的ViewController
中执行类似
let myView = MyCustomView()
myView.delegate = self
您告诉MyCustomView
对象将self
称为delegate
。
因此,当MyCustomView
执行此代码时
delegate!.myCustomView(self, didSomethingWithAString: aString)
它在你的代码中调用它
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...**
}
}
其中myCustomView
等于myView
。您可以使用==
运算符代替isEqual
来比较它们。
答案 2 :(得分:0)
您可以查找以下扩展程序:
extension ViewController: UITableViewDelegate {
// You extension to the UITableViewDelegate class
}
答案 3 :(得分:0)
protocol MyCustomViewDelegate {
func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String)
}
class MyCustomView: UIView {
var delegate: MyCustomViewDelegate?
func doSomethingWithAString(aString: String) {
// do something with a string
if let delegate = delegate {
delegate.myCustomView(self, didSomethingWithAString: aString)
}
}
}
class ViewController: UIVIewController {
var myView: MyCustomView!
override func viewDidLoad() {
super.viewDidLoad()
myView = MyCustomView()
//set the delegate
myView.delegate = self
}
}
//implement the delegate
extension ViewController: MyCustomViewDelegate {
func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String) {
//get the string from delegation
}
}