2016-04-15 7 views
0

私の質問をどこで検索するのか分かりませんでしたが、これについては非常に混乱しています。Enumの正しい使い方、ボタンは常に同じテキスト

前提は次のとおりです。私は1つのViewController Xを持っているのViewController Yを呼び出して、Xに選択されたボタンに応じて、私はSSPhotosSelectionViewでのViewController Yである(、のViewController XでのViewController Y.

に異なるボタンのタイトルを望んでいます)この場合:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    SSPhotoSelectionViewController *controller = [[SSPhotoSelectionViewController alloc] init]; 
    if ([segue.identifier isEqualToString:@"SelectPhotosSegue"]) { 
     if (self.albumsButton.selected) { 
      [controller.downloadButton setText:SSPhotosButtonTextAdd]; 
     } 
     else if (self.galleryButton.selected) { 
      [controller.downloadButton setText:SSPhotosButtonTextDownload]; 
     } 
    } 
} 

albumsButtonが選択されている場合は、コードから見ることができるように、私はSSPhotoSelectionViewControllerでdownloadButtonがそうでなければ、「ダウンロード」と言う、「追加」と言いたいです。

SSPhotosButton.h:

#import <UIKit/UIKit.h> 

typedef NS_ENUM(NSInteger, SSPhotosButtonText) { 
    SSPhotosButtonTextDownload = 0, 
    SSPhotosButtonTextAdd = 1, 
}; 

@interface SSPhotosButton : UIButton 

@property (nonatomic, assign) SSPhotosButtonText text; 

- (void)setText:(SSPhotosButtonText)text; 

@end 

SSPhotosButton.m:

#import "SSPhotosButton.h" 

@implementation SSPhotosButton 

- (id)initWithCoder:(NSCoder *)aDecoder { 
    if (self = [super initWithCoder:aDecoder]) { 
     self.layer.masksToBounds = YES; 
     if (self.text == SSPhotosButtonTextDownload) { 
      [self updateButtonWithText:@"Download"]; 
     } 
     else if (self.text == SSPhotosButtonTextAdd) { 
      [self updateButtonWithText:@"Add"]; 
     } 
    } 
    return self; 
} 

- (void)setText:(SSPhotosButtonText)text { 
    _text = text; 

    switch (text) { 
     case SSPhotosButtonTextDownload: 
      [self updateButtonWithText:@"Download"]; 
      break; 

     case SSPhotosButtonTextAdd: 
      [self updateButtonWithText:@"Add"]; 
      break; 

     default: 
      break; 
    } 
} 

- (void)updateButtonWithText:(NSString *)string { 
    self.titleLabel.text = string; 
    [self setTitle:string forState:UIControlStateNormal]; 
    [self setBackgroundColor:[UIColor clearColor]]; 
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    [self setTitleColor:[UIColor colorWithWhite:0.75 alpha:1.0] forState:UIControlStateHighlighted]; 
    [self setTitleColor:[UIColor colorWithWhite:0.75 alpha:1.0] forState:UIControlStateSelected]; 
    [self setTitleColor:[UIColor colorWithWhite:0.25 alpha:0.5] forState:UIControlStateDisabled]; 
} 
@end 

私の問題

downloadButtonは、この場合には、SSPhotosButtonオブジェクト、UIButtonのサブクラスであるです:選択したボタン(albumButtonまたはgalleryButton)に関係なく、テキストは常に「ダウンロード」で、決して「追加」されません。 SSPhotosButtonTextが常に0であるSSPhotosButtonクラスで何か間違っていると思われます。これは常にSSPhotosButtonTextDownloadですが、これをどのように修正できますか?

ありがとうございます。

答えて

2

prepareForSeque宛先ビューコントローラを直接インスタンス化します。代わりに、あなたはsegueパラメータを経由してアクセス:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    SSPhotoSelectionViewController *controller = (SSPhotoSelectionViewController *) segue.destinationViewController; 
    if ([segue.identifier isEqualToString:@"SelectPhotosSegue"]) { 
     if (self.albumsButton.selected) { 
      [controller.downloadButton setText:SSPhotosButtonTextAdd]; 
     } 
     else if (self.galleryButton.selected) { 
      [controller.downloadButton setText:SSPhotosButtonTextDownload]; 
     } 
    } 
} 

はそれはとても正しくテキストを設定する必要が行います。

+0

ねえ、ポインタありがとう!私はこれを完全に忘れる。今、私はクラッシュを取得しています:キャッチされていない例外 'NSInvalidArgumentException'の理由でアプリケーションを終了します。理由: ' - [SSNavigationController downloadButton]:インスタンス0x12611b800に送信されたセレクタが認識されません'。 SSPhotoSelectionViewControllerがナビゲーションビューコントローラ内に埋め込まれているためです – sallyp

+0

この場合は、「一度に値を1つのView Controllerに渡します」という意味です。つまり、 'prepareForSegue'では値を' SSNavigationController'に渡します(カスタムプロパティセッターや 'viewWillAppear'を使って)。 'SSPhotoSelectionViewController'です。 – Lasse

関連する問題