This is my second view called detail view and I am bringing over text from the first view into the detail view. Some of the text is larger than others, so I am trying to make it to where my textview changes automatically to adapt to the amount of text needed. I am still new to Swift so any help is appreciated.
import UIKit
class DetailViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var symptomsTextView: UITextView!
#IBOutlet weak var stepsTextView: UITextView!
#IBOutlet weak var emergencyLabel: UILabel!
var medicalObject : MedicalInfo? = nil
var emergencyText = ""
var symptomsText = ""
var stepsText = ""
//Function to show text in labels and text views
override func viewWillAppear(animated: Bool) {
//Adding text to the labels and text views
emergencyLabel.text = emergencyText
symptomsTextView.text = symptomsText
stepsTextView.text = stepsText
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Related
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 am trying to show an spinning activity indicator for my app when the user taps on a button before they are presented with the next view - it kind of works, I see it for a split second
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewWillAppear(animated: Bool) {
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Refactor your code. You don't need to stop the activity indicator in viewWillAppear. You can hide it in viewDidLoad. Here's the code:
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
myActivityIndicator.hidden = true
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Make sure that the IBOutlet for the activity indicator and IBAction for getting events are correctly connected in Interface Builder.
I changed it and it doesn't work, until I go back in the navigation
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
viewDidAppear is probably a more appropriate place to stop the animation and hide the spinner.
import UIKit
class DetailsViewController: UIViewController, UIScrollViewDelegate {
// titolo dell'annotazione selezionata proveniente dalla ViewController
var titoloStringa = ""
/*
// riferimento alla scrollView delle immagini
#IBOutlet var scrollView: UIScrollView!
// riferimento al page control relativo alle immagini
#IBOutlet var pageControl: UIPageControl!
// array che conterrĂ le immagini
var pageImages: [UIImage] = []
// array di optionals che conterrĂ istanze di UIImageView per mostrare ogni immagine nella sua relativa pagina
var pageViews: [UIImageView?] = []
*/
// riferimento alla label titolo
#IBOutlet weak var titolo: UILabel!
#IBOutlet weak var scroolView: UIScrollView!
#IBOutlet weak var labelTesto: UILabel!
#IBOutlet weak var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews(){
super.viewDidLayoutSubviews()
self.labelTesto.numberOfLines = 0
self.labelTesto.sizeToFit() }
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.*/
}
Why doesn't the scroll vertical work? I will implement after the scrolling of images with control page. how can i do it?
You need to make sure your UIScrollView height not more than ViewController height.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scroolView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
scroolView.contentSize = CGSizeMake(400, 2300)
scroolView.showsHorizontalScrollIndicator = true
scroolView.scrollEnabled = true
}
}
How do I make an array of UILabel in Swift. When I try to, I get an error like ViewController.Type'dose not have a member named 'Lable00'
my code:
import UIKit
class ViewController: UIViewController {
let individualScores = [75, 43, 103, 87, 12]
#IBOutlet var Lable00: UILabel?
#IBOutlet var Lable01: UILabel?
#IBOutlet var Lable02: UILabel?
#IBOutlet var Lable03: UILabel?
#IBOutlet var Lable04: UILabel?
var Lable_Arr = [Lable00, Lable01, Lable02, Lable03, Lable04]
override func viewDidLoad() {
super.viewDidLoad()
for score in individualScores {
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
private var labels:[UILabel]()
override func viewDidLoad()
{
super.viewDidLoad()
labels.append(Lable00)
labels.append(Lable01)
labels.append(Lable02)
labels.append(Lable03)
labels.append(Lable04)
// do stuff with the labels view
}
for Swift 2 :
var labels:[UILabel]=[]
In ViewDid load you don't have to append, you can just assign a new array to labels like so:
var labels:[UILabel]()
override func viewDidLoad() {
super.viewDidLoad()
labels = [label01, label02, label03, label04]
// do stuff with the labels view
}