swift - Segment controller issue -
i've got segment controller above connected code bellow.
@iboutlet weak var coatsegmentcontroller: uisegmentedcontrol! @ibaction func coatselectinput() { switch (self.unitsegmentcontroller.selectedsegmentindex) { case 0: coatsetter = 1 //println("switched coat 1") println(unitsegmentcontroller.selectedsegmentindex) case 1: coatsetter = 2 //println("switched coat 2") println(unitsegmentcontroller.selectedsegmentindex) case 2: coatsetter = 3 //println("switched coat 3") println(unitsegmentcontroller.selectedsegmentindex) default: println("coat switch did not work") } }
no matter segment select return i've selected first segment.
if hook unit segment outlet works. unit 1 stops working.
@iboutlet weak var unitsegmentcontroller: uisegmentedcontrol!
any idea be? i've tried everything. delete , create again many times. nothing seems work.
here full code view.
import uikit import coredata class paintviewcontroller: uiviewcontroller, uitextfielddelegate { //fields ********************************************************* @iboutlet weak var widthfield: uitextfield! @iboutlet weak var heigthfield: uitextfield! @iboutlet weak var basecoatpricefield: uitextfield! @iboutlet weak var basecoatcansizefield: uitextfield! @iboutlet weak var topcoatpricefield: uitextfield! @iboutlet weak var topcoatcansizefield: uitextfield! //field labels *************************************************** @iboutlet weak var areatopaintheader: uilabel! @iboutlet weak var basecoatpriceheader: uilabel! @iboutlet weak var topcoatheader: uilabel! @iboutlet weak var basecoatunit: uilabel! @iboutlet weak var topcoatunit: uilabel! //result labels @iboutlet weak var resultbasecoatunit: uilabel! @iboutlet weak var resultbasecoatamount: uilabel! @iboutlet weak var resultbasecoatcost: uilabel! @iboutlet weak var resulttopcoatunit: uilabel! @iboutlet weak var resulttopcoatamount: uilabel! @iboutlet weak var resulttopcoatcost: uilabel! @iboutlet weak var basecoatnocostwarning: uilabel! @iboutlet weak var topcoatnocostwarning: uilabel! // other controlers @iboutlet weak var whatthehell: uisegmentedcontrol! @iboutlet weak var coatsegmentcontroller: uisegmentedcontrol! @iboutlet weak var mainscrollview: uiscrollview! @iboutlet weak var unitsegmentcontroller: uisegmentedcontrol! // @iboutlet weak var coatsegcontroller: uisegmentedcontrol! // instances ****************************************************** var unitsetter = "metric" var coatsetter = 1 //create concrete core data object var paint = [paintentity]() let managedobjectcontext = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext // var calculations = paintmodel( // width: 0.0, // heigth: 0.0, // basecoatprice: 0.0, // basecoatcanamount: 0.0, // topcoatprice: 0.0, // topcoatcanamount: 0.0, // unit: "metric", // coats: 1.0) // ****************************************************************** // native methods // ****************************************************************** override func viewdidload() { super.viewdidload() //empty cost warning label basecoatnocostwarning.text = "" topcoatnocostwarning.text = "" // reset can basecoatcansizefield.text = "5.0" topcoatcansizefield.text = "5.0" // calculations.basecoatcanamount = 5.0 // calculations.topcoatcanamount = 5.0 // apply icon let imageview = uiimageview(frame: cgrect(x: 0, y: 0, width: 31, height: 36)) imageview.contentmode = .scaleaspectfit let image = uiimage(named: "concrete_bag_detail") imageview.image = image navigationitem.titleview = imageview // nav bar bg custom ******************** var navbarcolor = navigationcontroller!.navigationbar // bg color navbarcolor.bartintcolor = uicolor(hue: 162/360.0, saturation: 80/100.0, brightness: 45/100.0, alpha: 100.0/100.0) navbarcolor.titletextattributes = [nsforegroundcolorattributename: uicolor.blackcolor()] // status bar white color ******************** uiapplication.sharedapplication().statusbarstyle = uistatusbarstyle.lightcontent // detect tap trigger hide keyboard let tapgesture: uitapgesturerecognizer = uitapgesturerecognizer(target: self, action: "hidekeyboard") tapgesture.cancelstouchesinview = false mainscrollview.addgesturerecognizer(tapgesture) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // ****************************************************************** // methods // ****************************************************************** // reset ****************************************************** // func resetdatamodel (){ // //// calculations.width = 0.0 //// calculations.heigth = 0.0 //// calculations.basecoatprice = 0.0 //// calculations.basecoatcanamount = 0.0 //// calculations.topcoatprice = 0.0 //// calculations.topcoatcanamount = 0.0 // // } func resetinputandlabels(){ widthfield.text = "" heigthfield.text = "" basecoatpricefield.text = "" topcoatpricefield.text = "" basecoatnocostwarning.text = "" topcoatnocostwarning.text = "" resultbasecoatamount.text = "0.0" resultbasecoatcost.text = "$0.00" resulttopcoatamount.text = "0.0" resulttopcoatcost.text = "$0.00" switch unitsetter { case "metric": basecoatcansizefield.text = "5.0" topcoatcansizefield.text = "5.0" basecoatunit.text = "litre can" topcoatunit.text = "litre can" resultbasecoatunit.text = "litres" resulttopcoatunit.text = "litres" case "imperial": basecoatcansizefield.text = "1.0" topcoatcansizefield.text = "1.0" basecoatunit.text = "gal can" topcoatunit.text = "gal can" resultbasecoatunit.text = "gallons" resulttopcoatunit.text = "gallons" default: println("not able reset labels") } } //alert view methods ****************************************************** func alertviewlaunch (#message: string){ var alertview = uialertview(title: "ops!", message: message, delegate: self, cancelbuttontitle: "ok,got it!") alertview.show() } //keyboard resize view ****************************************** // call method somewhere in view controller setup code. func registerforkeyboardnotifications() { let notificationcenter = nsnotificationcenter.defaultcenter() notificationcenter.addobserver(self, selector: "keyboardwillbeshown:", name: uikeyboardwillshownotification, object: nil) notificationcenter.addobserver(self, selector: "keyboardwillbehidden:", name: uikeyboardwillhidenotification, object: nil) } // called when uikeyboarddidshownotification sent. func keyboardwillbeshown(sender: nsnotification) { let info: nsdictionary = sender.userinfo! let value: nsvalue = info.valueforkey(uikeyboardframebeginuserinfokey) as! nsvalue let keyboardsize: cgsize = value.cgrectvalue().size let contentinsets: uiedgeinsets = uiedgeinsetsmake(0.0, 0.0, keyboardsize.height, 0.0) mainscrollview.contentinset = contentinsets mainscrollview.scrollindicatorinsets = contentinsets // if active text field hidden keyboard, scroll it's visible // app might not need or want behavior. var arect: cgrect = self.view.frame arect.size.height -= keyboardsize.height } // called when uikeyboardwillhidenotification sent func keyboardwillbehidden(sender: nsnotification) { let contentinsets: uiedgeinsets = uiedgeinsetszero mainscrollview.contentinset = contentinsets mainscrollview.scrollindicatorinsets = contentinsets } override func viewwillappear(animated: bool) { super.viewwillappear(animated) self.registerforkeyboardnotifications() } override func viewdiddisappear(animated: bool) { super.viewwilldisappear(animated) nsnotificationcenter.defaultcenter().removeobserver(self) } // hide keyboard on touch **************************************** func hidekeyboard(){ widthfield.resignfirstresponder() heigthfield.resignfirstresponder() basecoatpricefield.resignfirstresponder() topcoatpricefield.resignfirstresponder() basecoatcansizefield.resignfirstresponder() topcoatcansizefield.resignfirstresponder() } // ********************************************************************************* // actions // ********************************************************************************* @ibaction func resetbutton() { resetinputandlabels() // resetdatamodel() } // change unit ************************************************** @ibaction func unitselectinput() { switch unitsegmentcontroller.selectedsegmentindex { case 0: unitsetter = "metric" resetinputandlabels() // resetdatamodel() //println("switched metric") println(unitsegmentcontroller.selectedsegmentindex) case 1: unitsetter = "imperial" resetinputandlabels() // resetdatamodel() //println("switch imperial") println(unitsegmentcontroller.selectedsegmentindex) default: println("unit switch did not work") } } // change coat ********************************************** @ibaction func coatselectinput() { switch (self.unitsegmentcontroller.selectedsegmentindex) { case 0: coatsetter = 1 //println("switched coat 1") println(unitsegmentcontroller.selectedsegmentindex) case 1: coatsetter = 2 //println("switched coat 2") println(unitsegmentcontroller.selectedsegmentindex) case 2: coatsetter = 3 //println("switched coat 3") println(unitsegmentcontroller.selectedsegmentindex) default: println("coat switch did not work") } } // generate results ********************************************** @ibaction func generateresults() { //scroll view show results - 4s let screensize: cgrect = uiscreen.mainscreen().bounds //println("screen heigth - \(screensize.height)") if screensize.height <= 480 { uiscrollview.animatewithduration(0.5, delay: 0.0, usingspringwithdamping: 1.5, initialspringvelocity: 1.5, options: uiviewanimationoptions.curvelinear, animations: { self.mainscrollview.contentoffset.y = 220.0 }, completion: { void in uiview.animatewithduration(0.6, delay: 0.0, usingspringwithdamping: 0.1, initialspringvelocity: 3.0, options: uiviewanimationoptions.curvelinear, animations: {}, completion: { void in }) }) } //save coredata //detect if no data has been entered if widthfield.text == "" || heigthfield.text == "" || basecoatcansizefield.text == "" || topcoatcansizefield.text == "" { alertviewlaunch(message: "i need @ least width, heigth, basecoat can size , topcoat can size basic calculations done") } else { //store data depending on unit switch unitsetter { case "metric": paintentity.createinmanagedobjectcontext(self.managedobjectcontext!, width: nsstring(string: (widthfield.text)).doublevalue, heigth: nsstring(string: (heigthfield.text)).doublevalue, basecoatprice: nsstring(string: (basecoatpricefield.text)).doublevalue, basecoatcansize: nsstring(string: (basecoatcansizefield.text)).doublevalue, topcoatprice: nsstring(string: (topcoatpricefield.text)).doublevalue, topcoatcansize: nsstring(string: (topcoatcansizefield.text)).doublevalue, unit: "metric", coats: coatsetter) case "imperial":paintentity.createinmanagedobjectcontext(self.managedobjectcontext!, width: nsstring(string: (widthfield.text)).doublevalue, heigth: nsstring(string: (heigthfield.text)).doublevalue, basecoatprice: nsstring(string: (basecoatpricefield.text)).doublevalue, basecoatcansize: nsstring(string: (basecoatcansizefield.text)).doublevalue, topcoatprice: nsstring(string: (topcoatpricefield.text)).doublevalue, topcoatcansize: nsstring(string: (topcoatcansizefield.text)).doublevalue, unit: "imperial", coats: coatsetter) default: println("no unit detected") } printresults() } } func printresults(){ let fetchrequest = nsfetchrequest(entityname: "paintentity") let sortdescriptor = nssortdescriptor(key: "date", ascending: false) fetchrequest.sortdescriptors = [sortdescriptor] fetchrequest.fetchlimit = 1 var error : nserror? if let fetchresults = managedobjectcontext!.executefetchrequest(fetchrequest, error: &error) as? [paintentity] { resultbasecoatamount.text = "\(roundnumbertoone(fetchresults[0].resultbasecoatamount.doublevalue))" resulttopcoatamount.text = "\(roundnumbertoone(fetchresults[0].resulttopcoatamount.doublevalue))" //println("i fetching \(fetchresults[0])") if basecoatpricefield.text == "" || topcoatpricefield.text == "" { resultbasecoatcost.textcolor = uicolor(hue: 162/360.0, saturation: 65/100.0, brightness: 45/100.0, alpha: 100.0/100.0) basecoatnocostwarning.text = "missing price" resultbasecoatcost.text = "$0.00" resulttopcoatcost.textcolor = uicolor(hue: 162/360.0, saturation: 65/100.0, brightness: 45/100.0, alpha: 100.0/100.0) topcoatnocostwarning.text = "missing price" resulttopcoatcost.text = "$0.00" }else{ if basecoatpricefield == "" { resultbasecoatcost.textcolor = uicolor(hue: 162/360.0, saturation: 65/100.0, brightness: 45/100.0, alpha: 100.0/100.0) basecoatnocostwarning.text = "missing price" resultbasecoatcost.text = "$0.00" } else { resultbasecoatcost.textcolor = uicolor.whitecolor() basecoatnocostwarning.text = "" resultbasecoatcost.text = "$\(roundnumbertotwo(fetchresults[0].resultbasecoatcost.doublevalue))" } if topcoatpricefield == "" { resulttopcoatcost.textcolor = uicolor(hue: 165/360.0, saturation: 63/100.0, brightness: 53/100.0, alpha: 100.0/100.0) topcoatnocostwarning.text = "missing price" resulttopcoatcost.text = "$0.00" }else{ resulttopcoatcost.textcolor = uicolor.whitecolor() topcoatnocostwarning.text = "" resulttopcoatcost.text = "$\(roundnumbertotwo(fetchresults[0].resulttopcoatcost.doublevalue))" } } } } }
Comments
Post a Comment