2015-01-21 73 views
0

我有两个类AB。我有一个删除将变量从A传递到B。我试着从BA,让我可以从B控制A。我试图创建另一个委托,但它不起作用。由于我已经在B.h中导入A.h,所以我不能在A.h.中导入B.h。我应该如何设置自定义委托的两种方式?ios两种方式自定义代理

A.H

@protocol SoundDetectDelegate <NSObject> 
-(void)update_from_sound: (SKSpriteNode*) node; 
@end 
@interface SoundDetect : NSObject 
@property (nonatomic, weak) id <SoundDetectDelegate> delegate; 
+ (id)sharedInstance; 
-(void)beat; 
@end 

B.h

@interface HelloScene : SKScene <SoundDetectDelegate> 
@property(nonatomic, strong) SoundDetect* sd; 
@property(nonatomic, strong)Song* song; 
@end 

这是一种方式代表。

我想有一些像在B.h:

@protocol SceneDelegate <NSObject> 
-(void)cancel_thread; 
@end 
@interface HelloScene : SKScene <SoundDetectDelegate> 
@property(nonatomic, strong) SoundDetect* sd; 
@property(nonatomic, strong)Song* song; 
@end 

,但我不能有@interface SoundDetect : NSObject <SceneDelegate>回历中

将变量从B传递到A的主要目的是我要发送一个变量到A,以便A可以取消它的当前线程。

感谢

+0

B到A? – 2015-01-21 05:00:58

回答

1

除了使用代表,你所要做的就是,使用块与BOOL返回类型,通知它是否应该被取消与否。

0

1-用于从传递数据到B可以使用Segue公司,并且当其代表被调用则可以传递数据从A到B这样

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"segueFromFirstToSecond"]) 
    { 
    UINavigationController *navController = (UINavigationController *)segue.destinationViewController; 
    SecondViewController *controller = (SecondViewController *)navController.topViewController; 
    controller.myValue = YES; /// myvalue is property in secondViewCOntroller 
    } 
} 

2-为选自B将数据传递到甲您可以按照以下链接

http://www.mysamplecode.com/2012/12/ios-passing-data-between-view-controllers.html

http://tomsbigbox.com/pass-data-from-modal-view-back-to-parent-in-the-ios-sdk/

0

解决方案 - 不要在.H不导入做进口的,即.mf或在.h中引用前面的声明@ class/@ protocol

in .m - 有类别要di关注。 @interface SoundDetect() @end

@protocol SoundDetectDelegate <NSObject> 
-(void)update_from_sound: (SKSpriteNode*) node; 
@end 
@interface SoundDetect : NSObject 
@property (nonatomic, weak) id <SoundDetectDelegate> delegate; 
+ (id)sharedInstance; 
-(void)beat; 
@end 

了Bh

@class SoundDetect; 
@interface HelloScene : SKScene 
@property(nonatomic, strong) SoundDetect* sd; 
@property(nonatomic, strong)Song* song; 
@end 

家蚕

#import "SoundDetect.h" 
@interface HelloScene()<SoundDetectDelegate> 
@end 

@implementation HelloScene 

-(void)update_from_sound: (id) node 
{ 

} 
@end 
要从做什么类型的,你想控制或有什么具体的