0

在我的 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;

}

4

1 回答 1

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
于 2013-03-12T22:37:11.860 回答