This is my first question (and program in Xcode to be honest) so apologies if I break any formatting rules, etc!
I am having an issue when creating an area calculator (to be used at a later date in a quote calculator) and having looked at every SIGABRT thread I could find, but I have been unable to resolve it.
import UIKit
class ViewController: UIViewController{
#IBOutlet weak var forename: UITextField!
#IBOutlet weak var surname: UITextField!
#IBOutlet weak var email: UITextField!
#IBOutlet weak var widthInput: UITextField!
#IBOutlet weak var lengthInput: UITextField!
#IBOutlet weak var areaOutput: UILabel!
#IBOutlet weak var perimeterOutput: UILabel!
#IBOutlet weak var results: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func calculate(sender: AnyObject) {
let width = NSString(string: widthInput.text).doubleValue
let length = NSString(string: lengthInput.text).doubleValue
// The calculation used to work out the area of a room
var area = width * length
areaOutput.text = "\(area)"
// The calculation used to work out the perimeter of a room
var perimeter = 2 * (length + width)
perimeterOutput.text = "\(perimeter)"
}
Any help would be appreciated.
Thanks,
Marc
Related
[![enter image description here][2]][2]i want to programm an App with two ViewControllers
The first one performs a segue to the second VC.
The Second VC has a Scrollview with different Views.
I did everything with Autolayout not with code
When i run the Application i get always the error "whose view is not in the window hierarchy!" and can't scroll to the bottom. I tried to find some solutions but nothing helped.
Hope someone can help me with the problem.
my first VC runs a normal performSegue("identifier",sender )
my code for the second VC is
import UIKit
class FragenViewController: UIViewController {
var fragen = [Question]()
var highscore = 0
var questionNumber = 0
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var answerButton1: UIButton!
#IBOutlet weak var answerButton2: UIButton!
#IBOutlet weak var answerButton3: UIButton!
#IBOutlet weak var answerButton4: UIButton!
#IBOutlet weak var forwardButton: UIButton!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var currentQuestionLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func answerButton_Tapped(_ sender: UIButton) {
}
}
the rest is with Main Storyboard Drag and drop
The Code from the first VC is
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var highscoreLabel: UILabel!
#IBOutlet weak var startButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Setup Layout
startButton.backgroundColor = UIColor.systemFill
startButton.layer.cornerRadius = 20
startButton.titleLabel?.font = UIFont(name: "Arial", size: 20)
startButton.layer.borderWidth = 2
startButton.tintColor = UIColor.systemBlue
highscoreLabel.font = UIFont(name: "Arial", size: 18)
}
#IBAction func start_Button(_ sender: Any) {
performSegue(withIdentifier: "startGame", sender: sender)
}
}
So I'm trying to program an "Mad Lib" themed app for a class I'm in but I keep getting a
EXC_BAD_INSTRUCTION
error when I hit the submit button. The other students in the class couldn't figure it out either. Please help me!
Here is my firstViewController.swift:
import UIKit
class ViewController: UIViewController
{
#IBOutlet weak var firstTextField: UITextField! //adjective
#IBOutlet weak var secondTextField: UITextField! //male name
#IBOutlet weak var thirdTextField: UITextField! //verb
#IBOutlet weak var fourthTextField: UITextField! //female name
#IBOutlet weak var fifthTextField: UITextField! //adjective
#IBOutlet weak var sixthTextField: UITextField! //athlete
#IBOutlet weak var seventhTextField: UITextField! //food
#IBOutlet weak var eighthTextField: UITextField! //restaurant name
var userInfo = myCustomClass()
override func viewDidLoad()
{
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
userInfo.adjectiveOne = firstTextField.text!
userInfo.maleName = secondTextField.text!
userInfo.verb = thirdTextField.text!
userInfo.femaleName = fourthTextField.text!
userInfo.adjectiveTwo = fifthTextField.text!
userInfo.athlete = sixthTextField.text!
userInfo.food = seventhTextField.text!
userInfo.restaurantName = eighthTextField.text!
let nextView = segue.destination as! secondViewController
nextView.passedObject = userInfo
}
}
Here is my secondViewController.swift:
import UIKit
class secondViewController: UIViewController
{
var passedObject = myCustomClass()
#IBOutlet weak var myFinishedProduct: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
myFinishedProduct.text = "There was once a \(passedObject.adjectiveOne) man /n \(passedObject.maleName). One day while he was \(passedObject.verb) he saw /n \(passedObject.femaleName), a rather \(passedObject.adjectiveTwo) woman. /n She was also a \(passedObject.athlete), and a very good /n one too. The two went to lunch together at \(passedObject.restaurantName) /n and ate some \(passedObject.food). After /n that they never crossed paths again."
}
}
Finally here is my NSOBject called "myCustomClass.swift":
import UIKit
class myCustomClass: NSObject
{
var adjectiveOne = ""
var maleName = ""
var verb = ""
var femaleName = ""
var adjectiveTwo = ""
var athlete = ""
var food = ""
var restaurantName = ""
}
Basically, `whatever the user enters into the eight text fields will be stored in myCustomClass when the submit button is pressed. From there, in the secondViewController it will put the eight inputs into the story and display it on a label.
Any help is appreciated, thank you!
Edit: The "Submit Button" is connected to the secondViewController on my storybook with the purpose of "show".
First View Controller
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var txtmobile: UITextField!
#IBOutlet weak var txtlname: UITextField!
#IBOutlet weak var txtfname: UITextField!
var ArrayStudent:[PassData] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnclick(_ sender: UIButton)
{
let objdata = PassData(fname: txtfname.text!, lname: txtlname.text!, mobile: txtmobile.text!)
ArrayStudent.append(objdata)
passdata()
}
func passdata()
{
let objstory = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
objstory.Arradata1 = ArrayStudent
_ = self.navigationController?.pushViewController(objstory, animated: true)
}
}
Second View Controller
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var lblmobile: UILabel!
#IBOutlet weak var lbllastname: UILabel!
#IBOutlet weak var lblname: UILabel!
var Arradata1:[PassData ] = []
override func viewDidLoad() {
super.viewDidLoad()
lblname.text = Arradata1.first?.StrFirstName
lbllastname.text = Arradata1.first?.StrLastName
lblmobile.text = Arradata1.first?.StrMobile
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
NSObject Class
import UIKit
class PassData: NSObject
{
var StrFirstName:String!
var StrLastName:String!
var StrMobile:String!
init(fname:String , lname:String , mobile:String)
{
StrMobile = mobile
StrFirstName = fname
StrLastName = lname
}
}
I'm a newbie to swift and for now it's a pain in the a$$ to code.
I'm trying to make a very simple app where the user enters a number to a textfield
and then when a button is clicked it shows the number + 1 in a label.
I always get this error:
Thread 1:EXC_BREAKPOINT (code=1, subcode=0x1002c11ec)
This is my code:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tav: UITextField!
#IBOutlet weak var fogy: UITextField!
#IBOutlet weak var ar: UITextField!
#IBOutlet weak var eredmeny: UILabel!
#IBOutlet weak var szamolasBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func szamol(_ sender: Any) {
let number = Int(tav.text!)! + 1
eredmeny.text = String(describing: number)
}
}
I ran your code and it works fine if an integer in entered in the textfield. But if you enter any character other than a string then the app crashes because Int(tav.text!)! returns nil and adding 1 to nil. Check for nil first and then add something to it.
I have made a small program which I use to count the money in the safe at work, but after updating my iPhone, it didn't work anymore.
After a lot of reading and so on I fixed everything, but I haven't found a new way to resign the keyboard.
Before the update I used this:
#IBAction func resignKeyboard(_sender: AnyObject) {
_sender.resignFirstResponder()
}
This doesn't work anymore so I would love a new way of doing it. I have searched quite a bit for a solution, but I haven't understood any of the solutions I have found. So please simplify your answers as a lot as possible.
This is everything from the "ViewController.Swift" from my program:
//
// ViewController.swift
// Pengeskabstæller
//
// Created by Alex on 09/07/2016.
// Copyright © 2016 Alex. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var In50B: UITextField!
#IBOutlet weak var In50L: UITextField!
#IBOutlet weak var In20B: UITextField!
#IBOutlet weak var In20L: UITextField!
#IBOutlet weak var In10B: UITextField!
#IBOutlet weak var In10L: UITextField!
#IBOutlet weak var In5B: UITextField!
#IBOutlet weak var In5L: UITextField!
#IBOutlet weak var In2B: UITextField!
#IBOutlet weak var In2L: UITextField!
#IBOutlet weak var In1B: UITextField!
#IBOutlet weak var In1L: UITextField!
#IBOutlet weak var In05B: UITextField!
#IBOutlet weak var In05L: UITextField!
#IBOutlet weak var Ialt: UILabel!
#IBAction func resignKeyboard(_sender: AnyObject) {
_sender.resignFirstResponder()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func Knap(sender: AnyObject) {
//Hvad knappen gør
//Ganger input ved værdi af bundt eller løse
var x50B: Int = Int(In50B.text!)!
x50B = x50B*5000
var x50L:Int = Int(In50L.text!)!
x50L = x50L*500
var x20B:Int = Int(In20B.text!)!
x20B = x20B*4000
var x20L:Int = Int(In20L.text!)!
x20L = x20L*400
var x10B:Int = Int(In10B.text!)!
x10B = x10B*2000
var x10L:Int = Int(In10L.text!)!
x10L = x10L*200
var x5B:Int = Int(In5B.text!)!
x5B = x5B*1000
var x5L:Int = Int(In5L.text!)!
x5L = x5L*200
var x2B:Int = Int(In2B.text!)!
x2B = x2B*500
var x2L:Int = Int(In2L.text!)!
x2L = x2L*50
var x1B:Int = Int(In1B.text!)!
x1B = x1B*500
var x1L:Int = Int(In1L.text!)!
x1L = x1L*50
var x05B:Int = Int(In05B.text!)!
x05B = x05B*200
var x05L:Int = Int(In05L.text!)!
x05L = x05L*20
//Lægger det hele sammen
let penge1 = (x50B + x50L + x20B)
let penge2 = (x20L + x10B + x10L)
let penge3 = (x5B + x5L + x2B + x2L)
let penge4 = (x1B + x1L + x05B + x05L)
let penge99 = String(penge1+penge2+penge3+penge4)
//Printer ialt
Ialt.text = penge99
}
}
I think this is a better way to do it, instead of adding an outlet.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
Hope it helps!
Assuming all textFields are subviews of view, you can use endEditing method like this:
#IBAction func resignKeyboard() {
view.endEditing(true)
}
Try to change the sender to UITextField instead of AnyObject and then you can do sender.resignFirstResponder()
I am trying to program a simple app in Xcode using Swift.
I have two labels which contain INT values which are incremented up/down by the user using Steppers as per below:
class ViewController: UIViewController {
#IBOutlet weak var valueLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBAction func stepperValueChanged(sender: UIStepper) {
valueLabel.text = Int(sender.value).description
}
#IBOutlet weak var valueLabel2: UILabel!
#IBOutlet weak var stepper2: UIStepper!
#IBAction func stepperValueChanged2(sender: UIStepper) {
valueLabel2.text = Int(sender.value).description
}
I want to then have another Label (label3) which takes the INTs from label1 & label2 and divides them.
So for example if the user has Label1 as '5' and label2 as '10' I want label3 to display '0.5'.
Is there an easy way that I can do this?
As I'm sure you can tell, I'm really new to iOS development so any help or advice would be greatly appreciated!
Cheers!
Although this is a simple program you should still use best practice and adopt a model-view-controller design. Your model (data) should be separate from view (in essence the way the data is displayed). In a more 'serious' application the model would typically be a separate class, but in this case you can use local properties.
Storing data in Int properties also means you won't have to convert back from strings when you want to perform calculations.
class ViewController: UIViewController {
#IBOutlet weak var valueLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBOutlet weak var valueLabel2: UILabel!
#IBOutlet weak var stepper2: UIStepper!
#IBOutlet weak var valueLabel3: UILabel!
var value1=0;
var value2=0;
#IBAction func stepperValueChanged(sender: UIStepper) {
self.value1=Int(sender.value);
self.valueLabel1.text="\(self.value1)";
self.updateResult();
}
#IBAction func stepperValueChanged2(sender: UIStepper) {
self.value2=Int(sender.value);
self.valueLabel1.text="\(self.value1)";
self.updateResult();
}
func updateResult {
if (self.value2 != 0) {
var result=Float(self.value1)/Float(self.value2)
self.valueLabel3.text="\(result)"
} else {
self.valueLabel3.text="Can't divide by 0"
}
}
}