Programmatically switching views swift

让人想犯罪 __ 提交于 2020-01-10 08:53:05

问题


I am trying out apple's new language swift in Xcode 6 beta. I am trying to programmatically switch views when a table view cell is pressed, although all it does is pull up a blank black screen. In my code, I commented out the things that didn't work. They would just make the iOS simulator crash. my code:

 // ViewController.swift 
 // Step-By-Step Tip Calculator  
 // Created by Dani Smith on 6/17/14. 
 // Copyright (c) 2014 Dani Smith Productions. All rights reserved.

import UIKit
var billTotalPostTax = 0
class BillInfoViewController: UIViewController {

//outlets
@IBOutlet var totalTextField: UITextField
@IBOutlet var taxPctLabel: UILabel
@IBOutlet var resultsTextView: UITextView
@IBOutlet var taxPctTextField : UITextField

//variables
var billTotalVar = ""
var taxPctVar = ""

//actions
override func viewDidLoad() {
    super.viewDidLoad()
    refreshUI()
}

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

@IBAction func stepOneNextPressed(sender : AnyObject)
{
    billTotalVar = totalTextField.text
    taxPctVar = taxPctTextField.text
}

@IBAction func calculateTipped(sender : AnyObject)
{
    tipCalc.total = Double(totalTextField.text.bridgeToObjectiveC().doubleValue)
    let possibleTips = tipCalc.returnPossibleTips()
    var results = ""
    for (tipPct, tipValue) in possibleTips
    {
        results += "\(tipPct)%: \(tipValue)\n"
    }

    resultsTextView.text = results
}
/*
@IBAction func taxPercentageChanged(sender : AnyObject)
{
    tipCalc.taxPct = String(taxPctTextField.text) / 100
    refreshUI()
}*/

@IBAction func viewTapped(sender : AnyObject)
{
    totalTextField.resignFirstResponder()
}

let tipCalc = TipCalculatorModel(total: 33.25, taxPct: 0.06)

func refreshUI()
{
    //totalTextField.text = String(tipCalc.total)

}
}

class RestaurantTableViewController: UITableViewController
{
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{
    let row = indexPath?.row
    println(row)
    //let vc:BillInfoViewController = BillInfoViewController()
    //let vc = self.storyboard.instantiateViewControllerWithIdentifier("billInfo") as UINavigationController

    //self.presentViewController(vc, animated: true, completion: nil)
}
}

Thank you so much for all of your help.


回答1:


I just figured it out. I had to make

let vc = self.storyboard.instantiateViewControllerWithIdentifier("billInfo")` 

to be

let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("billInfo")

to silence a warning. I then changed presentViewController to showViewController, and it worked. Here is my completed class:

class RestaurantTableViewController: UITableViewController
{
    override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
    {
        let row = indexPath?.row
        println(row)
        let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("billInfo")
        self.showViewController(vc as UIViewController, sender: vc)
    }
}



回答2:


let vc = self.storyboard.instantiateViewControllerWithIdentifier("billInfo") as! BillInfoViewController
self.presentViewController(vc, animated: true, completion: nil)

LE: downcast added



来源:https://stackoverflow.com/questions/24336581/programmatically-switching-views-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!