我不知道在哪里搜索我的问题,但我对此感到非常困惑。
前提是:我有一个调用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),文字总是&#34;下载&#34;,永远&#34;添加&#34;。我怀疑我在SSPhotosButton类中做错了什么,SSPhotosButtonText始终为0,这就是为什么它总是SSPhotosButtonTextDownload,但我该如何解决这个问题呢?
谢谢。
答案 0 :(得分:2)
在scope.contact[]
中,永远不会直接实例化目标视图控制器。相反,您可以通过prepareForSeque
参数访问它:
segue
这样做应该正确设置文本。