我有一个名为controller的父视图: AskHome 和一个名为* record_audio *的子视图。 AskHome显示modally recod_audio,我实现了一个委托protocole,这样我就可以向父视图(AskHome)发送一个我在record_audio(子视图)中收到的NSNumber变量,但是根本没有调用委托方法: / p>
所以chidView首先
* record_audio.h *
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
@protocol sendbackQuestionIdDelegate <NSObject>
@required
- (void)getIdDelegate:(NSNumber *)theQstId;
@end
@interface record_audio : UIViewController <AVAudioRecorderDelegate> {
//my stuff here
//......
id <sendbackQuestionIdDelegate> delegate;
}
@end
* record_audio.m *
#import "record_audio.h"
@implementation record_audio
@synthesize actSpinner, btnStart, btnPlay, btnCancel, btnValidate, delegate;
/* I do some stuff, upload the sound etc....
....
....
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//i call the delgate, theAnswer is the string i received from the server as a response
[delegate getIdDelegate:(NSNumber *)[theAnswer intValue]];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//just dismiss this child view
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
父视图:
AskHome.h
#import <UIKit/UIKit.h>
#import "record_audio.h"
@interface AskHome : UIViewController <sendbackQuestionIdDelegate>
{
//stuff here
}
//other stuff here
@end
AskHome.m
#import "AskHome.h"
#import "UIImage+Resize.h"
#import "record_audio.h"
@implementation AskHome
- (void)getIdDelegate:(NSNumber *)theQstId
{
NSLog(@"- ========= === <<<<<< The delegate method was called >>>>>>>>>> - ========= === ");
questionId = theQstId;
}
在控制台中我没有上面的NSLog,这意味着我的代表没有被调用,为什么?有人有想法吗?谢谢你们:)
答案 0 :(得分:1)
你在哪里设置record_audio的代表?这可能是它没有设定的问题。 (调试思考并确保委托是有效对象)
将代理方法称为“getWhatever”也不是最好的做法。您的设计模式可能存在缺陷,可能最适合单身人士。