2012-07-09 3 views
1

사용자가 '추가'버튼을 누르면 모달 뷰가 팝업되어 정보를 입력합니다. 탐색 막대의 왼쪽 상단에 '취소'버튼이 있으며 눌렀을 때 현재보기 컨트롤러를 닫으려고합니다. 개체를 클래스의 대리자로 설정하려면 어떻게해야합니까? 나는 프로토콜을 만들고 그 메소드를 구현하는 것을 이해하지만, 위임자를 설정할 수는 없다. 디버거를 실행할 때 '추가'보기 컨트롤러에서 내 [self delegate]은 항상 nil입니다.Xcode의 위임

답변

0

당신은 당신의 스토리 보드에서 설정 SEGUE을 통해 모달의 ViewController을 산란하고 있습니까? 그렇다면 prepareForSegue: 메서드에서 대리자를 설정합니다. 당신이 코드를 통해 완전히 모달의 ViewController를 설정하는 경우, 다른 한편으로

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([identifier isEqualToString:@"userGuideSegue_home"]){ 
     UserGuideViewController* vc = segue.destinationViewController; 
      [[segue destinationViewController] setDelegate:self]; 
    } 

} 

는, 당신은 다음 모달의 ViewController의 인스턴스를 만들고 그것의 대리자를 설정합니다. 이 모든 당신을지지 물론

- (void)showModelView:(NSString*)viewName 
{ 
    // code ripped out of project so a bit specific 

    if ([viewName isEqualToString:@"userGuide_name"]) { 
     modalViewController = (UserGuideViewController *) 
     [[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:NULL] 
     instantiateViewControllerWithIdentifier:@"UserGuide"]; 
    } 

    modalViewController.delegate = self; 
    modalViewController.modalPresentationStyle = UIModalPresentationFormSheet; 
    modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    modalViewController.view.frame = [[UIScreen mainScreen] applicationFrame];; 


    [self presentViewController:modalViewController 
         animated:YES 
        completion:^{ 
         //put your code here 
        }]; 
} 

은 모달의 ViewController에 delegate 속성을 정의했습니다.

+0

감사의 버튼 취소 탭 이벤트에 [[self delegate] dismissModalViewControllerAnimated:YES]를 호출 할 수 있습니다! 당신의 설명이 정말 도움이되었습니다. –

0

IB에서보기를 만든 경우, 단추를 ViewController의 헤더 파일로 Control- 드래그하고 IBOutlet을 추가하십시오. 하는 .m 파일에 메소드의 내부는

[self dismissModalViewControllerAnimated:YES];

는 다른 방법으로 프로그래밍 방식으로 버튼을 생성 할 수 있습니다

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(processCancel:)]; 

-(void)processCancel:(UIBarButtonItem *)item{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 
0
@interface MyViewController : UIViewController { 
    id delegate; 
} 
@property (nonatomic,retain) id delegate; 
@synthesize delegate; 

이 작업을 수행해야합니다, 당신은 지금 모달 뷰를 표시하기 전에 [MyViewController setDelegate:self]를 사용하고 MyViewController