【发布时间】:2014-09-13 13:44:22
【问题描述】:
我对客观 C 场景相当陌生。我正在编写一个应用程序,我注意到我一直在重复自己,那么为什么不创建一个方法呢?我试图创建一个具有多个参数的方法,这些参数是 UIButton 的颜色和位置。所以基本上每次我调用这个方法时,我都可以创建一个 UIbutton。我将如何将其创建为自己的方法?
//Left Button
uploadPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[uploadPhotoButton setImage:[UIImage imageNamed:@"uploadphotoicon"] forState:UIControlStateNormal];
uploadPhotoButton.frame = CGRectMake(0, 0, 80, 80);
uploadPhotoButton.clipsToBounds = YES;
uploadPhotoButton.layer.cornerRadius = 80/2.0f;
uploadPhotoButton.layer.borderColor = [UIColor colorWithRed:.102 green:.737 blue:.612 alpha:1.0].CGColor;
uploadPhotoButton.layer.borderWidth = 2.0f;
[self.view addSubview:uploadPhotoButton];
[uploadPhotoButton setCenter:CGPointMake(78, 139)];
[uploadPhotoButton addTarget:self action:@selector(uploadPhotoButton:) forControlEvents:UIControlEventTouchUpInside];
//Right Button
uploadPhotoButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
[uploadPhotoButton2 setImage:[UIImage imageNamed:@"uploadphotoicon"] forState:UIControlStateNormal];
uploadPhotoButton2.frame = CGRectMake(0, 0, 80, 80);
uploadPhotoButton2.clipsToBounds = YES;
uploadPhotoButton2.layer.cornerRadius = 80/2.0f;
uploadPhotoButton2.layer.borderColor = [UIColor colorWithRed:.749 green:.580 blue:.894 alpha:1.0].CGColor;
uploadPhotoButton2.layer.borderWidth = 2.0f;
[self.view addSubview:uploadPhotoButton2];
[uploadPhotoButton2 setCenter:CGPointMake(242, 139)];
[uploadPhotoButton2 addTarget:self action:@selector(uploadPhotoButton:) forControlEvents:UIControlEventTouchUpInside];
如您所见,我正在创建两个不同的按钮,只是颜色和位置不同,所以我宁愿有一种方法可以让我创建按钮。请逐步指导我如何创建此方法。 谢谢!
【问题讨论】:
-
你有没有努力创建这个方法?如果您对 Objective-C 的了解不够多,无法编写方法,我可以礼貌地建议您找一个关于该语言的好教程。 SO 并不是学习基础知识的好地方。
标签: ios objective-c