1

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];
}
.......

-(void)cancelImagePicker:(id)sender;からアクションを実行する secondViewController のナビゲーション バーに「キャンセル」ボタンを追加したいと考えていますNavigationController.m。プッシュされた 3 番目、4 番目、5 番目の ViewController についても同様です。これはどのように達成できますか?各viewControllerにコードを書く必要がありますか、それとも共通に書くことができますか?

私はボタンクリックのfromを次のようにNavigationController提示しました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 に答える