2011-04-28 10 views
0

我有一個叫做controller的父視圖:AskHome和一個名爲* record_audio *的子視圖。 AskHome顯示模態recod_audio,我實現了一個委託protocole,以便我可以發回到父視圖(AskHome)一個NSNumber變量,我在record_audio(子視圖)中收到,但委託方法根本不會被調用:ChildView不會調用我的委託方法!

所以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

已切開;我忘了nick.delegate = self;當我打電話給childView – john 2011-04-28 21:59:38

回答

1

你在哪裏設置record_audio的代表?這可能是它沒有被設置的問題。 (調試思想並確保代表是有效的對象)

此外,委託方法稱爲'getWhatever'不是最佳做法。您的設計模式可能存在缺陷,可能最適合單身人士。

+0

什麼是record_audio委託?我所展示的是我在代碼中所擁有的一切......缺少一些東西? – john 2011-04-28 16:14:22

+1

@john在你的record_audio_testViewController接口中有「id 委託;」。你在哪裏設置這個代表? – albertamg 2011-04-28 16:27:55

+0

就在@interface – john 2011-04-28 19:01:33