【问题标题】:my custom viewcontroller delegate not working我的自定义视图控制器委托不工作
【发布时间】:2012-06-22 08:59:24
【问题描述】:

我正在尝试使用委托将一个简单的字符串从一个视图控制器发送到另一个视图控制器,但我的委托不工作。我正在使用情节提要,不想使用 segue 传递数据。这是我的简单代码

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

#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

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

@interface ViewControllerB : UIViewController<viewControllerADelegate>

@end

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 *)controller data:(NSString *)item

【问题讨论】:

    标签: iphone objective-c ios delegates storyboard


    【解决方案1】:

    您的设计中的缺陷是尝试将数据与委托一起发送到您自己创建的对象。您当然不需要委托,因为您可以使用简单的属性来做到这一点。 myViewControllerBObject.dataProperty = @"some data"]; 就足够了。委托通常用于将数据发送回创建当前对象的控制器或应用中某处的任何其他控制器。

    例如,将ViewControllerA 中的委托代码移动到ViewControllerB,并将UIButtonIBAction 添加到ViewControllerB,这会将数据返回 发送到ViewControllerA .就是这样:

    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

    #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 对象。然后实现委托方法。

    ViewControllerA.h

    #import <UIKit/UIKit.h>
    #import "ViewControllerB.h"
    
    @interface ViewControllerA : UIViewController <viewControllerBDelegate>
    - (IBAction)buttonPressed:(id)sender;
    
    @end
    

    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
    

    希望这将有助于您更好地了解与代表的沟通。

    【讨论】:

    • 感谢您的解释。但我很困惑如何使用代表在标签栏之间发送数据,因为他们没有 segues ?例如将数据从第二个选项卡向后发送到第一个选项卡?提前致谢
    • 这是另一个问题,我相信它需要单独发布。尽管如此,这几乎与此相同,并且也易于理解。如果我发布的答案解决了您在这里遇到的问题,请随时接受:)
    • 这里我已经发布了关于标签栏的问题stackoverflow.com/questions/11133934/…如果您解决了问题,我将不胜感激。谢谢:)
    【解决方案2】:

    制作委托变量(强、非原子)而不是(__weak)。

    【讨论】:

    • 使它成为@property (strong,nonatomic) id 委托但没有结果:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    相关资源
    最近更新 更多