Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can create a selector for viewWillAppear but not for viewDidLoad

Tags:

ios

swift

I'm not seeing why this will compile:

let viewWillAppearSelector = #selector(UIViewController.viewWillAppear(_:))

But not this:

let viewDidLoadSelector = #selector(UIViewController.viewDidLoad())

The error is

"Use of instance member 'viewDidLoad' on type UIViewController; did you mean to use a value type 'UIViewController' instead?

Why does the latter not complile but the former does?

Update: I changed it to this and now it compiles:

let viewDidLoadSelector = #selector(UIViewController.viewDidLoad)

But I'm not 100% why that is

like image 535
Gruntcakes Avatar asked May 04 '16 18:05

Gruntcakes


People also ask

What is the difference between viewDidLoad and viewWillAppear?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.

Does viewDidLoad get called before viewWillAppear?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

Is viewDidLoad only called once?

viewDidLoad method is called only once in ViewController lifecycle. The reason retrieveMessage() is called in viewDidLoad because it's adding observer to start listening for received and sent message.

Can viewDidLoad be called multiple times?

viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears. In this simple app, that means it is only called once.


1 Answers

If a method does not take any parameters you need to omit the parentheses. In a future version of Swift your code will become an error.

Try

let viewDidLoadSelector = #selector(UIViewController.viewDidLoad)

instead.

like image 137
HAS Avatar answered Oct 09 '22 19:10

HAS