我正在尝试在两个视图控制器之间传递数据。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中,我实例化了第一个viewcontroller,并将自己设置为它的委托。我假设,当viewcontroller2在其方法中运行代码时,它将查看其委托是否实现了委托方法并使用该代码。。我在这里错过了什么?
您的问题是错误地使用了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;
}
}
您需要在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
}