2010-02-12 67 views
0

我有一个项目,在其中的SwitchViewController是根控制器 它加载viewcontroller2在某些状态, viewcontroller2在某些状态下加载modalviewcontroller1。 有一个功能modelviewcontroller1“okButtonPressed”(断点1), 我希望它可以通知viewcontroller2并调用该函数“DoSomething的”(//断点2)协议和多视图控制器

所以,我设置了协议,所有视图控制器(switchviewcontroller ,viewcontroller2,modalviewcontroller1) 包含协议

我设置了breakpoint1和breakpoint2如下。

没有报告任何错误,但在断点2没有停止,'dosomething'没有执行。

欢迎任何评论

感谢 InterDev中

//----------------------------------------------source codes 

//myprotocol.h 
#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 


@class Myprotocol; 

@protocol MyprotocolDelegate <NSObject> 
@optional 

- (void)function1 ; 

@end 
//----------------------------------- 

@interface Myprotocol : NSObject { 

    id <MyprotocolDelegate> delegate; 
} 

@property (nonatomic, assign) id <MyprotocolDelegate> delegate; 

@end 
//myprotocol.m 


#import "myprotocol.h" 
@implementation Myprotocol 
@synthesize delegate; 

- (void)dealloc {  
    [super dealloc]; 
} 
@end 

//"ModalViewController1.h" 
#import <UIKit/UIKit.h> 

#import "myprotocol.h" 

@interface ModalViewController1 : UIViewController <MyprotocolDelegate> { 
id<MyprotocolDelegate> delegate; 
} 
@property (nonatomic, assign) id<MyprotocolDelegate> delegate; 

- (IBAction)okButtonPressed:(id)sender; 
@end 

//"ModalViewController1.m" 


#import "ModalViewController1.h" 

@implementation ModalViewController1 
@synthesize delegate; 

- (IBAction)okButtonPressed:(id)sender; 
{  
    [delegate function1];//breakpoint 1 
    [self.view removeFromSuperview];  
} 

//------ViewController2.h" 
#import <UIKit/UIKit.h> 

#import "myprotocol.h" 


@class ModalViewController1 ; 
@interface ViewController2 : UIViewController <MyprotocolDelegate>{ 
    ModalViewController1 *vModalViewController1; 
    id<MyprotocolDelegate> delegate; 

} 
@property (nonatomic, assign) id<MyprotocolDelegate> delegate; 
@property (retain,nonatomic) ModalViewController1 *vModalViewController1; 
@end 

//----ViewController2.m"-------------- 
#import "ViewController2.h" 
#import "ModalViewController1.h" 

@synthesize delegate; 

- (void)function1; 
{ 
    [self dosomething];//breakpoint 2 
} 

//SwitchViewController.h 

#import <UIKit/UIKit.h> 

#import "myprotocol.h" 


@class ViewController2; 

@class ModalViewController1 ; 


@interface SwitchViewController : UIViewController <MyprotocolDelegate> { 

    ViewController2 *vViewController2; 

}  

@property (retain,nonatomic) ViewController2 *vViewController2; 

@end 

//in SwitchViewController.m 

ViewController2 *vvViewController2=[[ViewController2 alloc] 
     initWithNibName:@"View2" bundle:nil]; 
self.vViewController2=vvViewController2; 

[vvViewController2 release]; 
[self.vViewController2 setDelegate:self]; 
+0

请格式化正确使用四个代码每行之前有空格。 – FelixLam 2010-02-12 09:57:51

回答

0

对于这个工作你需要设置你的ViewController2的实例作为委托ModalViewController1

+0

完美答案! 我希望知道是否代码 [vvViewController2 release]; [self.vViewController2 setDelegate:self]; 无用 – arachide 2010-02-12 10:51:31

+0

您确实需要[vvViewController2版本],因为您已经分配了它。我不认为你需要在ViewController2中有一个委托,但只有在ModalViewController1中 – willcodejavaforfood 2010-02-12 10:58:01

相关问题