|
3 | 3 |
|
4 | 4 | //Add Settings button
|
5 | 5 | @interface MainFeedViewController : UIViewController
|
| 6 | +@property (nonatomic, strong) UIBarButtonItem *settingsButton; |
| 7 | +- (void)addSettingsButton; |
6 | 8 | @end
|
7 | 9 |
|
8 | 10 | %hook MainFeedViewController
|
| 11 | +%property (nonatomic, strong) UIBarButtonItem *settingsButton; |
| 12 | + |
9 | 13 | - (void)viewDidLoad{
|
10 | 14 | %orig;
|
11 |
| - @try{ |
12 |
| - JELog(@"Adding setting button"); |
13 |
| - MainFeedViewController *usableSelf = self; |
14 |
| - UIBarButtonItem *statsButton = [[UIBarButtonItem alloc] initWithTitle:[[JDESettingsManager sharedInstance] localizedStringForKey:@"emproved"] |
15 |
| - style:UIBarButtonItemStyleDone target:self |
16 |
| - action:@selector(presentJDEViewController:)]; |
17 |
| - |
18 |
| - usableSelf.navigationItem.leftBarButtonItem = statsButton; |
19 |
| - } |
20 |
| - @catch(NSException *exception){ |
21 |
| - JELog(@"Failed to add settings button: %@", exception); |
| 15 | + [self addSettingsButton]; |
| 16 | + [self addObserver:self forKeyPath:@"navigationItem" options:NSKeyValueObservingOptionNew context:nil]; |
| 17 | +} |
| 18 | + |
| 19 | +- (void)dealloc { |
| 20 | + // Remove observer to avoid memory leaks |
| 21 | + [self removeObserver:self forKeyPath:@"navigationItem"]; |
| 22 | + %orig; |
| 23 | +} |
| 24 | + |
| 25 | +%new |
| 26 | +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { |
| 27 | + // Check if the observed keyPath is "navigationItem" |
| 28 | + if ([keyPath isEqualToString:@"navigationItem"]) { |
| 29 | + // Re-add the button whenever the navigationItem changes |
| 30 | + [self addSettingsButton]; |
22 | 31 | }
|
23 | 32 | }
|
| 33 | +%new |
| 34 | +- (void)addSettingsButton { |
| 35 | + // Create the button |
| 36 | + MainFeedViewController *usableSelf = self; |
| 37 | + usableSelf.settingsButton = [[UIBarButtonItem alloc] initWithTitle:[[JDESettingsManager sharedInstance] localizedStringForKey:@"emproved"] |
| 38 | + style:UIBarButtonItemStyleDone |
| 39 | + target:self |
| 40 | + action:@selector(presentJDEViewController:)]; |
| 41 | + |
| 42 | + // Add the button to the navigation item |
| 43 | + |
| 44 | + usableSelf.navigationItem.leftBarButtonItem = usableSelf.settingsButton; |
| 45 | +} |
24 | 46 |
|
25 | 47 | %new
|
26 | 48 | -(void)presentJDEViewController:(id)sender{
|
|
0 commit comments