본문으로 바로가기

ViewController.h


@protocol MainDelegate

- (void)didReceiveTest:(NSString *)message1 Message2(NSString *)message2;

@end


@interface ViewController : UIViewController <MainDelegate>




ViewController.m


// SubView로부터 받은 값 셋팅

- (void)didReceiveTest: (NSString *)message1 Message2(NSString *)message2{

    NSLog(@"SubView에서 호출");

    //객체 컨트롤 코드

}


//( addSubview ) 부모 웹뷰 자바스크립트 함수 호출이 안됨

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

appDelegate.moveUrl = URL; //SubView 에 웹뷰를 사용하기 때문에-

                

CGRect screenRect=[[UIScreen mainScreen]bounds];

CGFloat deviceWidth=screenRect.size.width;

CGFloat deviceHeight=screenRect.size.height;

                 

[_popUpViewController.view setFrame:CGRectMake(0, 0, deviceWidth, deviceHeight)];

_popUpViewController=[[PopView alloc]initWithNibName:@"PopView" bundle:nil];

_popUpViewController.delegate = self; //부모 오브젝트 컨트롤을 위해 딜리게이트 적용

[self.view addSubview:_popUpViewController.view];




PopView.h


@protocol MainDelegate;


@interface PopView : UIViewController <UIWebViewDelegate>{

    __unsafe_unretained id<MainDelegate> delegate;

}


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





PopView.m


#import "ViewController.h"


@implementation PopView

@synthesize delegate;



// MainView의 함수 호출

[delegate didReceiveTest:value01 Message:value02];






참고원문 


- 부모 뷰 컨트롤러 : ParentViewController
- 자식 뷰 컨트롤러 : ChildViewController
로 정의하고 시작.

  1. 부모에 delegate @protocol 선언
  2. 자식에서 같은 @protocol 코딩
  3. 자식에서 부모에서 선언한 delegate 따른다고 선언
  4. 자식에서 해당 delegate를 통하여 부모의 object 제어


ParentViewController.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
#import <UIKit/UIKit.h>
 
@protocol ParentViewDelegate
    - (void)didReceiveMesssge:(NSString *)message;
@end
 
@interface ParentViewController : UIViewController <parentViewDelegate>
 
@property (nonatomic, retain) IBOutlet UILabel *myMessage;
 
-(IBAction)showChildWithDelegate:(id)sender;
 
@end


ParentViewController.m :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import "ParentViewController.h"
#import "ChildViewController.h"
......
// 모달 창 띄우기
-(IBAction)showChildWithDelegate:(id)sender {
    ChildViewController *childView = [[[ChildViewController alloc] init] autorelease];
    childView.delegate = self;
    [self presentModalViewController:childView animated:YES];
}
 
// 자식창으로부터 받은 값 셋팅
- (void)didReceiveMessage: (NSString *)message {
    myMessage.text = message;
}
......


ChildViewController.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import <UIKit/UIKit.h>
 
@protocol ParentViewDelegate;
 
@interface ChildViewController : UIViewController {
    id<parentViewDelegate> delegate;
    UIButton *closeButton;
}
 
@property (nonatomic, assign) id<parentViewDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIButton *closeButton;
 
- (IBAction)closeView:(id)sender;
 
@end


ChildViewController.m :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#import "ChildViewController.h"
#import "ParentViewController.h"
 
@synthesize closeButton;
@synthesize delegate;
 
- (IBAction)closeView:(id)sender {
    // delegate 통하여 부모에서 선언한 메쏘드에 메세지 전달
    [delegate didReceiveMessage:@"Hello World"];
    // 자신(자식창) 닫기
    [self dismissModalViewControllerAnimated:YES];
}
 
......
 
- (void)dealloc {
    [closeButton release];
    [super dealloc];
}

참고: http://timneill.net/2010/11/modal-view-controller-example-part-2/