Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically switching views swift

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

Thank you so much for all of your help.

like image 998
user3761579 Avatar asked Jun 20 '14 22:06

user3761579


People also ask

How do I move from one Viewcontroller to another in Swift 4 programmatically?

Ctrl+Drag from the “View Controller” button, to somewhere in the second View Controller(HomeViewController). It can be anywhere in the main box of the second view controller. When you release, it will show you a box like the one below. in this you don't need any code to switch, it will switch on click of a button.

How do I switch between view controllers?

From the Library, add a button to the first View Controller and name the button, for example: Show Second View . Select the button, press and hold the Control key on the keyboard and drag from the button to the second View Controller.

How do you call a view controller in storyboard programmatically?

NSString * storyboardName = @"MainStoryboard"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"]; [self presentViewController:vc animated:YES completion:nil];

What is a programmatic navigation controller in Swift?

Swift Swift: Programmatic Navigation View Controllers in Swift. Navigation controllers are the workhorse of organizing view controllers. I’ve covered much of their use in other posts about MVC, segues and delegates. In this chapter, We’ll go through some of the Swift code for the Navigation controller.

How do I create a single view project in Swift?

Start a new single view project in Swift called SwiftProgNavControllerDemo . Go into the storyboard and select the blank view controller. Be sure to select the controller and not the view.

What is auto layout in Swift?

Auto Layout in Swift: Writing constraints programmatically. Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions. Can you imagine a world without Auto Layout?

How to add a switch to the content view?

Add a switch to the content view as in the illustration, Hook up an outlet by control dragging from the switch to the ViewController class in the assistant editor. @IBOutlet weak var fourFiveSwitch: UISwitch!


2 Answers

I just figured it out. I had to make

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

to be

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

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)
    }
}
like image 102
user3761579 Avatar answered Oct 23 '22 00:10

user3761579


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

LE: downcast added

like image 37
Andrew Avatar answered Oct 22 '22 22:10

Andrew