when to use viewDidLoad - ios

I am new to both iOS development and programming in general. I need some clarification as to what sort of things should be declared in the viewDidLoad function of a UIViewController subclass
Thanks

In order to properly understand what viewDidLoad does, you should understand the View Controller Lifecycle. The best point to start is reading the Apple Documentation, e.g. the learning guides for developing iOS Apps: https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html

Declare elements that don't need to be refreshed or recreated when the view reloads. For instance, viewDidLoad is called only when it is created while viewDidAppear will be called every time the view is shown.
Read up on some apple docs.

Everything you write inside the viewDidLoad function will run the the View(which can be TableView, ViewController & more..) is loaded.
For example, if you got a label called 'label' and you want to set it's by the code so you type:
override func viewDidLoad() {
super.viewDidLoad()
label.text = String("any text here")
}
and then the text of the label will change when the View will load.

Related

Add analytics event to log each ViewController's name in the project

I have a project in which I need to log an analytics event whenever any View Controller (log the name of the View Controller) comes on screen.
I was trying to avoid littering all of my existing View Controller classes with call to the analytics SDK.
I tried making an AnalyticsViewController and all my View Controllers would subclass this View Controller, and then I add analytics event in AnalyticsViewController class's viewDidLoad method. But the problem with this approach is that AnalyticsViewController does not which child View Controller is the call coming from.
I am using Swift 3.0. I believe that Swift with its powerful language features should be able provide me with an abstraction of some sorts.
Is there any way through this problem without littering all the View Controllers?
You were on the right track. Making a UIViewController parent class is a good idea.
In viewDidLoad method you can just add this:
let className = NSStringFromClass(self.classForCoder)
It will give you the name of current loaded view controller and then you can use that name in your event to specify which view controller was actually loaded.
Edit: added example.
So your parent's viewDidLoad would look something like this:
override func viewDidLoad() {
super.viewDidLoad()
let className = NSStringFromClass(self.classForCoder)
sendEvent(withViewControllerName: className)
}
The answer given by #JPetric is an amazing starting point. I just had to do a little modification to get it to work.
I've put this in my AnalyticsViewController to retrieve the name of the current subclass.
private func currentClassName() -> String? {
return NSStringFromClass(self.classForCoder).components(separatedBy: ".").last
}

How can I detect a subview being added anywhere in the view hierarchy (including subviews)?

I am creating a framework for the other apps to use it. I want to find when the display presented to the user changes. These changes include addition and removal of subviews, scrolling down, adding text, etc. Is there a way I can directly check when the content presented on the screen is changing. Above question is a part of the problem.
Did you mean viewDidLoad?
That function called first time after all view loaded same as ViewTreeObserver.OnGlobalLayoutListener.
After you explanations I would simply do something like this:
class MyViewController:UIScrollViewDelegate{
func addSubview(){
self.takeSnaphot()
}
func scrollViewDidScroll(scrollView:UIScrollView){
self.takeSnaphot()
}
func takeSnaphot(){
//the code to take snaphots
}
}

Can a Delegate Apply to Two VCs

I think I'm using protocols in a way that shouldn't work... but it does. Can someone tell me if what I'm doing is okay?
Example
Let's say my app has three view controllers: MainView, SecondaryView, EditorView. The EditorView has buttons that should run code in the other views. I have a protocol which allows me to trigger functions remotely.
Here's the weird thing, the thing I'm doing that maybe I shouldn't... but it's working: Both MainView and SecondaryView conform to the same protocol. I don't understand why it works, but it does.
When I am in MainView and I load the EditorView and, for example, click "Add", the code in the MainView runs and works.
When I am in SecondaryView and I load the EditorView and click "Add", the code in the SecondaryView runs and works.
Question
If this is okay, how is it figuring out which (Main vs Secondary) to delegate to? If it's not okay, why is it working?
This is not only okay - it's great. You've discovered one of the primary benefits of protocols.
In EditorView you have a delegate property (you might've named it something else):
weak var delegate : EditorProtocol?
You also have some code somewhere in MainView and SecondaryView that looks like:
let editor = EditorView() // or perhaps you get it in prepareForSegue(_:sender:)
editor.delegate = self
That tells EditorView which object to communicate with.
Then within EditorView you call code like:
delegate?.doSomething()
If MainView created this editor, then delegate is a MainView. Otherwise, it's a SecondaryView.
When you set delegate (or whatever you named it) to a specific object, that's what EditorView will communicate with.

Tableview not reloading after button click

I am working in swift. I have a protocol defined that has a function called reload. I have a home class which is a tableviewcontroller where I extend the protocol and implement the reload function. I have another class where there is a button. On this click of the button I set the delegate and the reload function is called. Until this step it works fine. Now in the reload function i want to refresh the home page, so i tried
tableview.reload()
this is not working and the app is crashing, then i tried calling
viewDidLoad()
this is also not refreshing the page. I dont want to use NSNotification
what am i doing wrong?
Can anybody please help
Thanks in advance
Are you sure your UITableView data source delegate methods are working properly? You should look up viewDidLoad() and viewWillLoad(), etc... in the UIViewController reference page (Apple doc). If you're serious about app development I recommend at the very least making the time to spend 15 - 30 minutes each looking at the UIViewController and UIView pages to familiarize yourself with them, since you'll be using them all the time. You should know basically how they work. Also check out CALayers in UIViews. Then you'll be better established to write really nice apps.
you are setting a delegate on button touchup inside event, please check whether you are setting a delegate to your Home class .
I assume your mean tableview.reloadData() which is the correct method to call for reloading your tableview.
You shall not call viewDidLoad by yourself.

Where to customise IBOutlets

Where should I customise my IBOutlets?
Say I have created a button with interface builder, created an IBOutlet for it and I would want to change a property during runtime (ex: background color or localized title).
I would think of adding it to the viewDidLoad method, but outlets aren't yet created.
I remember having nil outlets in viewDidLoad, but I might be wrong.
If I move it viewWillAppear, the code will be executed every time the view controller's view appears.
Is there any better place for my IBOutlet related code, so it's only executed once?
Obviously I can do just about any customization using only the interface builder and making use of the User defined runtime attributes or localized stroryboards, but I don't like that since it's much more tedious to change later.
From the Doc
Its clearly says about the Views loaded into the memory in the -viewDidLoad() delegate itself.
I would think of adding it to the viewDidLoad method, but outlets
aren't yet created.
It is a false statement, Because you only get the viewDidLoad: message after IBOutlets are created. So you can safely do any customization in viewDidLoad:
Let’s say you have a Button you want to customise. You put the button at the place where you want it to be and then open the “Identity Inspector” on the right.
There is a textfield for “Custom Class”:
I usually create a subclass of UIButton / NSButton (depending on iOS or OSX) and edit the behaviour, drawing methods and functionality in this class file. Then just add the name of this class in this textfield. Voila!

Resources