steipete / Aspects

Delightful, simple library for aspect oriented programming in Objective-C and Swift.
https://twitter.com/steipete
MIT License
8.4k stars 1.26k forks source link

why can't hook viewDidLoad (UIViewController) ?? #73

Open wangqianjun opened 8 years ago

wangqianjun commented 8 years ago

Hi Steipete, why can't hook viewDidLoad (UIViewController) ??
Error: Aspects: Blog signature <NSMethodSignature: 0x7fbc8bd35740> doesn't match <NSMethodSignature: 0x7fbc8bf0cc40>.

Please help~

Thank u.

steipete commented 8 years ago

Hi, if you post some code I can help.

wangqianjun commented 8 years ago

Steipete, I create a Category for UIViewcontroller, and have a Class method viewDidloadLogging:

In my demo AppDelegate,

There are no problems using?

hustlzp commented 8 years ago

+1

steipete commented 8 years ago

+1's don't help. Somebody who digs in and figures out what fails would help though. Thanks!

wangqianjun commented 8 years ago

So that question answer is what,Steipete?^.^

xiaochaoren commented 8 years ago

This is the problem of execution order of [UIViewController viewDidLoad] and [UIViewController viewDidloadLogging]. Please make sure [UIViewController viewDidloadLogging] is invoked before [UIViewController viewDidLoad].

Freeway1979 commented 7 years ago

I got the same issue while hooking viewDidLoad selector of UIViewController class. I can't call the block I provided but "dealloc" is working.

Can you figure out the solution?

Thanks a lot.

The code as following:

Freeway1979 commented 7 years ago

Hi everyone, I made it. The root cause is that "aspect_isCompatibleBlockSignature" verifies that argument count of blockSignature and methodSignature,and if not matched ,nil returned. You can see these code : if (blockSignature.numberOfArguments > methodSignature.numberOfArguments) { signaturesMatch = NO; //You will go here if wrong block parameters provided as above. }else {

Now ,we can correct it by changing [UIViewController aspect_hookSelector:@selector(viewDidLoad) withOptions:AspectPositionBefore usingBlock:^(id info, BOOL animated) { to [UIViewController aspect_hookSelector:@selector(viewDidLoad) withOptions:AspectPositionBefore usingBlock:^(id info) {

It works now for "viewDidLoad".

Hi @Steipete, I suggest to throw an exception here for developers to correct it to right things.

Thanks.

ApesTalk commented 5 years ago

How can i use AspectPositionInstead option replace viewDidLoad method? Any help would be appreciated.

[MySubViewController aspect_hookSelector:@selector(viewDidLoad) withOptions:AspectPositionInstead usingBlock:^(id info){ //How can i use super here ??? // [super viewDidLoad]; WTSubViewController vc = (WTSubViewController )info.instance; vc.view.backgroundColor = [UIColor blueColor]; } error:NULL];