0

我是 iOS 开发的新手。在这里,我在不同时间使用相同的代码时遇到问题,因此如何在 iPhone 中实现 oops 概念。

sharebuttonpress = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
sharebuttonpress.frame = CGRectMake(330, 800, 145, 85);

[sharebuttonpress setBackgroundImage:[UIImage imageNamed:@"share_new.png"]
                        forState:UIControlStateNormal];
[sharebuttonpress addTarget:self action:@selector(sharePressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sharebuttonpress];

[self.view bringSubviewToFront:sharebuttonpress];
4

1 回答 1

1

方法会做

在开发应用程序之前,您必须学习 Objective-C。

Apple.Developer 站点阅读基础知识。阅读方法和消息部分

例子:

-(void)createMyButton{

  sharebuttonpress = [UIButton buttonWithType:UIButtonTypeCustom];
  sharebuttonpress.frame = CGRectMake(330, 800, 145, 85);

  [sharebuttonpress setBackgroundImage:[UIImage imageNamed:@"share_new.png"]
                        forState:UIControlStateNormal];
  [sharebuttonpress addTarget:self action:@selector(sharePressed:)   forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:sharebuttonpress];

  [self.view bringSubviewToFront:sharebuttonpress];

}

在需要的地方调用该方法

[self createMyButton];

在此处阅读有关 Objective C 中代码重用的高级文档

于 2012-10-11T13:53:56.523 回答