解雇多个视图控制器的委托问题 [英] delegation issue with dismissing multiple view controllers

查看:121
本文介绍了解雇多个视图控制器的委托问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾问过一个有关解雇多个视图控制器的问题,我给出的答案以及在其他地方找到的可能的解决方案都未能达到预期的效果.我已经以组建我的代表团的方式将问题缩小到某种程度.代码在下面,我非常感谢您的反馈.

I asked a question about dismissing multiple view controllers previously and the answers that i was given along with the possible solutions i found elsewhere have all failed to achieve the desired effect. i have narrowed down my issue to something with the way i set up my delegation. the code is below and i would really appreciate any feedback.

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

my full project can be downloaded here: 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并没有发生:

and finally my output is this (which shows that the completion block in indeed called, but the delegate call back to dimissQVC does not happen:

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

推荐答案

在prepareForSegue方法中,您错过了这一行:

In the prepareForSegue method, you missed this line:

avc.delegate =自我;

avc.delegate = self;

这篇关于解雇多个视图控制器的委托问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆