ios - 正确使用 Enum,Button 总是相同的文本

标签 ios objective-c enums

我不知道在哪里搜索我的问题,但我对此很困惑。

前提是:我有一个调用 ViewController Y 的 ViewController X,并且根据在 X 中选择的按钮,我想要 ViewController Y 的不同按钮标题。

在 ViewController X 中,(在这种情况下 SSPhotosSelectionView 是 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 说“添加”,否则说“下载”。

在这种情况下,downloadButton 是一个 SSPhotosButton 对象,它是 UIButton 的子类:

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

我的问题是:无论我选择哪个按钮(albumButton 或galleryButton),文本始终是“下载”,而不是“添加”。我怀疑我在 SSPhotosButton 类中做错了什么,即 SSPhotosButtonText 始终为 0,这就是为什么它总是 SSPhotosButtonTextDownload,但我该如何解决这个问题?

谢谢。

最佳答案

prepareForSeque中,您从不直接实例化目标 View Controller 。相反,您可以通过 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];
        }
    }
}

这样做应该可以正确设置您的文本。

关于ios - 正确使用 Enum,Button 总是相同的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36656243/

相关文章:

ios - 使用 Logo 时找不到实例方法%new

objective-c - 是否可以在 iPhone 应用程序中加载带有自签名安全证书的 SSL 加密网站?

iphone - SQLite 数据库需要太多时间来检索(或获取一些数据)

ios - 如何在 IOS 中弯曲多段线(谷歌地图)?

iphone - 将随机图像放在uibutton上

objective-c - NSView 太多?

objective-c - MPMoviePlayerController 隐藏下一个上一个按钮

javascript - Flowtype - 字符串与字符串枚举不兼容

url - 使用 Floki 和 HttPotion 的 Elixir 脚本无法解析 url

java - 在运行时创建和覆盖 Java 枚举对象 [GregTech/minecraft]