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.
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.
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.
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];
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.
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.
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?
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!
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)
}
}
let vc = self.storyboard.instantiateViewControllerWithIdentifier("billInfo") as! BillInfoViewController
self.presentViewController(vc, animated: true, completion: nil)
LE: downcast added
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With