The category implements a method that is also implemented in the main class: `viewWillAppear:`

I am trying to switch the UIViewController category to viewWillAppear:. But getting this warning.

A category implements a method that is also implemented in a class

@implementation UIViewController (ViewWillAppearCategory)

-(void)viewWillAppear:(BOOL)animated
{
    //.........
}

@end

I want some things to appear on all screens while watching, so I don’t want to touch the whole screen . therefore, go with the category.

I can implement some method in a subclass, and I can call this method in all VCs (full screen). But I do not want this. It is automatically called up on the screen, a call will appear. Is it any idea to do this or make any mistake above?

. . . , . .

+4
5

Method Swizzling, , .

, , .

http://nshipster.com/method-swizzling/

+11

, . , UIViewController, , MyUIViewController, :

-(void) viewWillAppear:(BOOL) animated {
    // do your "category" stuff
}

UIViewControllers MyUIViewController :

-(void) viewWillAppear:(BOOL) animated {
    [super viewWillAppear:animated];
    // rest of code for this class
}
+6

, - :

  • super , [super viewWillAppear:] .
  • Swizzling - , , , , -.

, UIViewController, viewWillAppear:, :

  • . / "".
  • Nibs Storyboards.
  • , .
  • , .

, . , , UINavigationControllerDelegate , .


viewWillAppear documentation:

, , . . , . , super - .

, .

+1

, , . , , UIViewController, viewWillAppear .

//UIViewController+CustomCategory.h
@interface UIViewConctroller (CustomCategory)

- (void)performCustomization;

@end

//UIViewController+CustomCategory.m
@implementation UIViewController (CustomCategory)

- (void)performCustomization {
    // Do custom stuff…
}

@end

//MYViewController.m
#import "UIViewController+CustomCategory.h"

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self performCustomization];
}
0

, . , , , , .

, ,

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

-(void)viewWillAppear:(BOOL)animated
{
    NSLog(@"I get callback here too");
}

#pragma clang diagnostic pop

, , XCode.

0

Source: https://habr.com/ru/post/1534988/


All Articles