This is the crash log Every time I click on the button that is a part of the simulator, I keep getting the error EXC_BAD_ACCESS. This code is the part of the calendar from the JTAppleCalendar.
import UIKit
import JTAppleCalendar
class EventsViewController: UIViewController {
#IBOutlet weak var calendarView: JTAppleCalendarView!
let formatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
setupCalendarView()
}
func setupCalendarView(){
calendarView.minimumLineSpacing = 0
calendarView.minimumInteritemSpacing = 0
}
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Check if calendarView is connected in the Storyboard in Referencing Outlets.
Related
I am new to programming, but I am interested in how to use iosMath for iOS. I could instal Cocoa Pod already and I did import iosMath to project. Question is: how to visualise math equations?
I understand that it should be used MTMathUILabel for that, however I do not know, how to add it to program. Is there a way how to create subclass of UIView or something, to be able able to do it?
Here sample of my code:
import UIKit
import Foundation
import CoreGraphics
import QuartzCore
import CoreText
import iosMath
class ViewController: UIViewController {
#IBOutlet weak var label: MTMathUILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let label: MTMathUILabel = MTMathUILabel()
label.latex = "x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}"
label.sizeToFit()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I tried to connect label to UIView() and UILabel() in my Storyboard, but obviously thats not how it works.
Thank you in advance for any help.
A few problems in your posted code
You are setting an IBOutlet then instantiating another MTMathUILabel with the same name
You don't really need to call label.sizeToFit()
Simple solution is to remove the IBOutlet, and do as follows
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let label: MTMathUILabel = MTMathUILabel()
label.latex = "x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}"
//ADD THIS LABE TO THE VIEW HEIRARCHY
view.addSubview(label)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Better solution is as follows:
Create a UIView in storyboard (because MTMathUILabel is actually a UIView)
Set this view's class to MTMathUILabel
Connect the IBOutlet for this view
then use the following code
class ViewController: UIViewController {
#IBOutlet weak var label: MTMathUILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//NO NEED TO INSTANTIATE A NEW INSTANCE HERE
label.latex = "x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}"
//NO NEED TO CALL sizeToFit()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I wanna get object of AppDelegate.
This program can build but it will stop running with lldb error.
Maybe the problem is the dirrerence of Swift2.0 and 3.0.
My textbook is for swift2.0 but I am using xcode8.0 and Swift3.0.
Error is here.
let ap = UIApplication.shared.delegate as! AppDelegate
I used this page for fixing.
How do I get a reference to the app delegate in Swift?
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var dataTextField: UITextField!
let ap = UIApplication.shared.delegate as! AppDelegate
override func viewWillAppear(_ animated: Bool) {
dataTextField.text = String(ap.cmValue)
}
#IBAction func tapInpu() {
dataTextField.resignFirstResponder()
if let text = dataTextField.text{
if let cmValue = Double(text){
ap.cmValue = cmValue
}
}
}
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.
}
}
If you remove ap initialization and ap.cmValue = cmValue, does it works ?
If it does work check your outlet referencing in your storyboard you may have old non existent references
Good morning!
I have an "unable to load" problem in my iOS widget. I've read a lot of about the "unable to load" message but nothing fixed my problem. I'm not sure but I think my problem is to refresh the widget after changing my content.
My widget has one button and one label. If the user press the button the text from the label will changed - in this moment the widget shows "unable to load". Just a milisecond after pressing the button.
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
#IBOutlet var segment_att: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
completionHandler(NCUpdateResult.NewData)
}
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
return UIEdgeInsetsZero
}
#IBAction func button_ae(sender: AnyObject) {
let tableviewclass = TodayTableViewController()
tableviewclass.newData()
}
}
Important is that the label is shown in a TableViewCell of a TableViewController. So the TableViewController is embeded in the ViewController within a Container... The listener from the button call the method newdata() of the file of the TableViewController.
import UIKit
import NotificationCenter
class TodayTableViewController: UITableViewController, NCWidgetProviding {
#IBOutlet var table: UITableView!
#IBOutlet var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
init()
}
func init() {
let meldung: String = "test"
label1.text = meldung
}
func newData() {
let meldung: String = "new test"
label1.text = meldung
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
The code is really simple and basic - so I'm wondering about the problem in this simple mechanism. I hope you can help me!
Thanks at all!
Your code assumes that label1 has been set when newData() is called, even immediately after the constructor is called.
Try using this optional chaining syntax instead, which will quietly fail if the property is nil:
import UIKit
import NotificationCenter
class TodayTableViewController: UITableViewController, NCWidgetProviding {
#IBOutlet var table: UITableView!
#IBOutlet var label1: UILabel!
var meldung: String = "test" // <-- meldung is property
override func viewDidLoad() {
super.viewDidLoad()
init()
}
func init() {
label1?.text = melding // <-- optional chaining
}
func newData() {
melding = "new test" // <-- set the class property
label1?.text = meldung // <-- optional chaining
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
and instead of calling newData(), you might instead just set the meldung property, e.g.:
tableviewclass.meldung = "new test"
as your viewDidLoad() will take care of setting the UILabel text from the property
So i am making an iOS app, that lets you to enter text into a TextBox and then there is a Button that will display the "Text" on a label, and that "Text" on the label will be Displayed in the Notification Center, on a widget, But something is going wrong Because every time i run the app it fails and throws me into the AppDelegate.Swift and highlights me this specific part: "class AppDelegate: UIResponder, UIApplicationDelegate {"
with an error that says "Thread 1: signal SIGABRT"
i Don't know what else to do ;( please help!!
This is the ViewController.Swift code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var UserInput: UITextField!
#IBOutlet var TextDisplay: UILabel!
#IBAction func DisplayButton(sender: AnyObject) {
let Text:String = UserInput.text!
if let Text = Int(Text) {
print("Computer says no!")
} else {
TextDisplay.text = (Text)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let SharedDefaults = NSUserDefaults(suiteName: "group.St33v3n.TextOnWidget")
SharedDefaults?.setObject(UserInput, forKey: "StringKey")
SharedDefaults?.synchronize()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
and Here is the TodayView controller code (the one from the widget):
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
#IBOutlet var TextToWidget: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view from its nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
let ShareDefaults = NSUserDefaults(suiteName:"group.St33v3n.TextOnWidget")
self.TextToWidget.text = ShareDefaults?.objectForKey("StringKey") as? String
completionHandler(NCUpdateResult.NewData)
}
}
In the line
SharedDefaults?.setObject(UserInput, forKey: "StringKey")
you are passing UITextField object, not the text. You should use
SharedDefaults?.setObject(UserInput.text, forKey: "StringKey")
I'm not sure if that is causing the crash, but it needs correction anyway ;)
This is my UIViewController:
class HabitTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet weak var habitTableView: UITableView!
private var _numOfRowsInSects: [Int] = []
private var _habitsByDaySet = false
private var _habitsBySection:[[Habit]] = []
private var _whatIsToday = -1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
habitTableView.delegate = self
habitTableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func reloadTableForDay(sender: DayButton){
if sender.tag != getDayOfWeek(-1){
_whatIsToday = sender.tag
self.habitTableView.reloadData() //line of exception
}
else{
_whatIsToday = -1
}
}
// methods of UITableView delegate and data source implemented
}
I got an NSInvalidArgumentException at the line self.habitTableView.reloadData(), and the reason is -[HabitWiz.HabitTableViewController reloadTableForDay:]: unrecognized selector sent to instance 0x7fb0d351b060. I have no idea what is going on here, any help is appreciated.
Solved it. I realized my app wasn't building correctly: I inserted a couple of print statements which weren't executed. So I just did a clean build, and everything works now.
My guess is that you messed up with the IBAction in the interface builder.
Try to unwire and re-wire the action and be sure the ViewController is set to be a subclass of HabitTableViewController.