如何制作一个按钮在iOS的每个屏幕上都做同样的事情?

时间:2012-05-18 16:22:43

标签: ios uiviewcontroller

我有一个"退出"我的应用程序的每个屏幕的工具栏中的按钮(通过故事板制作)。我希望它能够执行相同的操作,无论在哪个页面被点击时显示。

有没有办法让所有这些按钮做同样的事情,而不必在每个视图控制器中创建相同的IBAction方法(我有一堆不同类型的视图控制器)?

2 个答案:

答案 0 :(得分:4)

一种解决方案是在所有工具栏上共享按钮。这既丑陋又困难。我的首选是将logout逻辑封装在自己的类中,然后从所有按钮中调用它:

#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];
}

在这种情况下,您仍然在每个控制器中都有一个操作,但您的注销逻辑并未在整个应用程序中传播。

答案 1 :(得分:0)

我有类似的问题。我在许多viewControllers上需要一个按钮。点击它将转到付费版本的itunes链接。这就是我所做的。

CustomButton.h

+(void)createDownloadFullVersionButton:(UIButton*)button

CustomButton.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"]];
}

现在在viewControllers上我需要按钮。

  1. 添加一个简单的按钮(buttonAd)。

  2. 使用参数buttonAd调用CustomButton的createDownloadFullVersionButton方法,如下所示:

    [CustomBarButton createDownloadFullVersionButton:buttonAd];