gpt4 book ai didi

ios - 向所有推送的 viewControllers 的导航栏添加一个按钮,该按钮执行来自主导航 Controller 的操作

转载 作者:行者123 更新时间:2023-11-29 13:23:07 24 4
gpt4 key购买 nike

NavigationController.h

#import <UIKit/UIKit.h>
@protocol NavigationControllerDelegate;
@interface NavigationController : UINavigationController<UINavigationControllerDelegate>
@property (nonatomic , assign)id<NavigationControllerDelegate , UINavigationControllerDelegate> delegate;
-(void)cancelImagePicker:(id)sender;
@end
@protocol NavigationControllerDelegate <NSObject>

- (void)mediaPickerController:(NavigationController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;

- (void)mediaPickerControllerDidCancel:(NavigationController *)picker;

@end

NavigationController.m

#import "NavigationController.h"
#import "DataViewController.h"
@interface NavigationController ()
@end

@implementation NavigationController
@synthesize delegate;

-(id)init{
UIViewController *controller = [[DataViewController alloc]initWithNibName:@"DataViewController" bundle:nil];
self = [super initWithRootViewController:controller];
if (self) {
self.navigationBar.topItem.title = @"Data Table";
self.navigationBar.tintColor = [UIColor clearColor];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
style:UIBarButtonSystemItemCancel target:self action:@selector(cancelImagePicker:)];
self.navigationBar.topItem.rightBarButtonItem = rightButton;
}
return self;
}

-(void)cancelImagePicker:(id)sender{
[delegate mediaPickerControllerDidCancel:self];
}
@end

DataViewController.m

.....
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
secondViewController *secondController = [[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil ];
[self.navigationController pushViewController: secondController animated:YES];
}
.......

我想在 secondViewController 的导航栏上添加一个“取消”按钮,该按钮执行 -(void)cancelImagePicker:(id)sender; 来自 NavigationController.m 的操作。对于推送的第三、第四和第五个 ViewController 也是如此。如何实现?我必须在每个 viewController 中编写代码还是可以共同编写?

我已经在 buttonClick 上显示了来自 MainViewControllerNavigationController

- (IBAction)navView:(id)sender {
NavigationController * controller = [[NavigationController alloc]init];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
}

并在那里实现委托(delegate)方法。

最佳答案

您可以使用通知中心而不是委托(delegate),想法是,

[NSNotificationCenter DefaultCenter] addObserver]

关于ios - 向所有推送的 viewControllers 的导航栏添加一个按钮,该按钮执行来自主导航 Controller 的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13855689/

24 4 0