问题描述
我正在尝试从一个viewcontroller发送一个简单的字符串到另一个viewcontroller使用委托,但我的委托不工作。我正在使用storyboard,不想使用segue传递数据。这是我的简单代码
I am trying to send a simple string from One viewcontroller to another viewcontroller using delegate but my delegate is not working. I am using storyboard and dont want to use segue to pass data. Here is my simple code
ViewControllerA.h
ViewControllerA.h
#import <UIKit/UIKit.h>
@class ViewControllerA;
@protocol viewControllerADelegate <NSObject>
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item;
@end
@interface ViewControllerA : UIViewController{
__weak id <viewControllerADelegate> delegate;
}
- (IBAction)buttonPressed:(id)sender;
@property (weak) id<viewControllerADelegate> delegate;
@end
ViewControllerA.m
ViewControllerA.m
#import "ViewControllerA.h"
@interface ViewControllerA ()
@end
@implementation ViewControllerA
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonPressed:(id)sender {
[self.delegate addItem:self data:@"this is data"];
}
@end
ViewControllerB.h
ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
@interface ViewControllerB : UIViewController<viewControllerADelegate>
@end
ViewControllerB.m
ViewControllerB.m
#import "ViewControllerB.h"
#import "ViewControllerA.h"
@interface ViewControllerB ()
@end
@implementation ViewControllerB
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewControllerA *vca = [storyboard instantiateViewControllerWithIdentifier:@"A"];
[vca setDelegate:self];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item{
NSLog(@"delegate function called");
}
@end
我错过了一些傻事吗?代理方法 - (void)addItem:(ViewControllerA *)控制器数据:(NSString *)项目
不被触发。
Am I missing something silly ? The delegate method -(void) addItem: (ViewControllerA *)controller data:(NSString *)item
is not triggered.
推荐答案
您的设计中的缺陷是尝试发送数据与代表您自己创建的对象。您当然不需要代理,因为您可以使用简单的属性来执行。 myViewControllerBObject.dataProperty = @some data];
就够了。通常使用的代理是将数据发送回创建当前对象的控制器或应用程序中任何其他控制器。
The flaw in your design is to try sending data with delegates to an object you create yourself. You certainly don't need delegates for that as you can do it with simple properties. myViewControllerBObject.dataProperty = @"some data"];
is simply enough. What delegation is usually used for is send data back to the controller that created the current object or to any other controller somewhere in your app.
例如,移动代理代码在 ViewControllerA
到 ViewControllerB
并添加一个 UIButton
code> IBAction to ViewControllerB
,将数据返回发送到 ViewControllerA
。这里是:
For example, move the delegate code in ViewControllerA
to ViewControllerB
and add a UIButton
and an IBAction
to ViewControllerB
that will send the data back to ViewControllerA
. Here it goes:
ViewControllerB.h
ViewControllerB.h
#import <UIKit/UIKit.h>
@class ViewControllerB;
@protocol viewControllerBDelegate <NSObject>
-(void) sendDataBack: (ViewControllerB *)controller data:(NSString *)item;
@end
@interface ViewControllerB : UIViewController
{
__unsafe_unretained id <viewControllerBDelegate> delegate;
}
@property (nonatomic, assign) id<viewControllerBDelegate> delegate;
- (IBAction)buttonTouch:(id)sender;
@end
ViewControllerB.m
ViewControllerB.m
#import "ViewControllerB.h"
@interface ViewControllerB ()
@end
@implementation ViewControllerB
@synthesize delegate;
....
- (IBAction)buttonTouch:(id)sender {
[self.delegate sendDataBack:self data:@"my data"];
}
@end
而在 ViewControllerA
,你首先需要通过segues来抓住这个推送的 ViewControllerB
对象。然后执行委托方法。
And in ViewControllerA
, you first need to get a hold of that pushed ViewControllerB
object via segues. Then implement the delegate method.
ViewControllerA.h
ViewControllerA.h
#import <UIKit/UIKit.h>
#import "ViewControllerB.h"
@interface ViewControllerA : UIViewController <viewControllerBDelegate>
- (IBAction)buttonPressed:(id)sender;
@end
ViewControllerA.m
ViewControllerA.m
#import "ViewControllerA.h"
@interface ViewControllerA ()
@end
@implementation ViewControllerA
......
- (IBAction)buttonPressed:(id)sender {
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"id %@", [segue destinationViewController]);
ViewControllerB *vcb = (ViewControllerB *)[segue destinationViewController];
vcb.delegate = self; //key point
}
- (void)sendDataBack:(ViewControllerB *)controller data:(NSString *)item
{
NSLog(@"Here is the data %@ and the controller it comes from %@", item, controller);
}
@end
希望这将有助于您更好地了解与代表沟通。
Hope this will help you get a better understanding of communication with delegates.
这篇关于我的自定义viewcontroller委托不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!