在segue中使用类时遇到问题

时间:2019-10-10 00:40:13

标签: ios swift uistoryboardsegue

试图获取“编辑” VC以便将用户已通过情节提要(seedboard segue)更新的新信息传递回“显示” VC,但难以将用户输入存储到我初始化的类“事件”中

我不断收到此错误

Cannot invoke initializer for type 'Event' with an argument list of type '(eventName: String, eventLocation: String, eventStart: String, eventDescription: String, eventHost: String)'

我的编辑(源)VC(准备中的错误...)

class EditEventDetailTableViewController: UITableViewController {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var eventName: UITextField!
@IBOutlet weak var eventStartDate: UITextField!
@IBOutlet weak var eventLocation: UITextField!
@IBOutlet weak var eventDescription: UITextView!
@IBOutlet weak var eventHost: UITextField!

var event:Event?
var eventUpdate:Event?

override func viewDidLoad() {
    super.viewDidLoad()

    //imageView.image = detail?.photo
    eventName.text = event?.eventName
    eventStartDate.text = event?.eventStart
    eventLocation.text = event?.eventLocation
    eventDescription.text = event?.eventDescription
    eventHost.text = event?.eventHost

    guard let urlString = (event?.eventCoverUrl) else { return  }
    let url = URL(string: urlString)
         guard url != nil else {
             //Couldn't create url object
             return
         }
    self.imageView.sd_setImage(with: url) { (image, error, cacheType, url) in
        self.imageView.image = image
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {


   if let eventName = eventName.text?.trimmingCharacters(in: .whitespacesAndNewlines),
       let eventStart = eventStartDate.text?.trimmingCharacters(in: .whitespacesAndNewlines),
       let eventLocation = eventLocation.text?.trimmingCharacters(in: .whitespacesAndNewlines),
       let eventDescription = eventDescription.text?.trimmingCharacters(in: .whitespacesAndNewlines),
       let eventHost = eventHost.text?.trimmingCharacters(in: .whitespacesAndNewlines) {
    self.event = Event(eventName: eventName, eventLocation: eventLocation, eventStart: eventStart, eventDescription: eventDescription, eventHost: eventHost)
   }}}

这是我的课程

class Event {

var adminUser:String?
var eventCreated:Date?
var eventId:String?
var eventName:String?
var eventCoverUrl:String?
var eventLocation:String?
var eventStart:String?
var eventEnd:Date?
var eventStatus:String?
//var eventParticipants: [String?]
var eventDescription:String?
var eventDeleteDate:Date?
var eventHost:String?


init?(snapshot:DocumentSnapshot){

    self.eventId = snapshot.get("eventId") as? String ?? "No event Id"
    self.adminUser = snapshot.get("adminUser") as? String ?? "No admin user"
    //self.eventCreated = snapshot.get("eventCreated") as? Date ?? "No Event Created Date"
    self.eventName = snapshot.get("eventName") as? String ?? "No Event Name"
    self.eventCoverUrl = snapshot.get("eventCoverUrl") as? String ?? "No Cover URL"
    self.eventLocation = snapshot.get("eventLocation") as? String ?? "No Location"
    self.eventStart = snapshot.get("eventStart") as? String ?? "No Start Date"
    self.eventDescription = snapshot.get("eventDescription") as? String ?? "No Event Description"
    self.eventHost = snapshot.get("eventHost") as? String ?? "No Event Host"
    self.eventStatus = snapshot.get("eventStatus") as? String ?? "No Event Status"

}

init(adminUser:String?, eventId:String?, eventName:String?, eventCoverUrl:String?, eventLocation:String?, eventStart:String?, eventStatus:String?, eventDescription:String?, eventHost:String?) {

    self.adminUser = adminUser ?? "No \(LocalizedError.self)"
    self.eventId = eventId ?? "No \(LocalizedError.self)"
    self.eventName = eventName ?? "No \(LocalizedError.self)"
    self.eventCoverUrl = eventCoverUrl ?? "No \(LocalizedError.self)"
    self.eventLocation = eventLocation ?? "No \(LocalizedError.self)"
    self.eventStart = eventStart ?? "No \(LocalizedError.self)"
    self.eventStatus = eventStatus ?? "No \(LocalizedError.self)"
    self.eventDescription = eventDescription ?? "No \(LocalizedError.self)"
    self.eventHost = eventHost ?? "No \(LocalizedError.self)"
}
}

我已经尝试了多种方法,但是没有什么可以让我接受用户对已编辑字段的输入来创建“事件”,然后通过已经起作用的展开搜索将其传递到我的显示(目标)VC中。

这是Event类的更新代码,其中已将更新的参数初始化为空白字符串。其他所有内容均保持不变。

var adminUser = ""
var eventCreated = ""
var eventId = ""
var eventName = ""
var eventCoverUrl = ""
var eventLocation = ""
var eventStart = ""
var eventEnd:Date?
var eventStatus:String?
//var eventParticipants: [String?]
var eventDescription = ""
var eventDeleteDate = ""
var eventHost = ""

1 个答案:

答案 0 :(得分:1)

不能将参数放在函数调用之外。 Event(eventName, eventLocation, eventStart, eventDescription, eventHost)未定义,因为实际方法是Event(adminUser, eventId, eventName, eventCoverUrl, eventLocation, eventStart, eventStatus, eventDescription, eventHost),您需要将prepareForSegue的最后一行更改为:

self.event = Event(adminUser: adminUser, eventId: eventId, eventName: eventName, eventCoverUrl: eventCoverUrl, eventLocation: eventLocation, eventStart: eventStart, eventStatus:eventStatus, eventDescription: eventDescription, eventHost: eventHost)