1

我有两个视图控制器 - aUIViewControllerUITableViewController.

而且它们都需要相同的代码barButtonItems- 完成按钮和设置按钮。我认为方法选择器和 in 具有相同的代码viewDidLoad很难看,但是我想不出任何其他方法可以barButtonItems使两个控制器具有相同的代码。

共享代码如下所示:

- (void)dismissViewController { [self.navigationController dismissViewControllerAnimated:YES completion:nil]; }
- (void)done:(UIBarButtonItem *)sender { [self dismissViewController]; }
- (void)settings:(UIBarButtonItem *)sender { [self performSegueWithIdentifier:SETTINGS_SEGUE sender:sender]; }

- (void)viewDidLoad {
   [super viewDidLoad];

   if (self.navigationController.viewControllers.count == 1)
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(settings:)];
   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
}

在架构上,我的代码是这样的:

UITabBarController -view controllers->
    UINavigationController -root view controller-> UIViewController
    UINavigationController -root view controller-> UITableViewController

那么,有没有办法在UIViewController和之间共享代码UITableViewController?谢谢 :)

4

1 回答 1

0

如果你真的想要,你可以创建一个静态方法initNavigationBar在其中传入navigationItem.

+ (void ) initNavigationBar: (UiNavigationItem *) navigationItem 
{
      navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
         initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered 
         target:selfaction:@selector(settings:)];

      navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 

}

但是,如果这有意义,请三思。

于 2013-02-11T21:57:11.723 回答