0

OK、ここで私はここ数時間把握しようとしてきた奇妙な問題を抱えています:

FirstViewControllerメインのView ControllerとModalViewControllerモーダルセグエに表示される2つのView Controllerを備えたiPad用のシンプルなストーリーボードアプリがあり、Doneというボタンがあります。私はARCを使用しており、View ControllerはiPad用に指定されています。

UIModalViewControllerDelegateご想像のとおり、モーダルを閉じてデータを最初のView Controllerに戻すカスタムデリゲートもあります。

UIModalViewControllerDelegate.h

@protocol UIModalViewControllerDelegate <NSObject>
@required
-(void)btnDonePressed:(id)sender Values:(NSArray *)values;
@end

FirstViewController.h

@interface FirstViewController : UIViewController <UIModalViewControllerDelegate> {    
@private
    ModalViewController *mvc;
}
@end

FirstViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
// Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    mvc = [segue destinationViewController];
    mvc.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma UIModalViewControllerDelegate

-(void)btnDonePressed:(id)sender Values:(NSArray *)values {
        ...
}

ModalViewController.h

@interface ModalViewController : UIViewController {
    __weak id<UIModalViewControllerDelegate> delegate;
}

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

ModalViewController.m

@implementation ModalViewController

@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];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonPressed:)];
}

-(void) buttonPressed:(id) sender {
    [delegate btnDonePressed:sender Values:values];
}

さて、非常に興味深い問題がいくつかあります:

1-セグエの準備でself.mvc.delegate = self;、デバッガーを設定した後、デリゲートがまだnilであることを示しています(!)ただし、使用するNSLog(@"%@", self.mvc.delegate);と、nilではないポインターのアドレスが取得されます。

ModalViewController2-デリゲートでは常にnilであるため、上記のコードは機能しません。したがって、[delegate btnDonePressed:sender Values:values];決して実行されません。この問題を引き起こす可能性があるとわかっていることはすべて試しましたが、何もうまくいかないようです。

3-デリゲートに強い参照を持つことでこの問題を解決できると思いますが、パターンに違反して保持サイクルの問題を引き起こしたくありません。プライベート変数ModalViewController *mvc;FirstViewControllerライフサイクルに問題はありますか? プライベート変数はいつ nil になりますか? ところで、私もそれを置き換えようとしました@property (strong, nonatomic) ModalViewController *mvc;が、何も変わりませんでした。

4

2 に答える 2

0

答えてくれてありがとう。私はついにこの問題の解決策を見つけました。

実際、コードは完全に正しいです。問題は、UINavigationControllerModalViewControllerが組み込まれていることです。

それ以外の

myModalWindowViewController = [segue destinationViewController];
myModalWindowViewController.delegate = self;

そうです

UINavigationController *navigationController = [segue destinationViewController];
myModalWindowViewController = (MyModalWindowViewController *)[navigationController.viewControllers objectAtIndex:0];
myModalWindowViewController.delegate = self;

NavigationControllerに埋め込まれているViewControllerにセグエするとき、DestinationViewControllerがNavigationControllerであり、ViewControllerではないことを知りませんでした。私の悪い!

于 2013-01-22T09:17:04.020 に答える
0

「ここに間違ったバージョンをコピーしました。ただし、問題は解決しません。(上記のコードを更新しました)」

あなたはコピペについて言及しているので。元のコードに が含まれていることを確認してください@synthesize。この問題は、合成されたプロパティとプライベート変数名の不一致のように見えます。

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

と組み合わせて宣言されたプロパティは、@synthesizeという名前のプライベート変数を使用しますdelegate。誤っ@synthesizeて名前の付いたプライベート変数を省略した場合は、_delegate自動的に作成されます (アンダースコアに注意してください)。

buttonPressed:メソッドでは、プライベート変数delegateに直接アクセスします。したがって、アクセサーは実際にはアンダースコア付きのバージョンを使用するため、常に nil を返す可能性があります。

念のために、手動で宣言された変数を削除するか、メソッドでdelegate使用self.delegateしてくださいbuttonPressed:。何が起こるか見てください。

于 2013-01-18T17:34:14.557 に答える