我需要将一个字符串从NSObject类传递给UIViewController,我知道最好的方法是委托但是没有调用委托方法。我正在尝试将UILabel设置为DieFacesViewController作为TemporarySelection中的selectedOption。
一个tableview显示了CustomOptionStore的值,一旦点击它将其值传递给TemporarySelection并打开模态视图DieFacesViewCountroller,至少在我看来,它应该从TemporarySelection中获取标签值。我创建TemporarySelection的原因是因为DieFacesViewController将被其他类使用,而不仅仅是CustomOptionStore,并且当选择不同的tableView时,它将需要从所有这些类加载标签。
我试图在viewDidLoad和viewWillAppear中将委托设置为self而没有运气,我不明白视图是否在能够调用委托方法之前加载,或者如果我设置方法的方式有问题。
我已经被困在这里两天了,这是我第一次发帖提问,如果有点困惑请原谅我。
我的委托人类TemporarySelection.h是
#import <Foundation/Foundation.h>
#import "CustomOptionsStore.h"
@class DieFacesViewController;
@protocol TemporarySelectionDelegate <NSObject>
-(void)sendSelection;
@end
@interface TemporarySelection : NSObject
@property (nonatomic, weak) id <TemporarySelectionDelegate> delegate;
@property (nonatomic, strong) NSString *selectedOption;
-(void)addSelection: (CustomOptionsStore *) selection;
@end
和我的TemporarySelection.m是
#import "TemporarySelection.h"
@implementation TemporarySelection
-(void)addSelection: (CustomOptionsStore *) selection{
self.selectedOption = selection.description;
[self.delegate sendSelection];
}
@end
委托类DiewFacesViewController.h是
#import <UIKit/UIKit.h>
#import "SelectedStore.h"
#import "TemporarySelection.h"
@interface DieFacesViewController : UIViewController <TemporarySelectionDelegate>
@property (strong, nonatomic) IBOutlet UILabel *SelectionName;
@end
和DieFacesViewController.m是
#import "DieFacesViewController.h"
@interface DieFacesViewController ()
@end
@implementation DieFacesViewController
- (void)viewDidLoad {
TemporarySelection *ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
TemporarySelection *ts = [[TemporarySelection alloc]init];
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}