programing

UITextFieldDelegate 유형의 값에 ViewController 유형의 값을 할당 할 수 없습니까?

copyandpastes 2021. 1. 19. 08:16
반응형

UITextFieldDelegate 유형의 값에 ViewController 유형의 값을 할당 할 수 없습니까?


내가 줄을 썼을 때의 오류는 다음과 같습니다 self.MessageTextField.delegate = self.

/ChatApp/ViewController.swift:27:42 : 'UITextFieldDelegate?'유형의 값에 'ViewController'유형의 값을 할당 할 수 없습니다.

다음은 내 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은 AS delegateUITextField.
하지만 당신은 ViewController 하지 않습니다UITextFieldDelegate . 클래스를 이런 종류의 위임자로 만들려면 UITextFieldDelegate프로토콜 을 채택해야 합니다 . 이것은 클래스가 상속받은 / 준수하는 프로토콜 및 클래스 목록에 추가하여 수행 할 수 있습니다. 라인을 변경하여 수행되는 경우

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

...에

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate

Declare that your class conforms to the UITextFieldDelegate protocol and implement any of those protocol methods that you need.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate { ... }

I had the same problem, but it was because of a different issue:

class CustomViewController: UIViewController {

    let customerCardTableView: UITableView = {
        let table = UITableView()
        table.translatesAutoresizingMaskIntoConstraints = false
        table.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)
        table.delegate = self
        table.dataSource = self
        table.separatorStyle = .none
        table.separatorColor = UIColor(rgb:0xFFFFFF)
        table.separatorInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)

        return table
    }()

    ...other code 
}

and I did extend my CustomViewController to conform to the UITableViewDelegate, UITableViewDataSource protocols.

The reason was that self isn't accessible if you're using let. I had to lazy var customerCardTableView: UITableView i.e.:

lazy var customerCardTableView: UITableView = {
    let table = UITableView()
    table.translatesAutoresizingMaskIntoConstraints = false
    table.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)
    table.delegate = self
    table.dataSource = self
    table.separatorStyle = .none
    table.separatorColor = UIColor(rgb:0xFFFFFF)
    table.separatorInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)

    return table
}()

lets are required to happen during instantiation, but lazy vars can be delayed to a time after instantiation, hence they can access self...


Embarrassing to own up to it, but worth a mention. Make sure that you're extending the correct view controller when declaring conformance. For example, don't do this by accident.

class ViewController: UIViewController {

}

extension SimilarlyNamedViewController: UITableViewDelegate {

}

It's an easy one to do if you use autocomplete and gets by you because, at a glance, it looks the same.

ReferenceURL : https://stackoverflow.com/questions/29605496/cannot-assign-a-value-of-type-viewcontroller-to-a-value-of-type-uitextfielddeleg

반응형