xcode – 以编程方式切换视图swift

我正在Xcode 6测试版中尝试苹果的新语言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)
}
}

非常感谢你的帮助。

我只是想出来了我不得不做

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)
    }
}
http://stackoverflow.com/questions/24336581/programmatically-switching-views-swift

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:xcode – 以编程方式切换视图swift