0

我需要做什么才能在我的 iOS 项目中设置每个主视图的标题。以下是我到目前为止所做的以及我正在尝试做的事情......

创建了我试图一起工作的文件。

  • PageTitleView.h
  • PageTitleView.m
  • PageBaseViewController.h
  • PageBaseViewController.m
  • HomeViewController.h
  • 主视图控制器.m
  • ActivityFeedViewController.h
  • ActivityFeedViewController.m

基本上设置 PageTitleView.h * .m 以及 PageBaseViewController.h & .m 以便我可以调用 HomeViewController&ActivityFeedViewController 并设置当前视图的标题。

这是我从这些文件中获取的代码


PageTitleView.h

#import "ContainerView.h"
#import "BaseLabel.h"

@interface PageTitleView : ContainerView
@property (nonatomic, strong) BaseLabel* label;
@property (nonatomic, strong) NSString* title;

@end

PageTitleView.m

#import "PageTitleView.h"

@implementation PageTitleView

@synthesize label;



- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 35)];
    label = [[BaseLabel alloc] initWithFrame:CGRectMake(18, 10, frame.size.width , 25)];
    label.font=[UIFont fontWithName:@"Helvetica" size:15.0];
    label.textColor = (UIColor *) COLOR_DARK_BLUE;
    label.text =[[NSString alloc] initWithFormat: @"text"];


    self.backgroundColor = (UIColor *)COLOR_WHITE;

    self.layer.shadowOpacity = 0.05;
    self.layer.shadowOffset = CGSizeMake(.25, .25);
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowRadius = 5;

    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.layer.bounds].CGPath;


    self.layer.cornerRadius = 10.0;

    self.layer.borderWidth = 0.25;
    self.layer.borderColor = [UIColor grayColor].CGColor;


    [self addSubview:label];

    return self;
}



@end

PageBaseViewController.h

#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#import "Settings.h"
#import "CustomButton.h"
#import "PageTitleView.h"

@interface PageBaseViewController : UIViewController

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) CustomButton *backButton;


@end

PageBaseViewController.m

#import "PageBaseViewController.h"

@interface PageBaseViewController ()

@end

@implementation PageBaseViewController
synthesize scrollView;


- (void)viewDidLoad {
    [super viewDidLoad];


    // GRADIENT
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.colors = [NSArray arrayWithObjects:
                       (id)BACKGROUND_GRADIENT_COLOR_1.CGColor,
                       (id)BACKGROUND_GRADIENT_COLOR_2.CGColor,
                       (id)BACKGROUND_GRADIENT_COLOR_3.CGColor,  nil];
    gradient.locations = [NSArray arrayWithObjects:
                          (id)[NSNumber numberWithFloat:0.0],
                          (id)[NSNumber numberWithFloat:0.4],
                          (id)[NSNumber numberWithFloat:1.0], nil];
    [self.view.layer insertSublayer:gradient atIndex:0];



    float height = self.view.frame.size.height - TITLEBAR_HEIGHT - self.tabBarController.tabBar.frame.size.height;
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, TITLEBAR_HEIGHT, self.view.frame.size.width, height)];
    [self.view addSubview:scrollView];


    PageTitleView *pageTitle = [[PageTitleView alloc] initWithFrame:CGRectMake(105, -10, 100 , 35)];
    [self.view addSubview:pageTitle];



    /* This button is currently added to the view and will show in the TITLEBAR_HEIGHT. 
     These will be used throughout the entire project, we will be working next to have 
     correct buttons turn on and off for each view.*/

    CustomButton *buttonLeft = [[CustomButton alloc] initWithFrame:CGRectMake(5, 5, 60, 25) andText:@"Left"];
    [buttonLeft addTarget:self action:@selector(buttonLeft:) forControlEvents:UIControlEventTouchUpInside];
    [buttonLeft setBackgroundColor:BUTTON_COLOR_1];
    [self.view addSubview:buttonLeft];

    // Remove this button from homeview and have a logo show up.
    CustomButton *buttonRight = [[CustomButton alloc] initWithFrame:CGRectMake(235, 5, 80, 25) andText:@"Right"];
    [buttonRight addTarget:self action:@selector(buttonRight:) forControlEvents:UIControlEventTouchUpInside];
    [buttonRight setBackgroundColor:BUTTON_COLOR_1];
    [self.view addSubview:buttonRight];



}


- (void)buttonLeft:(UIButton *)buttonLeft {
    NSLog(@"Ping");
}

- (void)buttonRight:(UIButton *)buttonRight {
    NSLog(@"Ping");
}

@end

4

1 回答 1

0

通过以下步骤解决了这个问题。

  1. 将 PageTitleView.h 导入到 PageBaseViewController.h

  2. 在 PageBaseViewController.h 中声明了以下属性

    @property (nonatomic, strong) PageTitleView *pageTitle;

  3. 然后在PageBaseViewController中调整了PageTitleView的导入

    来自:PageTitleView *pageTitle = [[PageTitleView alloc] initWithFrame:CGRectMake(105, -10, 100 , 35)];

    到:pageTitle = [[PageTitleView alloc] initWithFrame:CGRectMake(105, -10, 100 , 35)];

  4. 然后在我的一个视图中,例如 HomeViewController 我添加了以下代码。

    [self.pageTitle setTitle:@"Home"];

  5. 现在我将以下代码添加到我的其他视图中,并用适当的名称填充它。

    [self.pageTitle setTitle:@""];

于 2013-03-04T21:42:40.227 回答