tags:

views:

24

answers:

1

Hello everyone

I have a project in which the SwitchViewController is root controller It loads the viewcontroller2 in some status, viewcontroller2 load modalviewcontroller1 in some status. There is one function 'okButtonPressed' in modelviewcontroller1(breakpoint 1), I hope it can notify viewcontroller2 and call the function 'dosomething'(//breakpoint 2)

So, I set a protocol, all viewcontroller(switchviewcontroller,viewcontroller2,modalviewcontroller1) contain the protocol

I set breakpoint1 and breakpoint2 as below.

There is no any error reported, but no stop at breakpoint 2, 'dosomething' was not executed.

Welcome any comment

Thanks 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];
A: 

For this to work you need to set your instance of ViewController2 as the delegate for ModalViewController1

willcodejavaforfood
perfect answer!I hope to know if codes [vvViewController2 release]; [self.vViewController2 setDelegate:self];are useless
You do need the [vvViewController2 release] since you have allocated it. I do not think you need to have the a delegate in ViewController2 though, only in ModalViewController1
willcodejavaforfood