2012-02-09 4 views
2

複数のビューでどのように動作するメソッドを作成しますか?例えば。私は別のビュー内UILabels上setPageTitleメソッドを呼び出すことができるようにしたいiOS 5 SDKどこでも動作するメソッドを作成する

- (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; 
} 

:私はこれを作成しました。これをどうやって行うのですか?私はこのコードをどこに入れて動作させるのですか?私はそれを1つのファイルに入れて、別のビューで動作させたいだけです。ありがとうございました。

+0

はすべて*あなたの意見*同じカスタムタイプはありますか? –

+0

はい、すべてカスタムです。 – Ross

+0

すべて共通のカスタムタイプを持っている場合は、単純なインスタンスメソッドで行います。 –

答えて

9

これを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 
+0

今すぐ試してみてください... – Ross

+0

私は私のビューでそれを呼び出すときに、私はこれをしなければいけません:[self setPageTitle:pageTitle withText:@ "My text。"]; – Ross

+0

はい。 #import "UIView + PageTitle.h" – picciano

0

簡単な方法を追加して、別のクラスの静的メソッド作る '+' を追加することです:

UIKitUtilities.h

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

とMファイル内:

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

+0

作成したファイルの.hを、使用したいビュー内でインポートする必要がありますか? – Ross

+0

はい、正しいです。カテゴリはあなたのコメントに基づいていますが。 – TigerCoding

1

あなたが探しているのは、UIViewControllerのサブクラスを作成するかあなたが使っているものです)、メソッドとしてそのクラスをMyUIViewControllerクラスにするか、またはUIViewControllerのカテゴリを作成してそのメソッドを追加することができます。 Hereはカテゴリの作成方法と便利な情報の説明です。カテゴリは、クラスの機能の拡張であり、あなたがやろうとしていることです。

1

あなたが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"]; 
関連する問題