以编程方式快速切换视图

Posted

技术标签:

【中文标题】以编程方式快速切换视图【英文标题】:Programmatically switching views swift 【发布时间】:2014-06-20 22:30:25 【问题描述】:

我正在 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)


非常感谢您的所有帮助。

【问题讨论】:

【参考方案1】:

我刚刚想通了。我不得不做

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)
    

【讨论】:

【参考方案2】:
let vc = self.storyboard.instantiateViewControllerWithIdentifier("billInfo") as! BillInfoViewController
self.presentViewController(vc, animated: true, completion: nil)

LE:添加了向下转换

【讨论】:

以上是关于以编程方式快速切换视图的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式切换 MVC 视图控制器 - iOS

如何以编程方式快速切换到暗模式

在 Swift 中以编程方式切换视图

以编程方式从模态视图控制器切换父选项卡

以编程方式切换自定义视图(推送和弹出)

以编程方式切换子视图控制器:XLPagerTabStrip