管理ios中一个类的代码

时间:2015-02-26 06:49:39

标签: ios

您好我在10类viewdidload方法中使用这些行

  SWRevealViewController *revealController = [self revealViewController];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont fontWithName:@"Arial" size:23.0f]};

UIButton *settingButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
UIImage *image = [UIImage imageNamed:@"fb_menu_icon.png"];
[settingButton setBackgroundImage:image forState:UIControlStateNormal];
[settingButton addTarget:revealController action:@selector(revealToggle:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:settingButton];
[self.navigationItem setLeftBarButtonItem:leftBarButton animated:YES];

我想从一个类方法中使用它并在任何类中导入它我该怎么做才能告诉我?

2 个答案:

答案 0 :(得分:3)

让一个类说SWViewController和超类作为UIViewController。将上面的代码放在SWViewController.m的viewDidLoad中。然后将您的每个类的超类更改为SWViewController。

SWViewController.h

@interface SWViewController : UIViewController
{

}

SWViewController.m

@implementation SWViewController

-(void)viewDidLoad
{
    [super viewDidLoad];

SWRevealViewController *revealController = [self revealViewController];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont fontWithName:@"Arial" size:23.0f]};

UIButton *settingButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
UIImage *image = [UIImage imageNamed:@"fb_menu_icon.png"];
[settingButton setBackgroundImage:image forState:UIControlStateNormal];
[settingButton addTarget:revealController action:@selector(revealToggle:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:settingButton];
[self.navigationItem setLeftBarButtonItem:leftBarButton animated:YES];

}
@end

现在让你的视图控制器像这样:

@interface MyViewController : SWViewController
    {

    }

答案 1 :(得分:0)

使该方法成为UIViewController上的类别方法。你仍然需要从每个viewDidLoad调用它,但它只有一行。

或者,如果您通过导航控制器呈现所有内容,则可以使导航控制器的委托在每个视图控制器上调用该方法,因为它被推入堆栈。

为此创建一个基本视图控制器类并不是一个好主意,会给你带来太多限制。