1

我正在尝试在两个视图控制器之间传递数据。viewcontroller2 是 viewcontroller 的代表.. 注意.. 我称委托属性为“homie”是的,我知道这是不好的做法,但我只是在试图理解这个概念。

这是视图控制器:

#import <UIKit/UIKit.h>
@protocol ViewControllerDelegate <NSObject>
@required
- (void)sendData:(NSString *)theString;
@end

@interface ViewController : UIViewController
@property (nonatomic, assign) id <ViewControllerDelegate> homie;
- (void)doSomething;
- (IBAction)doneText:(id)sender;

@end

执行:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)doSomething{
}

- (IBAction)doneText:(id)sender {
    UITextField *thisField = sender;

    if([_homie respondsToSelector:@selector(sendData:)]){
        [_homie sendData:[thisField text]];
    }
}

现在这是另一个实现第一个视图控制器

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface ViewController2 : UIViewController <ViewControllerDelegate>

- (IBAction)hittingbtn:(id)sender;

@property (weak, nonatomic) IBOutlet UITextField *textfield;
@property (strong, nonatomic) ViewController *vc;

@end

执行:

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    ViewController *theview = [[ViewController alloc]init];
    theview.homie = self;

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


// DELEGATE METHOD
- (void)sendData:(NSString *)theString{
    [_textfield setText:theString];
}

- (IBAction)hittingbtn:(id)sender {
}
@end

在 viewdidload 中,我实例化了第一个视图控制器,并将自己设置为它的代表。我假设当 viewcontroller2 在其方法中运行代码时,它将查看其委托是否已实现委托方法并使用该代码..我在这里缺少什么?

4

2 回答 2

1

您的问题是您使用 ViewControllers 错误。你已经初始化了 ViewController

ViewController *theview = [[ViewController alloc]init];
theview.homie = self;

接下来呢?你还没有将它分配给你的属性或其他东西。你做了什么?这很简单。你必须实施- (void)prepareForSegue:sender:. 首先在你的故事板中设置 segue 的标识符。然后进行以下操作。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegue"]) {
        ViewController *destinationViewController = (ViewController *)[segue destinationViewController];
        ViewController.homie = self;
    }
}
于 2013-07-28T19:31:44.660 回答
1

您需要在 prepareForSegue 中设置委托

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController respondsToSelector:@selector(setHomie)]) {
        [segue.destinationViewController setHomie:self];
    //or
    if ([[segue.destinationViewController isKindOfClass:[ViewController class]]) {
        ViewController *destinationViewController = (ViewController *)[segue destinationViewController];
        ViewController.homie = self;
    }
    //or the segue identifier which has already been mentioned
}
于 2013-07-28T19:33:11.500 回答