roller类型的值分配给类型为UITextFieldDeleg

roller类型的值分配给类型为UITextFieldDeleg

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

问题描述

下面是错误,当我写的行 self.MessageTextField.delegate = self

Here's the error when I wrote the line self.MessageTextField.delegate = self:

这是我的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 self.MessageTextField.delegate = self 会导致错误$ c>作为委托的一个 UITextField

但是你的 ViewController 不是一个 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

to

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate

这篇关于不能将ViewController类型的值分配给类型为UITextFieldDelegate的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:33