我的cpViewController有一个按钮,该按钮的模式继续到cpScanViewController。我想让cpScanViewController在成功扫描完成后通知ViewController一个字符串。在网上阅读了大量文章后,我相信代表团是实现这一目标的最佳方式吗?代表不通知我查看
- 委托设置是否正确?
- 委托人如何通知cpViewController?
cpScanViewController.h
#import <UIKit/UIKit.h>
@protocol ScanDelegate <NSObject>
-(void)receivedUPC:(NSString *)Scan;
@end
@interface cpScanViewController : UIViewController
@property (nonatomic, weak) id <ScanDelegate> delegate;
@property (nonatomic, retain) NSString *UPC;
-(void)checkUPC;
@end
cpScanViewController.m
@interface cpScanViewController()
{
NSString *UPC;
}
@end
@implementation cpScanViewController
@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)doSomeScanStuff
{
UPC = @"a string"
// even tried
[delegate receivedUPC:UPC];
}
-(void)checkUPC
{
}
@end
cpViewController.h
#import <UIKit/UIKit.h>
#import "cpScanViewController.h"
@interface cpViewController : UIViewController <ScanDelegate>
@end
cpViewController.m
@interface cpViewController()
{
cpScanViewController *cp;
}
@end
@implementation cpViewController
- (void)viewDidLoad
{
// set delegate
cp = [[cpScanViewController alloc] init];
[cp setDelegate:self];
}
-(void)receivedUPC:(NSString *)Scan{
// Nothing happening
NSLog(@"%@", Scan);
NSLog(@"The %@", cp.UPC);
}
@end
加入[delegate receivedUPC:self];到cpScanViewController中的viewDidLoad产生并警告不兼容的指针类型,将'cpScanViewController *'发送给'Scanner *'类型的参数。 – iDev 2014-09-12 14:05:59