Reputation: 226
I didn't know where to search for my question, but I am very confused about this.
The premise is: I have one ViewController X that calls ViewController Y and depending on which button is selected in X, I want a different button title for ViewController Y.
In ViewController X, (SSPhotosSelectionView is ViewController Y in this case):
- (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];
}
}
}
As you can see from the code, if albumsButton is selected, I want the downloadButton in SSPhotoSelectionViewController to say "Add", otherwise, say "Download".
downloadButton in this case, is a SSPhotosButton object, which is a subclass of 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
My problem is: No matter which button I select, (albumButton or galleryButton), the text is always "Download", never "Add". I suspect I'm doing something wrong in my SSPhotosButton classes, which is SSPhotosButtonText is always 0, which is why it's always SSPhotosButtonTextDownload, but how can I fix this?
Thank you.
Upvotes: 0
Views: 49
Reputation: 650
In prepareForSeque
you never instantiate the destination view controller directly. Instead, you access it via the segue
parameter:
- (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];
}
}
}
Doing it so should set your text correctly.
Upvotes: 2