我不知道在哪里搜索我的问题,但我对此很困惑。
前提是:我有一个调用 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/