Xcode 以编程方式切换视图
我正在Xcode 6测试版中试用苹果的新语言swift。当按下表视图单元格时,我试图以编程方式切换视图,尽管它所做的只是拉出一个空白的黑屏。在我的代码中,我注释掉了不起作用的东西。他们只会让iOS模拟器崩溃。我的代码:Xcode 以编程方式切换视图,xcode,view,swift,xcode6,Xcode,View,Swift,Xcode6,我正在Xcode 6测试版中试用苹果的新语言swift。当按下表视图单元格时,我试图以编程方式切换视图,尽管它所做的只是拉出一个空白的黑屏。在我的代码中,我注释掉了不起作用的东西。他们只会让iOS模拟器崩溃。我的代码: // ViewController.swift // Step-By-Step Tip Calculator // Created by Dani Smith on 6/17/14. // Copyright (c) 2014 Dani Smith Product
// 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)
}
}
乐视:悲观情绪增加了
let vc = self.storyboard.instantiateViewControllerWithIdentifier("billInfo") as! BillInfoViewController
self.presentViewController(vc, animated: true, completion: nil)