2017-03-15 5 views
1

값을 곱할 수 없습니다. pickerViews 값을 가져 와서 버튼 동작에서 곱하려고합니다.하지만 오류가 발생하는 경우 다른 아이디어로 시도해 보았습니다. 그러나 슬프게도 나는 아직도 끝내려고 노력하고있는 것을 성취하지 못했다. 나는 더 나은 이해를 위해 코드를 업데이트했다. - 피커 뷰 값을 곱과 텍스트 뷰UIpickerView Swift 3

import UIKit 

class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource,UIPickerViewDelegate,UIPickerViewDataSource { 
@IBOutlet weak var productPrice: UITextField! 
@IBOutlet weak var myPrice: UILabel! 
@IBOutlet weak var pickerView: UIPickerView! 
@IBOutlet weak var perlol: UILabel! 
var distribTopp:Array = [String]() 
var distribBottomm:Array = [String]() 
var priceLabell:Array = [String]() 
var titleBarr:Array = [String]() 
var iconeImage:Array = [UIImage]() 

override func viewDidLoad() { 
    titleBarr = ["Fees","","","Profit","Calculations",""] 

    distribTopp = ["Paypal Fee","Ebay Fee","Feeless Price","Profit","Price Increase","Percent Increase"] 

    distribBottomm = ["Takes 2.9% + $0.30","Takes 10%","Price Without Fees","Profit From Original Price","How much USD Added","Percentage of Increased Price"] 

    priceLabell = ["$0.00","$0.00","$0.00","$0.00","$0.00","0.00%"] 

    iconeImage = [UIImage(named: "home")!,UIImage(named: "home")!,UIImage(named: "home")!,UIImage(named: "home")!,UIImage(named: "home")!,UIImage(named: "home")! 
    ] 

    pickerView.delegate = self 
    pickerView.dataSource = self 


    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

var percentagesPicker = ["10%","20%","30%","40%","50%"] 

// DataSource 
func numberOfComponents(in pickerView: UIPickerView) -> Int { 

    return 1 

} 

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 

    return percentagesPicker.count 
} 

// Delegate 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 


    return percentagesPicker[row] 

} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    // var thirtyone = Double(percentagesPicker[row]) 


    perlol?.text! = percentagesPicker[row] 

    if perlol?.text! == "10%"{ 

     let thirty = Double(3000/10000) 
     perlol?.text = String(thirty) 
    } 
    if perlol?.text! == "20%"{ 

     perlol?.text = String(thirty) 
    } 
    if perlol?.text! == "30%"{ 

     perlol?.text = String(thirty) 
    } 
    if perlol?.text! == "40%"{ 

     perlol?.text = String(thirty) 
    } 
    if perlol?.text! == "50%"{ 

     perlol?.text = String(thirty) 
    } 

    print(percentagesPicker[row]) 
    print(perlol) 


} 


// var percenta = self.percentagesPicker[row] 

var Duration = Double(0.30) 
var ebayfeess = Double(0.1) 
var paypalfeess = Double(0.029) 
var profitsss = Double(100) 
var thirty = Double(3000/10000) 


@IBAction func clearBtn(_ sender: Any) { 

    priceLabell = ["$0.00","$0.00","$0.00","$0.00","$0.00","0.00%"] 
    myPrice.text = String("0.00") 
    productPrice.text = String("") 

} 

@IBAction func goCalcul(_ sender: Any) { 



    let productnum = Double(productPrice.text!)! // Original Price 
    let feess = Double(productnum * thirty + productnum) //New Listing Price 
    let paypalfs = Double(paypalfeess * feess + Duration)//Paypal Fees 
    let ebayfs = Double(ebayfeess * feess) // Ebay Fee 
    let profitss = Double(((feess/productnum) * profitsss) - profitsss) //Percent 
    let priceIncrease = Double(productnum * 3000/10000) // Price Increase 
    let buying = Double(feess - paypalfs - ebayfs) // Left with 
    let profitnow = Double(buying - productnum) // Profit 

    let decimalPaypal = NSString(format: "%.2f", paypalfs) 
    let decimalebay = NSString(format: "%.2f", ebayfs) 
    let decimalbuying = NSString(format: "%.2f", buying) 
    let decimalProfit = NSString(format: "%.2f", profitnow) 
    let decimalprice = NSString(format: "%.2f", priceIncrease) 
    let decimaloriginal = NSString(format: "%.2f", feess) 
    // let l = String(perlol?.text) 
    myPrice.text = String(decimaloriginal) 
    //perlol.text = Double(perlol?.text) 
    //print(perlol.text) 

    priceLabell = [String("-$\(decimalPaypal)"),String("-$\(decimalebay)"), String("$\(decimalbuying)"), String("+$\(decimalProfit)"), String("+$\(decimalprice)"),String("+\(profitss)%")] 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return distribTopp.count & distribBottomm.count & titleBarr.count & priceLabell.count 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell") as! MenuTableViewCell 
    cell.imgLbl.image = iconeImage[indexPath.row] 
    cell.describTop.text! = distribTopp[indexPath.row] 
    cell.describBottom.text! = distribBottomm[indexPath.row] 
    cell.priceLabel.text! = priceLabell[indexPath.row] 
    cell.titleBar.text! = titleBarr[indexPath.row] 


    if cell.describTop.text! == "Paypal Fee" 
    { 
     cell.priceLabel?.textColor = UIColor.red 
    } 
    if cell.describTop.text! == "Feeless Price" 
    { 
     cell.priceLabel?.textColor = UIColor.blue 
    } 
    if cell.describTop.text! == "Profit" 
    { 
     cell.priceLabel?.textColor = UIColor.green 
    } 

    if cell.describTop.text! == "Price Increase" 
    { 
     cell.priceLabel?.textColor = UIColor.green 
    } 

    if cell.describTop.text! == "Percent Increase" 
    { 
     cell.priceLabel?.textColor = UIColor.green 
    } 
    if cell.describTop.text! == "Ebay Fee" 
    { 
     cell.priceLabel?.textColor = UIColor.red 
    } 

    return cell 
} 



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let cell:MenuTableViewCell = tableView.cellForRow(at: indexPath) as! MenuTableViewCell 

    if cell.describTop.text! == "Paypal Fee" 
    { 

    } 

} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

}

+1

'Double '에'UILabel'을 곱한 것입니까? – Eendje

+0

코드를 업데이트했습니다. – Sadin

답변

0

좀 더 정보가 도움이 될 것입니다 그것을 보여줍니다. 어떤 오류가 있습니까?

내 생각에 UIPickerView에 delegate 또는 datasource을 설정하지 않았기 때문입니다. 당신이 그들을 구현 한 후, 당신의 ViewController 클래스 정의는 다음과 같이 표시됩니다 프로토콜에

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 

명령을 클릭하면/구현해야합니다 수있는 기능을 볼 수 있습니다.

+0

코드를 업데이트했습니다. – Sadin

1

"goCalcul"메서드에서 "perlol"이란 무엇입니까? UIPickerView 대리자 메서드에서의 사용법에 기반한 레이블 인 것으로 보입니다. 그리고 그렇다면, 레이블에서 텍스트 값을 가져 와서 곱셈을하기 전에 일종의 백분율 값으로 변환하면 안됩니까?

+0

코드를 업데이트했습니다. – Sadin

+0

그게 무엇을하려하는지 알려줍니다./작동하지 않습니다. – Sadin

+0

오류 메시지는 무엇입니까? 도망? 샘플 프로젝트를 어딘가에 업로드하여 진행 상황을 다운로드하고 볼 수 있습니까? 프로젝트를 업로드 할 수 있다면 일단 완료했다면 알려주세요. – Fahim