简体   繁体   English

重写自定义UITableView类方法

[英]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. 我正在使用MWPhotoBrowser在应用程序中显示一些照片,例如,我想更改导航控制器的标题。 With a value I have in my current view controller. 使用我当前的视图控制器中的值。 From the current view controller I modally call MWPhotoBrowser like this 从当前的视图控制器,我像这样模态地调用MWPhotoBrowser

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 所以我进入了该类的实际文件,并且在MWPhotoBrowser.m有一个像这样的函数

- (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 所以我尝试了这样的事情,在我放的updateNavigation下面

-(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? 但这也不起作用,那么如何在MWPhotoBrowser值进行硬编码的情况下从当前的视图控制器中设置MWPhotoBrowser的标题?

Thanks 谢谢

updateNavigation can be called anywhere after you called your updateTitle: method, so thats why it didn't work. 在您调用updateTitle:方法之后,可以在任何地方调用updateNavigation ,这就是为什么它不起作用的原因。 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 : 现在,实现所需目标的最简单方法是创建一个MWPhotoBrowser的简单子类,如下所示:

CustomTitlePhotoBrowser.h 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 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. 我们添加了一个存储的属性customTitle ,以便每当updateNavigation ,我们仍然记得要显示的内容。 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. 我还决定在此处调用[self updateNavigation] -可能不需要,但可以在显示浏览器时更改标题。
  2. We override updateNavigation - this is the clue of all of this. 我们覆盖了updateNavigation这就是所有这些的线索。 When using CustomTitlePhotoBrowser , whenever the original code calls updateNavigation , our overriden implementation will be called instead. 使用CustomTitlePhotoBrowser ,无论原始代码何时调用updateNavigation ,都将调用我们的重写实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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