iphone - Delegating a string to a UILabel and an image to a UIImageView - Swift -
so, what's happening: have 2 controllers; mainviewcontroller , detailstableviewcontroller. on detailstableviewcontroller want write name random recipe, pick image photo library. after that, want send data cell in mainviewcontroller. code this:
import uikit import coredata class mainviewcontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate, senddetailstomvcdelegate { @iboutlet weak var mainimage: uiimageview! @iboutlet weak var tableview: uitableview! var nameslistarray: [string] = [] var imageslistarray: [uiimage] = [] override func viewdidload() { super.viewdidload() } override func viewdidappear(animated: bool) { super.viewdidappear(animated) } func senddetailstomvc (name: string, image: uiimage) { nameslistarray.append(name) imageslistarray.append(image) } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return nameslistarray.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var row = indexpath.row var name: string = nameslistarray[row] string var image: uiimage = imageslistarray[row] uiimage var cell = self.tableview.dequeuereusablecellwithidentifier("customcell") as! customcelltableviewcell cell.customlabel!.text = (name string) cell.customimage.image = (image uiimage) return cell } }
and
import uikit import coredata protocol senddetailstomvcdelegate { func senddetailstomvc(name: string, image: uiimage) } class detailsviewcontroller: uiviewcontroller, uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate, uitableviewdatasource, uitableviewdelegate, sendnametodetailsviewcontrollerdelegate { @iboutlet weak var nametextfield: uitextfield! @iboutlet weak var recipeimage: uiimageview! @iboutlet weak var tableview: uitableview! var ingredientslist = [""] var delegatedetails: senddetailstomvcdelegate? override func viewdidload() { super.viewdidload() let howtodobutton = uibarbuttonitem(title:"how do", style: uibarbuttonitemstyle.plain, target: self, action: selector("showhowtodoscreen")) navigationitem.rightbarbuttonitem = howtodobutton let newingredientbutton = uibarbuttonitem(title:"new ingredient", style: uibarbuttonitemstyle.plain, target:self, action: selector("shownewingredientscreen")) navigationitem.leftbarbuttonitem = newingredientbutton //picker imagem pelo toque let tapgesturerecognizer: uitapgesturerecognizer = uitapgesturerecognizer(target: self, action: "chooseimage:") tapgesturerecognizer.numberoftapsrequired = 1 recipeimage.addgesturerecognizer(tapgesturerecognizer) recipeimage.userinteractionenabled = true } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return ingredientslist.count } func addnametodetailsviewcontroller(nametodetail: nsstring) { ingredientslist.append(nametodetail string) if tableview == nil { return } tableview!.reloaddata() } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let row = indexpath.row let ingredientname = ingredientslist[row] var cell = uitableviewcell(style: uitableviewcellstyle.default, reuseidentifier: nil) cell.textlabel!.text = ingredientname return cell } //picka imagem pelo toque, acessando photolibrary func chooseimage(recognizer: uitapgesturerecognizer) { let imagepicker: uiimagepickercontroller = uiimagepickercontroller() imagepicker.delegate = self imagepicker.sourcetype = uiimagepickercontrollersourcetype.photolibrary self.presentviewcontroller(imagepicker, animated: true, completion: nil) } //ao selecionar imagem, coloca-a na tela tocada func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [nsobject:anyobject]) { let pickedimage: uiimage = (info nsdictionary).objectforkey(uiimagepickercontrolleroriginalimage) as! uiimage // small picture let smallpicture = scaleimagewith(pickedimage, newsize: cgsizemake(75,75)) var sizeofimageview:cgrect = recipeimage.frame sizeofimageview.size = smallpicture.size recipeimage.frame = sizeofimageview recipeimage.image = smallpicture picker.dismissviewcontrolleranimated(true, completion: nil) } func imagepickercontrollerdidcancel(picker: uiimagepickercontroller) { picker.dismissviewcontrolleranimated(true, completion: nil) } func scaleimagewith(image:uiimage, newsize: cgsize) -> uiimage { uigraphicsbeginimagecontextwithoptions(newsize, false, 0.0) image.drawinrect(cgrectmake(0,0, newsize.width, newsize.height)) let newimage: uiimage = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() return newimage } @ibaction func shownewingredientscreen() { let newingredient = addingredientviewcontroller(delegate: self) if let navigation = navigationcontroller { navigation.pushviewcontroller(newingredient, animated: true) } } @ibaction func showhowtodoscreen() { let howtodo = howtodoviewcontroller(nibname: "howtodoviewcontroller", bundle: nil) if let navigation = navigationcontroller { navigation.pushviewcontroller(howtodo, animated: true) } } @ibaction func addbutton(sender: anyobject) { if nametextfield == nil || recipeimage == nil { return } if recipeimage == nil { return } let namelabel = nametextfield.text let imageview = recipeimage.image if delegatedetails == nil { return } delegatedetails?.senddetailstomvc(namelabel, image: imageview!) println("button pressed, name \(namelabel) , image \(imageview) added.") if let navigation = self.navigationcontroller { navigation.popviewcontrolleranimated(true) } }
my question is: passing information correctly? correct way storing images creating images arrays did? when press addbutton, nothing happens, has no crashes. i'm in issue quite time , thankful if pointed me what's wrong.
by way, i'm new swift, forgive me n00b mistakes.
thanks!
should same in other question. in addbutton
function setting delegate if not nil
and not if should. delete if delegatedetails == nil { return }
detailviewcontroler - addbutton , should work.
Comments
Post a Comment