我正在尝试在两个视图控制器之间传递数据。 viewcontroller2是viewcontroller的委托..注意..我调用委托属性“homie”是的我知道这是不好的做法,但我只是在试图理解这个概念。
这里是viewcontroller:
#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在其方法中运行代码时,它将查看其委托是否已实现委托方法并使用该代码..我在这里缺少什么?
答案 0 :(得分: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;
}
}
答案 1 :(得分: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
}