这是我写 self.MessageTextField.delegate = self
行时的错误:
Here's the error when I wrote the line self.MessageTextField.delegate = self
:
/ChatApp/ViewController.swift:27:42: 无法将ViewController"类型的值分配给UITextFieldDelegate"类型的值?
/ChatApp/ViewController.swift:27:42: Cannot assign a value of type 'ViewController' to a value of type 'UITextFieldDelegate?'
这是我的 Swift 代码 (ViewerController.swift):
Here's my Swift code (ViewerController.swift):
//
// ViewController.swift
// ChatApp
//
// Created by David Chen on 15/4/12.
// Copyright (c) 2015年 cwsoft. All rights reserved.
//
import UIKit
import Parse
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var messagesArray:[String] = [String]()
@IBOutlet weak var MessageTableView: UITableView!
@IBOutlet weak var ButtonSend: UIButton!
@IBOutlet weak var DockViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var MessageTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//
self.MessageTableView.delegate = self
self.MessageTableView.dataSource = self
//Set delegate
self.MessageTextField.delegate = self
self.messagesArray.append("Test 1")
self.messagesArray.append("Test 2")
self.messagesArray.append("Test 3")
}
@IBAction func ButtonSendPressed(sender: UIButton) {
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5, animations: {
self.DockViewHeightConstraint.constant = 400
self.view.layoutIfNeeded()
}, completion: nil)
}
//MARK : TextField Delegage Methods
//MARK : Table View Delegate Methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.MessageTableView.dequeueReusableCellWithIdentifier("MessageCell") as! UITableViewCell
cell.textLabel?.text = self.messagesArray[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messagesArray.count
}
}
行 self.MessageTextField.delegate = self
导致错误,因为您尝试将 self
分配为UITextField
的 delegate
.
但是您的 ViewController
不是 UITextFieldDelegate
.要使您的课程成为这种委托,您需要采用 UITextFieldDelegate
协议.这可以通过将其添加到您的类继承/符合的协议和类列表中来实现.在您的情况下,这是通过更改行来完成的
The line self.MessageTextField.delegate = self
causes the error since you try to assign self
as the delegate
of a UITextField
.
But your ViewController
is not a UITextFieldDelegate
. To make your class this kind of delegte, you need to adopt the UITextFieldDelegate
protocol. This can be achieved by adding it to the list of protocols and classes your class inherits from / conforms to. In your case that is done by changing the line
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
到
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate
这篇关于无法将 ViewController 类型的值分配给 UITextFieldDelegate 类型的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!