无法将 ViewController 类型的值分配给 UITextFieldDelegate 类型的值? [英] Cannot assign a value of type ViewController to a value of type UITextFieldDelegate?

查看:24
本文介绍了无法将 ViewController 类型的值分配给 UITextFieldDelegate 类型的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我写 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 分配为UITextFielddelegate.
但是您的 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 类型的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆