这是我写该行时的错误self.MessageTextField.delegate = self:
self.MessageTextField.delegate = self
/ChatApp/ViewController.swift:27:42:无法将“ ViewController”类型的值分配给“ UITextFieldDelegate”类型的值?
这是我的Swift代码(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作为delegate的UITextField。 但是,你ViewController 是不是 一个UITextFieldDelegate。为了使您的课程成为此类委托,您需要采用UITextFieldDelegate协议。可以通过将其添加到您的类继承自/遵循的协议和类列表中来实现。在您的情况下,可以通过更改行来完成
self
delegate
UITextField
ViewController
UITextFieldDelegate
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
至
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate