0

I have been making simple GPA calculation app. and it takes user input from lot of text fields and do the calculate and show the result.i want to show that result in 2ndViewController

@IBAction func CalculateGpa(sender: AnyObject){
//all the calculation happen here
//example
        let gpa:Float = TotalGiCi/TotalGi

}

and i want to pass that gpa to my 2ndViewController label. so i did the coding like this

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var ResultViewController : ViewControllerResult = segue.destinationViewController as! ViewControllerResult

        ResultViewController.GPAResultLabel = "\(gpa)"
    }

Then i got the error saying Use of unresolved identifier gpa

what i can do here?

i tried removing @IBAction func CalculateGpa(sender: AnyObject){ and replacing it with override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { because sender is anyObject. then i got error

unrecognized selector sent to instance

3
  • @Bhavin I did like that and changed gpa = TotalGiCi/TotalGi !error cannot assign 'gpa' in self`` Commented Jul 11, 2015 at 3:59
  • use var instead of let in global defination Commented Jul 11, 2015 at 4:00
  • @Bhavin Before @IBAction i defined like this globally let gpa:Float and inside @IBAction gpa = TotalGiCi/TotalGi am i doing right? Commented Jul 11, 2015 at 4:01

2 Answers 2

1

Define variable globally with var because Variables declared with let must immediately be assigned a value. You cannot simply define a variable with let and no value like you're trying to do on the first line.

If you are modifying value at runtime, which is not what a constant is. Thus, you need a variable, even if its value only changes once.

class viewcontroller:UIViewcontroller{

// Define  variable gpa here with its return type...
   var gpa:Float?   

override func viewDidLoad() {
    super.viewDidLoad()
    //   relevant code
}

@IBAction func CalculateGpa(sender: AnyObject){
    gpa = TotalGiCi/TotalGi
}

// Data passing code
Sign up to request clarification or add additional context in comments.

2 Comments

Got it. and why im getting this error in here ResultViewController.GPAResultLabel = "\(gpa)" cannot assign value of type float to a value of type UIlabel
ohhh....sorry....you need to give a text property to label....like label.text...and also convert float to string
1

Global variables are variables that are defined outside of any function, method, closure, or type context.Global constants and variables are always computed lazily

Refer the below code.We have declare the variable below like this.

 class YourViewControllerName: UIViewController 
 {
   let gpa:Float = TotalGiCi/TotalGi
              //OR
   var gpa:Float = TotalGiCi/TotalGi
   @IBAction func CalculateGpa(sender: AnyObject)
   {
   //all the calculation happen here
   //example
   //Do your Calculation here
   }

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
   {          
      var ResultViewController : ViewControllerResult = segue.destinationViewController as! ViewControllerResult
      ResultViewController.GPAResultLabel = "\(gpa)"
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.