티스토리 뷰
- 부모 뷰 컨트롤러 : ParentViewController
- 자식 뷰 컨트롤러 : ChildViewController
로 정의하고 시작.
ParentViewController.h :
ParentViewController.m :
ChildViewController.h :
ChildViewController.m :
- 자식 뷰 컨트롤러 : ChildViewController
로 정의하고 시작.
- 부모에 delegate @protocol 선언
- 자식에서 같은 @protocol 코딩
- 자식에서 부모에서 선언한 delegate 따른다고 선언
- 자식에서 해당 delegate를 통하여 부모의 object 제어
ParentViewController.h :
#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 :
#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 :
#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 :
#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/
'모바일 > iOS' 카테고리의 다른 글
| 모달뷰에 네비게이션 상단바 붙여 보이기 (0) | 2012.01.31 |
|---|---|
| SNS공유 라이브러리 ShareKit 0.2.1 iOS5 에서 Cancel 버튼 안먹는문제 해결 (0) | 2012.01.30 |
| UITableViewCellStyle & 구성요소 (0) | 2012.01.20 |
| Attribute Unavailable: Defines Presentation Context is not available prior to Xcode 4.2. (0) | 2012.01.18 |
| 테이블셀 오른쪽 끝 > 나타내기 (0) | 2012.01.18 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Linux
- IOS
- git
- macos
- JSON
- javascript
- 한글
- PHP
- Mac
- ASP
- nodejs
- CSS
- Wordpress
- IE
- classic asp
- JQuery
- API
- Docker
- Debug
- iphone
- Prototype
- laravel
- sencha touch
- mssql
- Android
- iis
- 워드프레스
- nginx
- 안드로이드
- centos
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
글 보관함