简体   繁体   English

我需要使用委托将字符串从NSObject类传递给UIViewController

[英]I need to pass a string from a NSObject class to a UIViewController with a delegate

I need to pass a string from a NSObject class to a UIViewController, I understand that the best way is delegation but the delegate method isn't being called. 我需要将一个字符串从NSObject类传递给UIViewController,我知道最好的方法是委托,但不会调用委托方法。 I'm trying to set the UILabel an DieFacesViewController as the selectedOption from TemporarySelection. 我试图将UILabel和DieFacesViewController设置为TemporarySelection中的selectedOption。

A tableview shows the value of CustomOptionStore, once it's tapped passes its value to TemporarySelection and opens the modal view DieFacesViewCountroller which should, at least in my mind, take the label value from TemporarySelection. 表格视图显示CustomOptionStore的值,一旦点按它,将其值传递给TemporarySelection并打开模式视图DieFacesViewCountroller,至少在我看来,该视图应从TemporarySelection中获取标签值。 The reason I created TemporarySelection is because the DieFacesViewController will be used by other classes, not only by CustomOptionStore, and it will need to load the label from all those classes when different tableViews are selected. 我之所以创建TemporarySelection,是因为DieFacesViewController将被其他类使用,而不仅是CustomOptionStore,而且在选择了不同的tableViews时,还需要从所有这些类中加载标签。

I tried to set the delegate as self in both viewDidLoad and viewWillAppear with no luck, I don't understand if the view loads before being able to call the delegate method or if there's something wrong the way I set the method up. 我尝试在没有运气的情况下在viewDidLoad和viewWillAppear中将委托设置为self,我不明白视图是否在能够调用委托方法之前加载,或者我设置方法的方式是否有问题。

I've been stuck here for two days, this is the first time I post a question so please forgive me if it's a bit confused. 我已经在这里停留了两天,这是我第一次发布问题,因此如果有点困惑,请原谅我。

my delegator class TemporarySelection.h is 我的委托人类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

and my TemporarySelection.m is 我的TemporarySelection.m是

#import "TemporarySelection.h"

@implementation TemporarySelection

-(void)addSelection: (CustomOptionsStore *) selection{

    self.selectedOption = selection.description;

    [self.delegate sendSelection];


}

@end

the delegate class DiewFacesViewController.h is 委托类DiewFacesViewController.h是

#import <UIKit/UIKit.h>
#import "SelectedStore.h"
#import "TemporarySelection.h"


@interface DieFacesViewController : UIViewController <TemporarySelectionDelegate>


@property (strong, nonatomic) IBOutlet UILabel *SelectionName;


@end

and the DieFacesViewController.m is 并且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];

} 

You are not setting the delegate object properly.Check the above code 您没有正确设置委托对象。检查上面的代码

 #import "DieFacesViewController.h"

@interface DieFacesViewController ()<TemporarySelectionDelegate>
{
//global object

TemporarySelection *ts;
}
@end

@implementation DieFacesViewController


- (void)viewDidLoad {

    ts = [[TemporarySelection alloc]init];
    ts.delegate = self;

    [super viewDidLoad];

}

-(void)sendSelection{

   //Use the object to extract
    self.SelectionName.text = ts.selectedOption;

}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM