以编程方式快速切换视图 [英] Programmatically switching views swift

查看:19
本文介绍了以编程方式快速切换视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

非常感谢您的帮助.

推荐答案

我刚刚想通了.我必须做

I just figured it out. I had to make

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

成为

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

使警告静音.然后我将 presentViewController 更改为 showViewController,并且它起作用了.这是我完成的课程:

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

这篇关于以编程方式快速切换视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆