0

アプリのすべての画面のツールバー (ストーリーボードから作成) に「ログアウト」ボタンがあります。タップしたときにどのページが表示されていても、同じアクションを実行したいと思います。

すべてのView Controllerで同じメソッドを作成することなく、これらすべてのボタンに同じことをさせる方法はありますIBActionか(私はさまざまなタイプのView Controllerを持っています)?

4

2 に答える 2

4

1 つの解決策は、すべてのツールバーでボタンを共有することです。これは見苦しくもあり、難しいことでもあります。私の好みは、ログアウト ロジックを独自のクラスにカプセル化し、すべてのボタンから呼び出すことです。

#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

次に、View Controllerで次のことができます

#import "AccountManager.h"

- (IBAction)didSelectLogout:(id)sender
{
    [[AccountManager sharedManager] logout];
}

この場合、各コントローラーにはまだアクションがありますが、ログアウト ロジックはアプリ全体に広がっていません。

于 2012-05-18T16:41:13.497 に答える
0

同様の問題がありました。多くのviewControllerにボタンが必要でした。タップすると、有料版の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];
    
于 2012-05-19T12:48:10.143 に答える