简体   繁体   中英

Override Custom UITableView Class Method

I am using MWPhotoBrowser to display some photos in an app and I would like for example to change the title of the navigation controller. With a value I have in my current view controller. From the current view controller I modally call MWPhotoBrowser like this

MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
//Tried this below didn't work
browser.title = @"MY NEW TITLE";
 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
    nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:nc animated:YES completion:nil];

So I went into the actually files of this class and in MWPhotoBrowser.m there is a function like this

- (void)updateNavigation {
     NSUInteger numberOfPhotos = [self numberOfPhotos];
    self.title = [NSString stringWithFormat:@"%lu Photots", photosText];
}

And I could just change it manually (hard code a value) there but I want the value to change depending on my original view controller, so I want to pass it a value to be able to set it on my original view controller. So I tried something like this, below updateNavigation I put

-(void)updateTitle:(NSString*)title {
   self.title = title;
}

And then in my original view controller I tried

MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
[browser updateTitle:@"TEST TITLE"];

But this didn't work either, so how can I set the title of the MWPhotoBrowser form my current view controller, without hard coding a value?

Thanks

updateNavigation can be called anywhere after you called your updateTitle: method, so thats why it didn't work. Also, changing/adding something to external libraries is a bad idea, because when you udpate them you have to remember to apply all your changes again.

Now the easiest way to achieve what you want would be to create a simple subclass of MWPhotoBrowser , something like this :

CustomTitlePhotoBrowser.h

#import <MWPhotoBrowser/MWPhotoBrowser.h> //I'm not sure if this is a correct import, change accordingly

@interface CustomTitlePhotoBrowser : MWPhotoBrowser

-(void)updateTitle:(NSString *)title;

@end

CustomTitlePhotoBrowser.m

#import "CustomBrowser.h"

@interface CustomTitlePhotoBrowser()

@property(nonatomic, copy) NSString *customTitle;

@end

@implementation CustomTitlePhotoBrowser

-(void)updateTitle:(NSString *)title {

    self.customTitle = title;
    [self updateNavigation];
}

-(void)updateNavigation {
    self.title = self.customTitle;
}

@end

And to use it :

CustomTitlePhotoBrowser *browser = [[CustomTitlePhotoBrowser alloc] initWithDelegate:self];
[browser updateTitle:@"TEST TITLE"];

There are two things added in the subclass :

  1. We add a stored property customTitle , so that whenever updateNavigation is called, we still remember what we want to show. I also decided to call [self updateNavigation] here - this may not be needed, but gives you a possibility to change the title while the browser is shown.
  2. We override updateNavigation - this is the clue of all of this. When using CustomTitlePhotoBrowser , whenever the original code calls updateNavigation , our overriden implementation will be called instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM