1

导航控制器.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

导航控制器.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

数据视图控制器.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 上的NavigationControllerfrom呈现为MainViewController

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

并在那里实现了委托方法。

4

1 回答 1

1

您可以使用通知中心而不是代表,想法是,

[NSNotificationCenter DefaultCenter] addObserver]
于 2012-12-14T07:47:31.133 回答