协议和多视图控制器

时间:2010-02-12 09:57:10

标签: iphone

我有一个项目,其中SwitchViewController是根控制器 它以某种状态加载viewcontroller2, viewcontroller2在某种状态下加载modalviewcontroller1。 modelviewcontroller1(断点1)中有一个函数'okButtonPressed', 我希望它可以通知viewcontroller2并调用函数'dosomething'(// breakpoint 2)

所以,我设置了一个协议,所有的viewcontroller(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];

1 个答案:

答案 0 :(得分:0)

要使其正常工作,您需要将ViewController2的实例设置为ModalViewController1的委托