【问题标题】:iOS 5 SDK Create methods that work everywhereiOS 5 SDK 创建适用于任何地方的方法
【发布时间】:2012-03-02 08:42:08
【问题描述】:

您如何制作可跨多个视图工作的方法?例如。我创建了这个:

- (void)setPageTitle:(UILabel *)title withText:(NSString *)text
{
    UIColor *pageTextColor = [UIColor colorWithRed:18.0/255.0 green:79.0/255.0 blue:118.0/255.0 alpha:1.0];

    // Set page title
    UIFont *font = [UIFont fontWithName:@"PassionOne-Regular" size:23];
    [title setFont:font];
    [title setText: text];
    title.textColor = pageTextColor;
    title.shadowColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
    title.shadowOffset = CGSizeMake(0, 1);

    CGRect titleRect = [title textRectForBounds:title.bounds limitedToNumberOfLines:999];
    CGRect tr = title.frame;
    tr.size.height = titleRect.size.height;
    title.frame = tr;
}

我希望能够在不同视图中调用 UILabel 上的 setPageTitle 方法。我该怎么做呢?我在哪里放置此代码以使其工作?我只想把它放在 1 个文件中,让它在不同的视图中工作。谢谢。

【问题讨论】:

  • 所有您的视图都是同一个自定义类型吗?
  • 如果他们都有共同的自定义类型,一个简单的实例方法就可以了,不是吗?

标签: ios iphone methods ios5


【解决方案1】:

我建议将此作为 UIView 类的一个类别。

UIView+PageTitle.h

@interface UIView (PageTitle)
- (void)setPageTitle:(UILabel *)title withText:(NSString *)text;
@end

UIView+PageTitle.m

#import "UIView+PageTitle.h"
@implementation UIView (PageTitle)
- (void)setPageTitle:(UILabel *)title withText:(NSString *)text {
    // your implementation
}
@end

【讨论】:

  • 当我在我看来调用它时,我应该这样做:[self setPageTitle:pageTitle withText:@"My text."];
  • 是的。请务必导入类别标题以使其正常工作。#import "UIView+PageTitle.h"
  • 非常好,它有效。我不得不将 UIView 更改为 UIViewController 因为这就是我的文件,但除此之外,它还有效。谢谢!
  • edit *******我应该这样做:[self.view setPageTitle:pageTitle withText:@"My text."];
【解决方案2】:

您可能正在寻找的是要么创建 UIViewController 的子类(我相信这是您正在使用的)并将其作为您的类 MyUIViewController 作为方法,或者,您可以创建一个UIViewController 的类别并添加该方法。 Here 是关于如何创建类别的说明,以及一些有用的信息。类别是类功能的扩展,几乎就是您想要做的。

【讨论】:

    【解决方案3】:

    如果您想使用category,您至少应该创建一个有用的类别。不使用self 的类别中的实例方法放错了位置。

    由于您正在操作 UILabel,因此您应该将其设为 UILabel 类别。

    @interface UILabel (PageTitle)
    - (void)setPageTitle:(NSString *)text {
        UIColor *pageTextColor = [UIColor colorWithRed:18.0/255.0 green:79.0/255.0 blue:118.0/255.0 alpha:1.0];
    
        // Set page title
        UIFont *font = [UIFont fontWithName:@"PassionOne-Regular" size:23];
        [self setFont:font];
        [self setText: text];
        self.textColor = pageTextColor;
        self.shadowColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
        self.shadowOffset = CGSizeMake(0, 1);
    
        CGRect titleRect = [self textRectForBounds:self.bounds limitedToNumberOfLines:999];
        CGRect tr = self.frame;
        tr.size.height = titleRect.size.height;
        self.frame = tr;
    }
    @end
    

    像这样使用它:

    UILabel *myLabel;
    [myLabel setPageTitle:@"Foobar"];
    

    【讨论】:

      【解决方案4】:

      添加快速方法是添加一个'+'使其成为另一个类中的静态方法:

      UIKitUtilities.h

      + (void)setPageTitle:(UILabel *)title withText:(NSString *)text;

      而在m文件中:

      + (void)setPageTitle:(UILabel *)title withText:(NSString *)text { ...your code here... }

      【讨论】:

      • 我应该导入我在要使用的视图中创建的文件的 .h 吗?
      • 是的,没错。尽管根据您的 cmets,该类别更好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多