我有两个视图控制器 - aUIViewController
和UITableViewController
.
而且它们都需要相同的代码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
?谢谢 :)