2011-08-21 1 views
1

유틸리티 기반 응용 프로그램을 구축 중이며 데이터는 MainViewController에 저장되어 있으며 이제는 FlipsideViewController (많은 내용은이 스레드 BTW, Sending data from Mainview to Flipside?)으로 데이터를 전달하는 방법을 알고 있습니다. 그러나 나는 플립 사이드보기에 추가 한 하위 뷰 (서브 클래스 UIView)에 데이터를 가져오고 있습니다. 이 하위 뷰에 데이터를 전달하려면 어떻게해야합니까? FlipsideViewController.h에 이미 위임자와 프로토콜이 설정되어있는 것을 보았습니다. 저는 위임 일종의 일에 정말 새로운 것을 알았습니다. 어떤 도움이라도 대단히 감사하겠습니다!mainview에서 서브 뷰로 데이터를 전달합니다.


업데이트 : 주 화면에

, 나는 객체를 생성하는 입력 사용자를위한 텍스트 필드의 몇 가지있다. 모든 객체는 배열에 저장됩니다. 즉, 내 데이터가 생성되어 MainViewController에 저장됩니다. 이제 플립 측면에서, 그 배열의 데이터를 기반으로 내 자신의 그림을 할 수있는 사용자 지정 UIView 하위 클래스가 있습니다. 여기서해야 할 일은 MainViewController에 저장된 데이터를이 하위 뷰로 전달하는 것입니다. 여기 내 관련 코드입니다 : 위의 코드에서 FlipsideViewController.h

@protocol FlipsideViewControllerDelegate; 

@interface FlipsideViewController : UIViewController { 
    id <FlipsideViewControllerDelegate> delegate; 
    DataModel *receiver; //create a property to receive the data transferred from main view 
} 

@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate; 
@property (nonatomic, retain) DataModel *receiver; 
- (IBAction)done:(id)sender; 
@end 


@protocol FlipsideViewControllerDelegate 
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller; 
@end 

에서

MainViewController.m

- (IBAction)showInfo:(id)sender {  

    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil]; 
    controller.delegate = self; 

    controller.receiver = data;//this is what I've done. 

    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:controller animated:YES]; 

    [controller release]; 
} 

에서 "데이터"는 MainViewController.h 파일에 선언 된 DataModel이 개체입니다.

drawing 클래스 (UIView의 하위 클래스)에서 사용자 정의 드로잉을 수행하고 싶습니다. FlipsideViewController의 데이터를이 서브 뷰로 전달하는 방법은 무엇입니까? FlipsideViewController.h 파일에 선언 된 대리인을 사용해야합니까? 미리 감사드립니다!

+0

좀 더 구체적으로 말씀해 주시겠습니까? 어떤 종류의 데이터를 전달 하시겠습니까? – matteodv

+0

업데이트되었습니다. – Michael

답변

1

템플릿을 빠르게 살펴본 결과 대리인이 사용중인 것과 혼동스러워하고 있다고 생각합니다.

이 템플릿의 대리인은 데이터를 전송하지 않습니다. 완료 버튼을 클릭하면 MainViewController으로 다시 전화하여 dismissModalViewControllerAnimated 메서드를 호출하여 뷰 컨트롤러를 제거 할 수 있도록 요청합니다. 이것은 문서화 상태처럼 약간 초라한 것처럼 보입니다.

If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller. 

따라서 부모에게 전화 할 필요가 없습니다.

인터페이스 빌더에서 FlipsideView.xibFile's OwnerFlipsideViewController.xib으로 설정되어 있습니다.

enter image description here

이제 마우스 오른쪽 버튼을 클릭하면 File's Owner 당신이 view이 기본적으로 viewFlipsideViewController에서 속성의 이름이고 View은 인터페이스 빌더의 요소는 것을 의미 View에 연결되어있는 것을 볼 수 있습니다.

enter image description here

거기서 콘센트를 사용 FlipsideViewController에서 XIB 파일의 요소에 액세스 할 수있다.당신이

먼저 다시 인터페이스 빌더에서 다음

// FlipsideViewController.h 
@interface FlipsideViewController : UIViewController 

@property (nonatomic, retain) IBOutlet UILabel *testLabel; // <----- Added this 
@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate; 

- (IBAction)done:(id)sender; 

@end 

// FlipsideViewController.m 
@implementation FlipsideViewController 

@synthesize delegate = _delegate; 
@synthesize testLabel = _testLabel; // <----- Added this 

// More methods 

- (void)dealloc 
{ 
    [_testLabel release]; // Always do you memory management 
    [super dealloc]; 
} 

같이하는 .m에 .H에 속성을 추가하고 합성 몇 가지를 수행해야합니다 라벨을 그릴 말을하려면

  1. File's Owner톤에서보기
  2. ctrl + dragUILabel 요소를 추가합니다 UILabel O를 당신은
  3. 가 제대로 testLabel

이제이가 매혹 내 예에서 레이블을 선택했다. 당신이 레이블의 값을 설정해야 할 장소는 지금은 직접의 데이터를에 의해 설정되는 다른 하나 개의보기에서 데이터를 전달하는 가장 쉬운 방법을 찾아이

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.testLabel.text = @"It Works"; // You would use the data passed in from `MainViewController` 
} 
+0

안녕하세요, 답장을 보내 주셔서 감사합니다. 아직 조금 혼란 스럽습니다. 내 사용자 정의보기는 펜촉에서로드 될 때 플립보기의 인스턴스입니다. initWithCoder() 메서드에서 데이터를 전달해야한다고 생각하십니까? flipviewcontroller에서 addSubview 메서드를 호출하는 대신? – Michael

+0

죄송합니다. 템플릿을 너무 오랫동안 보지 않아서 답변을 업데이트 할 예정입니다. –

+0

FlipsideViewControllerDelegate 프로토콜의 mainviewcontroller에서 사용되는 - (void) flipsideViewControllerDidFinish와 같은 메소드를 선언 할 수 있습니까? 그런 다음 데이터를 가져 오기 위해이 새 메서드를 하위 뷰에서 호출합니다. – Michael

0

같이 할 수있는 viewDidLoad:에 원래보기에서 다음보기.

예 :

FlipsideViewController.h에서 전달할 데이터의 '컨테이너'를 선언하십시오. 즉, 제대로 작동하려면 양쪽에서 동일한 클래스 여야합니다. NSArray에 NSArray, NSMutableDictionary에 NSMutableDictionary.

NSMutableArray *newData; 
... 
@property (nonatomic, retain) NSMutableArray *newData; // This allows you to access this object from outside this class. 

@synthesize newData; 
... 
[newData release]; 

FlipsideViewController.m

에서

이제 우리는 말하자면, 데이터를 전달해야합니다. 우리가 보내려는 데이터가 NSMutableArray에 '결과'라고 저장되어 있다고 가정 해 봅시다.

MainViewController.m에서 다음 뷰 컨트롤러 (이 경우 FlipsideViewController)를 인스턴스화 할 때 우리가 펜촉을 초기화 한 후에 newData 변경 가능 어레이를 직접 참조 할 수 있습니다.

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil]; 
controller.newData = results; 
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
[self presentModalViewController:controller animated:YES]; 
[controller release]; 

MainViewController.h 파일에서 FlipsideViewController를 가져 오는지 확인하십시오.

.h 파일에서 속성을 선언하면보기 스택 내 어디에서나 개체의 내용을 참조 할 수 있습니다.

도움이 되길 바랍니다. D

+0

내 게시물에서 언급 한 것처럼 메인보기에서 플립 사이드보기로 데이터를 보내는 데 아무런 문제가 없습니다.내가하고 싶은 일은 flipsideview 위에 만들어진 사용자 정의보기로 데이터를 보냅니다. – Michael