【问题标题】:iPhone repeat call of a select codeiPhone 重复调用选择代码
【发布时间】:2010-01-18 17:18:59
【问题描述】:

假设我有一些代码我想重复多次。我应该如何最好地将它包含在我的 iPhone 应用程序中,只需要编写一次?

它是一个典型的 TableView 控制器应用程序。

//Set Icon
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(30,25,20,20)];
        imgView.image = [UIImage imageNamed:@"ico-date.png"];
        [self.view addSubview:imgView];

问候

【问题讨论】:

  • 图像和 imageNamed: 存在内存缓存问题。您可能应该使用 imageWithContentsOfFile:pathForResource:ofType:。另请参阅stackoverflow.com/questions/806374/…
  • 对不起,我的意思是:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ico-date" ofType:@"png"]]。

标签: iphone uitableview call


【解决方案1】:

您的选择:

1) 创建一个静态 C 函数来完成它

static UIImageView* myfunc(CGFloat x, CGFloat y, CGFloat w, CGFloat h,
  NSString* name, NSString* type, UIView* parent) {
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:
      CGRectMake(x,y,w,h)]; 
    imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
      pathForResource:name ofType:type]];
    [self.view addSubView:imgView];
    [imgView release];
    return imgView;
}

2) 创建一个 C 宏

#define CREATE_ICON_VIEW(parent,x,y,w,h,name) \
  ...

3) 创建一个 Objective-C 静态方法

// in @interface section
+ (UIImageView*)addIconWithRect:(CGRect)rect name:(NSString*)name
  type:(NSString*)type toView:(UIView*)view;

// in @implementation section
+ (UIImageView*)addIconWithRect:(CGRect)rect name:(NSString*)name
  type:(NSString*)type toView:(UIView*)view {
  UIImageView *imgView = [[UIImageView alloc] initWithFrame:rect];
  imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
    pathForResource:name ofType:type]];
  [self.view addSubView:imgView];     }
  [imgView release];
  return imgView;
}

// somewhere in app code
[MyClass addIconWithRect:CGMakeRect(0,0,32,32) name:@"ico-date"
  type:@"png" toView:parentView];

4) 在 UIImage 或 UIImageView 上创建一个 Objective-C 类别

5) 在要添加 UIImageView 的视图上创建方法

- (void)addIconWithRect:(CGRect)rect name:(NSString*)name type:(NSString*)type {
  UIImageView *imgView = [[UIImageView alloc] initWithFrame:rect];
  imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
    pathForResource:name ofType:type]];
  [self.view addSubView:imgView];     }
  [imgView release];
  return imgView;
}

6) 创建一个 Helper 类

与选项 (3) 类似,但将静态方法放在单独的类中,该类仅用于重复代码部分的实用方法,例如调用帮助程序类“UIUtils”。

7) 使用 C 内联函数

static inline UIImageView* myfunc(CGFloat x, CGFloat y, CGFloat w, CGFloat h,
  NSString* name, NSString* type, UIView* parent) {
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:
      CGRectMake(x,y,w,h)]; 
    imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
      pathForResource:name ofType:type]];
    [self.view addSubview:imgView]; 
    [imgView release];
    return imgView;
}

8) 使用循环重复执行相同的代码

9) 使用普通的非静态 Objective-C 方法

就我个人而言,对于您的特定示例,我不会选择任何这些,而只是将其写出来,除非它在文件中重复十次以上,在这种情况下,我可能会选择 (3)。如果它在很多文件中使用,我可能会选择 (6)。

编辑:扩展了 (3) 和 (6) 的描述,并注意我何时使用 (6)。

编辑:添加选项 8 和 9。修复了内存泄漏和一些错误。

编辑:更新代码以使用 imageWithContentsOfFile 而不是 imageNamed。

【讨论】:

  • 您可以在 C 函数中传递 CGRect
  • 关于 3) 创建一个Objective-C静态方法,第一个位去哪里了,appDelegate?然后我要为“MyClass”添加什么?
  • 这是一个静态方法,所以它是例如“XYZAppDelegate”(类名)而不是例如“appDelegate”我写 MyClass 的变量名。
  • 但是如果我正在实现#3, 1) 我在哪里粘贴上面的方法,它会从 myAppAppDelegate 运行,然后我可以从任何地方使用“[MyClass addIconWithRect:CGMakeRect(0,0 ,32,32) 名称:@"ico-date.png" toView:parentView];" ??
  • (+) 符号表示静态方法。所有方法,无论是否静态,都必须在@interface 部分中声明,并且在@implementation 部分中实现。如果您从很多地方调用它,请考虑将它放在自己单独的类中(我的意思是“创建一个 Helper 类”)并调用 UIUtils 或类似的东西。
【解决方案2】:

有几种方法

  • 子分类

把它放在一个基类中,然后从那个继承,不用大惊小怪

  • 共享方法

参数化self 的使用,而是将 UIView* 作为您想要添加的参数传递。然后将该方法粘贴到一个可从各处访问的单个类中,并仅在适当时调用它。

  • 类别

创建一个类似 UIImageView+Icon 的类别并将代码放在那里,然后您可以将其缩短为类似 [self.view addSubview:[UIImageView icon:@"ico-date.png"]] 的内容

【讨论】:

    猜你喜欢
    • 2012-07-14
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多