I have an application using Swift3 and Eureka Forms 2.0.0. I really love Eureka forms, but my app is leaking memory like crazy and I'm trying to pick it apart to see what's wrong. On the most basic level, I have a custom view controller that contains a table view (I have some other elements around the form that I need to control separately) that I want to tie into a Eureka form. However, even with the most basic possible case, I'm still seeing memory leaks. Here's the view controller I'm testing with:
class TestViewController: FormViewController {
#IBOutlet weak var formTableView: UITableView!
#IBOutlet var mainText: UILabel!
var titleString : String?
override func viewDidLoad() {
NSLog("viewDidLoad")
super.tableView = formTableView
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Just loading this and moving back and forth to another view results in the following leak report in Instruments:
Any advice on this would be much appreciated.
Thanks,
Alex
Related
I understand it’s rather basic, but I’m only trying to get a grasp on basic functions.
I have produced some code by partially my own knowledge and partial bits from different guides.
I am not getting any errors, but the label is not displaying itself as “Text”. I believe it’s to do with the order/place my code is put.
Please help explain how I can fix this!
Please note as well:
I have just a single label called myLabel (named under the document section of my the identity inspector
It is has the text “Loaded” put into it already when I put it in.
I have no other code anywhere, only the default new project code.
I renamed the ViewController to ViewManager to avoid a class error.
First image: This is the image just so you know the location and other bits. I’ll attach the code too:
Second image: What I get, with no errors:
Third image: My main storyboard file:
And now it in code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
#IBAction func labelSet() {
myLabel.text = "Text"
}
}
Make sure that the IBAction is connected to Touch Up Inside in Interface Builder.
Change the signature of the IBAction to
#IBAction func labelSet(_ sender: UIButton) {
Your function func labelSet() isn't called anywhere. Neither in the Storyboard nor elsewhere.
You can call it in viewDidLoad() like this:
override func viewDidLoad() {
super.viewDidLoad()
labelSet()
}
Alternatively call it after the label has loaded.
#IBOutlet weak var myLabel: UILabel! {
didSet {
labelSet()
}
}
I have to set up a project that has to contain several targets. The goal is to have a code base to share it with all the targets that will be created later. I make an example : I will have a UIViewController for the login, but this will be different for every target. My idea is to have a code base to share with every specific LoginViewController. What is the best approach to structure the code? I started writing a protocol, here it is :
protocol LoginProtocol {
weak var txtUsername: UITextField? { get set }
weak var txtPassword: UITextField? { get set }
weak var btnLogin: UIButton? { get set }
func login()
}
The idea is to implement this protocol in every controller.
The other approach is to write a sort of base view controller that every other controller will inherit from. Like this :
// MARK: - Instance vars and IBOutlets
class LoginViewController: UIViewController {
#IBOutlet weak var txtUsername: UITextField?
#IBOutlet weak var txtPassword: UITextField?
#IBOutlet weak var btnLogin: UIButton?
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension LoginViewController {
func login() {
print("Hello")
}
}
What do you think? What is the best choice, architecturally?
Tough to "answer" this since it is to some extent opinion...but here's my input:
I think it boils down to common behavior: If you use a protocol, but then feel like you are writing almost the same code every time you implement it...then you probably want a base class. I love protocols and use them heavily, but I think people are too quick to dismiss base classes these days. (There ARE pros and cons, but so many people blindly make protocols without thinking about it.)
I will sometimes use a hybrid of a base class that has a delegate to "configure" certain parts.
Delegates an important concept in ObjC/SWIFT or any other coding language. I know that delegates are used to pass messages from one class to another class especially when we want to pass message back to a view controller from where we have just moved to some other view controller.
I was searching for more technical answer and searched a lot about this, and here is what I got what I feel might be the exact answer -
By the rules of MVC, we need a method to return a value. Where in a
called instance can we go back to the class calling it? With an
encapsulated class we can’t. There is no way to send that revised
model back to the original controller without breaking encapsulation
or MVC. The new view controller does not know anything about the class
that called it. We look stuck. If we try to make a reference directly
to the calling controller, we may cause a reference loop that will
kill our memory. Simply put, we can’t send things backwards.
But the explanation says something about
Where in a
called instance can we go back to the class calling it? With an
encapsulated class we can’t. There is no way to send that revised
model back to the original controller without breaking encapsulation
or MVC.
So exactly what does this para mean. Can any one please explain this in a more simple way taking the following code as reference -
VC2 -
import UIKit
protocol myDelegate : class
{
func sendItems(name:NSString)
}
class EnterViewController: UIViewController
{
weak var delegate: myDelegate?
#IBOutlet weak var nameTextfield: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func sendData(sender: AnyObject)
{
delegate?.sendItems(nameTextfield.text!)
self.navigationController?.popViewControllerAnimated(true)
}
VC2
import UIKit
class DisplayViewController: UIViewController,myDelegate
{
#IBOutlet weak var nameLabel: 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.
}
func sendItems(name: NSString) {
self.nameLabel.text = name as String
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let destinationVC = segue.destinationViewController as! EnterViewController
if segue.identifier == "enterDetail"
{
destinationVC.delegate = self
}
}
}
Thanks.
In simple terms, Delegate is a representative of Class which works on behalf of class. If we compare in real world, delegates to foreign countries go to represent their government and have all controls and powers. Similarly, here delegates has all the control that an object of class will have and working on behalf of Class.
Now delegate in specific is an object assigned by class to notify the event. This can be acheived by NSNotification too. But the difference is Delegates can intercept the event but NSNotification cant.
Here in your code:
You have assigned DestinationVC's delegate to DisplayViewController.
Now DisplayviewController will notify all the event whatever you want to notify to Class DestinationVC and also intercept in between event.
In your case you are calling sendItems of DestinationVC.
I am sorry for my bad explanation but I guess you would have get the basic idea.
I am making an app and one of the features is that you take a quiz with some options that ping a web API and show these results. The results pop up on a table view and i have the self.tableview.reload() under the viewdidload() but I still have to navigate back to the previous view controller and hit the results button again to get the proper results to show. How do i make it so the tableview refreshes when it gets called? My second error is the exc_bad_acces (code 1, address: 0x0) and i cannot seem to get it to go away. It happens when the you click on a result in the table view and it takes you to a view controller where a summary of the car is presented. Here is the code used in the file. http://i.imgur.com/FER1VMN.png and http://i.imgur.com/jj2xAvR.png. I can provide more if needed, i hope someone can help!
#adrianB does this help?
the console output says this
class DetailViewController: UIViewController {
var trim : Trims?
#IBOutlet weak var summaryLabel: UILabel!
#IBAction func showSummary(sender: AnyObject) {
summaryLabel.text = trim?.summary (Thread 1: EXC_BAD_ACCESS (code =1, address = 0x0)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}`
I'm new to Swift (and programming in general). I'm having some issues getting a Text View to scroll.
I've created outlets for the scroll view and the text view already.
I've found a boat load of people asking the same question on Stack Overflow/Reddit but I am still unable to fix the issue due to just not really knowing what I'm doing.
Here is a screenshot of my view controller so you can see the text I am referring to:
And here is the code I have in my viewcontroller.swift file:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var descriptionTextView: UITextView!
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.scrollView.layoutIfNeeded()
self.scrollView.contentSize = self.descriptionTextView.bounds.size
}
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.
}
}
I'm getting the following green error message when I build:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
I would love some help from anyone if you are feeling kind or have the time to chime in.
Call me crazy, but would it be silly to believe that it is just overwhelmingly difficult to achieve such a simple thing as scrollable text? I feel like I'm missing something.
Thanks to anyone who may be able to lend a hand.