以编程方式快速切换视图

时间:2014-06-20 22:30:26

标签: xcode view swift xcode6

我正在试用Xcode 6 beta中的苹果新语言swift。我正在尝试以编程方式在按下表格视图单元格时切换视图,尽管它只是拉出一个空白的黑屏。在我的代码中,我评论了那些不起作用的东西。他们只会让iOS模拟器崩溃。我的代码:

 // 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)
}
}

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:24)

我只是想通了。我不得不做

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

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

沉默警告。然后我将presentViewController更改为showViewController,然后就可以了。这是我完成的课程:

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)
    }
}

答案 1 :(得分:11)

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

LE:低调添加