ios - I am doing a basic Contactlist application. But the details that I give in my DetailViewController does not save and show in my MasterViewController. -
i creating simple application contact list. masterviewcontroller has contacts , detailviewcontroller has details of contacts such : firstname, middlename, lastname etc. when enter data detailviewcontroller should save , hitting button saves these data automatically. except works fine. enter data , hit button data not saved. appreciate if help. know simple error, not figure out is.
detailviewcontroller
import uikit protocol detailviewcontrollerdelegate{ func detailviewcontroller(dvc: detailviewcontroller, didupdate contact:person) } class detailviewcontroller: uiviewcontroller { var delegate: detailviewcontrollerdelegate? var contact: contactlistentry! @iboutlet weak var txtphonenumber: uitextfield! @iboutlet weak var txtlastname: uitextfield! @iboutlet weak var address: uitextfield! @iboutlet weak var yearofbirth: uitextfield! @iboutlet weak var middlename: uitextfield! @iboutlet weak var firstname: uitextfield! @ibaction func okpressed(sender: anyobject) { if let lastname = txtlastname.text { if let phonenumber = txtphonenumber.text { if let firstname = firstname.text { if let middlename = middlename.text { if let yearofbirth = yearofbirth.text { contact.lastname = lastname contact.phonenumber = phonenumber contact.firstname = firstname contact.middlename = middlename contact.yearofbirth = yearofbirth.toint() delegate?.detailviewcontroller(self,didupdate: contact) } } } } } } override func viewdidappear(animated: bool) { firstname.text = "\(contact.firstname)" txtlastname.text = "\(contact.lastname)" txtphonenumber.text = "\(contact.phonenumber)" } override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
masterviewcontroller
import uikit class contacttableviewcontroller: uitableviewcontroller, detailviewcontrollerdelegate{ var contacts: [contactlistentry] = [] var currentcontact: contactlistentry! var detailobject: detailviewcontroller! override func viewdidload() { super.viewdidload() // uncomment following line preserve selection between presentations // self.clearsselectiononviewwillappear = false // uncomment following line display edit button in navigation bar view controller. self.navigationitem.leftbarbuttonitem = self.editbuttonitem() } @ibaction func addcontact(sender: anyobject) { currentcontact = contactlistentry(firstname: "", lastname: "", phonenumber: "123") contacts.append(currentcontact) performseguewithidentifier("showdetail", sender: self) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // mark: - table view data source override func numberofsectionsintableview(tableview: uitableview) -> int { // #warning potentially incomplete method implementation. // return number of sections. return 1 } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { // #warning incomplete method implementation. // return number of rows in section. return contacts.count } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("contactcell", forindexpath: indexpath) as! uitableviewcell let contact = contacts[indexpath.row] cell.textlabel?.text = "\(contact.firstname) ,\(contact.lastname) ,\(contact.phonenumber)" return cell } /* // override support conditional editing of table view. override func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool { // return no if not want specified item editable. return true } */ // override support editing table view. override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { if editingstyle == .delete { contacts.removeatindex(indexpath.row) tableview.deleterowsatindexpaths([indexpath], withrowanimation: .fade) } else if editingstyle == .insert { // create new instance of appropriate class, insert array, , add new row table view } } /* // override support rearranging table view. override func tableview(tableview: uitableview, moverowatindexpath fromindexpath: nsindexpath, toindexpath: nsindexpath) { } */ /* // override support conditional rearranging of table view. override func tableview(tableview: uitableview, canmoverowatindexpath indexpath: nsindexpath) -> bool { // return no if not want item re-orderable. return true } */ // mark: - navigation // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if let cell = sender as? uitableviewcell { let indexpath = tableview.indexpathforcell(cell)! currentcontact = contacts[indexpath.row] } if let dvc = segue.destinationviewcontroller as? detailviewcontroller { dvc.contact = currentcontact dvc.delegate = self } } func detailviewcontroller(dvc: detailviewcontroller, didupdate contact: person) { dvc.dismissviewcontrolleranimated(true, completion: nil) navigationcontroller?.poptoviewcontroller(self, animated: true) tableview.reloaddata() } }
swift file
import foundation class person{ var firstname: string var middlename: string? var lastname: string var yearofbirth: int? let currentyear = nscalendar.currentcalendar().component(nscalendarunit.calendarunityear, fromdate: nsdate()) //delegated initialiser init(firstname: string, lastname: string, yearofbirth: int? = nil, middlename: string? = nil) { self.firstname = firstname self.lastname = lastname self.yearofbirth = yearofbirth self.middlename = middlename } //convenience initialiser convenience init(firstname: string, lastname: string, age: int, middlename: string? = nil) { self.init(firstname: firstname, lastname: lastname, age: age, middlename: middlename) } //age computed property //this calculates age current year , year of birth var age: int!{ get{ self.age = currentyear - yearofbirth! return self.age } set{ self.yearofbirth = currentyear - age } } //fullname function returns fullname of person when first name , last name entered func fullname() -> string { if middlename != nil{ return firstname + " " + middlename! + " " + lastname } else { return firstname + " " + lastname } } } // subclass of person class accepts address , phone number class contactlistentry: person{ var address : string? var phonenumber : string? init(firstname: string, lastname: string, yearofbirth: int? = nil, middlename: string? = nil, address: string? = nil, phonenumber: string? = ""){ self.address = address self.phonenumber = phonenumber super.init(firstname: firstname, lastname: lastname, yearofbirth: yearofbirth?, middlename: middlename?) } } // class accept contact list entries class contactlist{ var entries: [contactlistentry]=[] }
first off, since swift 1.2 can bind multiple optionals, no need nested if
s:
if let lastname = txtlastname.text, phonenumber = txtphonenumber.text, firstname = firstname.text, middlename = middlename.text, yearofbirth = yearofbirth.text { // ... }
update: looks tap system-provided 'back' button, appears automatically in navigation bar after perform segue detailed view. if that's case, move code okpressed()
viewwilldisappear()
method. then, in detailviewcontroller(dvc: detailviewcontroller, didupdate contact:person)
method need have tableview.reloaddata()
, rid of rest. hope works!
Comments
Post a Comment