我的应用程序的每个屏幕的工具栏中(通过情节提要制作)都有一个“注销”按钮。无论点击哪个页面,我都希望它执行相同的操作。
有没有办法让所有这些按钮做同样的事情而不必IBAction
在每个视图控制器中创建相同的方法(我有一堆不同类型的视图控制器)?
我的应用程序的每个屏幕的工具栏中(通过情节提要制作)都有一个“注销”按钮。无论点击哪个页面,我都希望它执行相同的操作。
有没有办法让所有这些按钮做同样的事情而不必IBAction
在每个视图控制器中创建相同的方法(我有一堆不同类型的视图控制器)?
一种解决方案是在所有工具栏中共享按钮。这既丑陋又困难。我的偏好是将注销逻辑封装在它自己的类中,然后从所有按钮中调用它:
#import "AccountManager.h"
@implementation AccountManager
+ (id)sharedManager
{
static AccountManager *sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[AccountManager alloc] init];
});
return sharedManager;
}
- (void)logout
{
// logout logic
}
@end
然后在您的视图控制器中,您可以拥有
#import "AccountManager.h"
- (IBAction)didSelectLogout:(id)sender
{
[[AccountManager sharedManager] logout];
}
在这种情况下,您仍然在每个控制器中都有一个操作,但您的注销逻辑并未遍布整个应用程序。
我有一个类似的问题。我需要许多 viewController 上的按钮。点击它会转到付费版本的 iTunes 链接。这就是我所做的。
自定义按钮.h
+(void)createDownloadFullVersionButton:(UIButton*)button
自定义按钮.m
+(void)createDownloadFullVersionButton:(UIButton*)button
{
// UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Since the buttons can be any width we use a thin image with a stretchable center point
UIImage *buttonImage = [[UIImage imageNamed:@"button_slice.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIImage *buttonPressedImage = [[UIImage imageNamed:@"button_slice_over.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
[button.layer setCornerRadius:10.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderColor:[[UIColor whiteColor]CGColor]];
button.layer.borderWidth = 1.0;
[button addTarget:self action:@selector(actionDownloadPaidVersion:) forControlEvents:UIControlEventTouchUpInside];
//CGRect buttonFrame = [button frame];
//buttonFrame.size.width = 220;
//buttonFrame.size.height = 48;
//[button setFrame:buttonFrame];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
UIImage *image = [UIImage imageNamed:@"fullversion_icon.png"];
UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake((button.frame.size.height - (image.size.width / 2))/2, (button.frame.size.height - (image.size.width / 2))/2, image.size.width / 2, image.size.height / 2)];
iconView.image = image;
[button addSubview:iconView];
[iconView release];
UILabel *iconLabel = [[UILabel alloc]initWithFrame:CGRectMake(iconView.frame.origin.x * 2 + iconView.frame.size.width, (button.frame.size.height - (image.size.width / 2))/2, 180, 24)];
iconLabel.text = @"Download Full Version";
iconLabel.shadowColor = [UIColor blackColor];
iconLabel.shadowOffset = CGSizeMake(1.0, 1.0);
iconLabel.backgroundColor = [UIColor clearColor];
iconLabel.textColor = [UIColor whiteColor];
iconLabel.font = [UIFont boldSystemFontOfSize:14.0];
[button addSubview:iconLabel];
[iconLabel release];
}
+(void)actionDownloadPaidVersion:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/app/filehider/id475295711"]];
}
现在在我需要按钮的视图控制器上。
添加一个简单的按钮(buttonAd)。
使用参数 buttonAd 调用 CustomButton 的 createDownloadFullVersionButton 方法,如下所示:
[CustomBarButton createDownloadFullVersionButton:buttonAd];