swift - Button to upload integer to Firebase database IOS -
when button pressed, need save rating of item in database. while slider works , variable 'currentvalue' works, have unable rating submitted database though crashes app. if i've made simple mistake, let me know.
import uikit import fbsdkcorekit import fbsdkloginkit import firebasedatabase class secondviewcontroller: uiviewcontroller { var ref:firdatabasereference! @iboutlet weak var sliderlabel: uilabel! @iboutlet weak var verticalslider: uislider! { didset{ verticalslider.transform = cgaffinetransform(rotationangle: cgfloat(-m_pi_2)) } } @ibaction func verticalsliderchanged(_ sender: uislider) { var currentvalue = int(sender.value); sliderlabel?.text = "\(currentvalue)" } @ibaction func checkmarkbuttonpressed(_ sender: any) { let img:uiimageview = uiimageview(frame: cgrect(x: 30, y: 180, width: 250, height: 320)) let randomimage = arc4random() if (randomimage % 4 == 0) { img.image = uiimage(named: "hero.jpg") } else if (randomimage % 3 == 1) { img.image = uiimage(named: "hillary.jpg") } else { img.image = uiimage(named: "thistimeisdifferent.png") **self.ref.child(uuid).child("book info").child("book title").child("slider value").setvalue(currentvalue)** } }
while not able give exact answer fix without seeing more of project, can suggest few things.
first, make sure of @iboutlets , @ibactions connected. sure cause crash if these objects , actions not connected. troubleshoot this, comment out code , set breakpoint on actions , see if able interact outlets , actions.
second, validate execution of code make sure working safe data. example,
@ibaction func verticalsliderchanged(_ sender: uislider) { // allow safer way interact data if let currentvalue = sender.value as? int { sliderlabel?.text = "\(currentvalue)" } } @ibaction func checkmarkbuttonpressed(_ sender: any) { let img:uiimageview = uiimageview(frame: cgrect(x: 30, y: 180, width: 250, height: 320)) self.view.addsubview(img) guard img != nil else { return } let randomimage = arc4random() if (randomimage % 4 == 0) { img.image = uiimage(named: "hero.jpg") } else if (randomimage % 3 == 1) { img.image = uiimage(named: "hillary.jpg") } else { img.image = uiimage(named: "thistimeisdifferent.png") guard let currentval = currentvalue, currentval.characters.count > 0 else { return } self.ref.child(uuid).child("book info").child("book title").child("slider value").setvalue(currentval) }
validating data working each step of way should allow determine if issue data inserting firebase, outlets, or application logic.
hope helps.
Comments
Post a Comment