我有一個項目,在其中的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];
請格式化正確使用四個代碼每行之前有空格。 – FelixLam 2010-02-12 09:57:51