在 segue 中使用类时遇到问题

Posted

技术标签:

【中文标题】在 segue 中使用类时遇到问题【英文标题】:Having issues using a class within a segue 【发布时间】:2019-10-10 00:40:13 【问题描述】:

试图让“编辑”VC 将用户通过 Storyboard 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)"


我尝试了很多方法,但没有任何方法可以让我将用户输入用于编辑字段来创建“事件”,然后通过已经工作的展开 segue 将其传递到我的显示(目标)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 = ""

【问题讨论】:

您的形式参数init(adminUser:String?, eventId:String?, eventName:String?, eventCoverUrl:String?, eventLocation:String?, eventStart:String?, eventStatus:String?, eventDescription:String?, eventHost:String?) 与您尝试使用的参数(eventName: String, eventLocation: String, eventStart: String, eventDescription: String, eventHost: String) 不匹配。你不能跳过参数,除非它们有默认值(在形式参数列表中指定) Event(eventName, eventLocation, eventStart, eventDescription, eventHost) 方法在哪里?你从来没有定义它。你不能只是不包含参数。 既然我是这里的n00b,能不能帮我更基本的理解?如果我将 adminUser、eventId、eventName、eventCoverUrl.... 初始化为 param = " " 我仍然会遇到同样的错误。 是否有一种更简单的方法来创建“事件”对象而不是使用预定义的 intilizer,因为只有通过用户在相应文本字段中输入内容而更新的字段才需要通过转场? @akash23a 您将它们全部初始化为空白字符串的邮政编码。这应该是你的解决方案。 【参考方案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)

【讨论】:

所以例如 adminUser 不是这个 VC 上的一个字段,那么 adminUser: 的右边会是什么?因为每个参数都需要出现在这个函数调用中。显然 eventName: eventName 等参数是实际在这个 VC 上的。 你已经有了处理程序,以防任何参数为 nil,所以只需输入adminUser: nil。或者创建另一个没有 adminUser 参数的初始化程序。

以上是关于在 segue 中使用类时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章

为啥我在编译模板类时遇到问题?

当我尝试使用 Tomcat 服务器在 java servlet 中发送 GET 请求时,我在实例化 servlet 类时遇到错误

乳清我在识别 javascript 上的类时遇到问题

故事板中的 Segues 问题

iOS Segue ViewController 缓存

适配 iOS 8 时遇到的问题两则:远程推送和 Unwind Segue