解除多个视图控制器的委托问题

Posted

技术标签:

【中文标题】解除多个视图控制器的委托问题【英文标题】:delegation issue with dismissing multiple view controllers 【发布时间】:2012-08-03 23:15:41 【问题描述】:

我之前问过一个关于解除多个视图控制器的问题,我得到的答案以及我在其他地方找到的可能解决方案都未能达到预期的效果。我已将我的问题范围缩小到我组建代表团的方式。代码如下,我非常感谢任何反馈。

我的完整项目可以在这里下载:https://www.yousendit.com/download/TEhWckhYQVNYSHpIRHNUQw

谢谢。

//
//  QuestionViewController.h
//  learningTheRopes1
//
//  Created by James Ulle on 7/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Question.h"
#import "AnswerViewController.h"

@interface QuestionViewController : UIViewController <AnswerViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *currentQuestionDisplay;

@property (weak, nonatomic) IBOutlet UITextField *userAnswerTextField;

@property (nonatomic, strong) Question *currentQuestion;

- (IBAction)dismissKeyboard:(id)sender;

- (void)dismissQVC;

@end

    //
    //  QuestionViewController.m
    //  learningTheRopes1
    //
    //  Created by James Ulle on 7/18/12.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //

    #import "QuestionViewController.h"

    @interface QuestionViewController ()

    @end

    @implementation QuestionViewController

    @synthesize currentQuestionDisplay;
    @synthesize userAnswerTextField;
    @synthesize currentQuestion;

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    
        AnswerViewController *avc = [segue destinationViewController];
        [avc setCurrentQuestion:currentQuestion];
        [avc setUserAnswer:[userAnswerTextField text]];
    

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    [super viewDidLoad];

    [self.currentQuestionDisplay setText:[currentQuestion question]];

    // Do any additional setup after loading the view.


- (void)viewDidUnload

    [self setCurrentQuestionDisplay:nil];
    [self setUserAnswerTextField:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    return (interfaceOrientation == UIInterfaceOrientationPortrait);


- (IBAction)dismissKeyboard:(id)sender 
    [userAnswerTextField resignFirstResponder];


- (void)dismissQVC 
    NSLog(@"Dismiss QVC");
    [self.navigationController popViewControllerAnimated:NO];


@end

    //
//  AnswerViewController.h
//  learningTheRopes1
//
//  Created by James Ulle on 7/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Question.h"

@protocol AnswerViewControllerDelegate <NSObject>
- (void)dismissQVC;
@end

#import "QuestionViewController.h"

@interface AnswerViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *displayCurrentAnswer;

@property (nonatomic, strong) Question *currentQuestion;

@property (nonatomic, strong) NSString *userAnswer;

@property (nonatomic, weak) id <AnswerViewControllerDelegate> delegate;

- (IBAction)dismissAnswerVC:(id)sender;

@end

    //
//  AnswerViewController.m
//  learningTheRopes1
//
//  Created by James Ulle on 7/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AnswerViewController.h"

@interface AnswerViewController ()

@end

@implementation AnswerViewController

@synthesize displayCurrentAnswer;
@synthesize currentQuestion;
@synthesize userAnswer;
@synthesize delegate;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    [super viewDidLoad];

    if([userAnswer isEqualToString:currentQuestion.answer]) 
        [self.displayCurrentAnswer setText:@"You are correct!"];
    
    else 
        [self.displayCurrentAnswer setText:@"You are wrong!"];
    

    // Do any additional setup after loading the view.


- (void)viewDidUnload

    [self setDisplayCurrentAnswer:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    return (interfaceOrientation == UIInterfaceOrientationPortrait);


- (IBAction)dismissAnswerVC:(id)sender 
    [self dismissViewControllerAnimated:YES completion:^

        NSLog(@"Dismiss AVC");
        [[self delegate] dismissQVC];

    ];



@end

最后我的输出是这样的(这表明确实调用了完成块,但是没有发生对 dimissQVC 的委托回调:

2012-08-03 19:04:34.235
learningTheRopes1[4165:f803] Dismiss AVC

【问题讨论】:

【参考方案1】:

在 prepareForSegue 方法中,你漏掉了这一行:

avc.delegate = self;

【讨论】:

谢谢。谢谢你。谢谢你。我是 ios 新手,代表团让我难过。就像我现在想要的那样工作。

以上是关于解除多个视图控制器的委托问题的主要内容,如果未能解决你的问题,请参考以下文章

尝试在解除分配时加载视图控制器的视图... UIAlertController

MFMessageComposeVIewController 正在解除它的委托而不是它自己

具有自定义委托或数据源的视图控制器的状态保留

使用过渡委托时触发 viewWillAppear

解除控制器后未调用委托

在具有委托的视图控制器之间传递多个值