3

デリゲートを使用して、あるビューコントローラーから別のビューコントローラーに単純な文字列を送信しようとしていますが、デリゲートが機能していません。私はストーリーボードを使用していますが、セグエを使用してデータを渡したくありません。これが私の簡単なコードです

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はトリガーされません。

4

2 に答える 2

4

設計上の欠陥は、自分で作成したオブジェクトにデリゲートを使用してデータを送信しようとすることです。単純なプロパティでできるので、デリゲートは必要ありません。myViewControllerBObject.dataProperty = @"some data"];で十分です。通常、委任が使用されるのは、現在のオブジェクトを作成したコントローラー、またはアプリ内の他のコントローラーにデータを送り返すことです。

たとえば、デリゲート コードを に移動しViewControllerA、データを に送り返すを追加ViewControllerBします。ここに行きます:UIButtonIBActionViewControllerBViewControllerA

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、最初にプッシュされた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

これが、代表者とのコミュニケーションをよりよく理解するのに役立つことを願っています。

于 2012-06-21T06:53:10.990 に答える
1

デリゲート変数を (__weak) ではなく (strong、nonatomic) にします。

于 2012-06-20T09:50:00.730 に答える