2013-03-12 84 views
0

在我的iOS应用程序中,我将下面的代码在每个类中重复一遍又一遍地重复。iOS应用程序视图和导航设计自定义类

我试图将该方法强制转换为NSObject类,但我收到“navigationItem”使用错误。

-(void)customDesign { 
//background pattern 
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"BG-pattern.png"]]; 

// nav bar 
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bar.png"] forBarMetrics:UIBarMetricsDefault]; 

//back button color #2974c3 
[self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:41.0/255.0f green:116.0/255.0f blue:195.0/255.0f alpha:1.0]]; 

//settings button 
UIImage* settingsImage = [UIImage imageNamed:@"ButtonMenu.png"]; 
CGRect frameimg = CGRectMake(0, 0, settingsImage.size.width, settingsImage.size.height); 
UIButton *uiSettingsButton = [[UIButton alloc] initWithFrame:frameimg]; 
[uiSettingsButton setBackgroundImage:settingsImage forState:UIControlStateNormal]; 
[uiSettingsButton addTarget:self action:@selector(menuButton) forControlEvents:UIControlEventTouchUpInside]; 
[uiSettingsButton setShowsTouchWhenHighlighted:YES]; 
//add buton to navbar 
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithCustomView:uiSettingsButton]; 
self.navigationItem.leftBarButtonItem = settingsButton; 

}

回答

0

你应该创建自己的UIViewController或UITableViewController中(你用哪个)的子类,并把这个代码在该子类的viewDidLoad方法。所有需要此代码的控制器都可以扩展您的自定义类。这将为您节省在任何地方编写代码的麻烦。

定制子类:

@interface CustomViewController : UIViewController 

@end 

在子类中你viewDidLoad方法:

@implementation CustomViewController 
    - (void)viewDidLoad { 
     [super viewDidLoad]; 

     // all your custom code here... 
    } 
@end 

扩展您的自定义类控制器,你想拥有共同的东西在做:

@interface VisuallyChangedViewController : CustomViewController 

@end 

@implementation VisuallyChangedViewController 
    - (void)viewDidLoad { 
     [super viewDidLoad]; 
    } 
@end 
+0

不幸的是,我已经与另一个班级扩展了一个班级。我不能用2来扩展 – 2013-03-15 10:32:25