2

私はしばらくこの問題に苦しんでいますので、どんな助けでも大歓迎です。

状況は次のとおりです。私のアプリケーションには、というUIViewControllerサブクラスがありInitialViewControllerます。このビューコントローラにはUIButtonがあり、そのボタンを押すと、というNSObjectサブクラスが作成されMyEngineます。このようなもの:

@interface InitialViewController : UIViewController <MyEngineDelegate>
...
@end

@implementation InitialViewController
...
-(IBAction)pressedButton:(id)sender {
    MyEngine *engine = [[MyEngine alloc] init];
    [engine start];
}

内部では、ユーザーが選択できるようにstartViewController( )をモーダルに表示します。ConflictViewController

@interface MyEngine : NSObject <ConflictViewControllerDelegate>
...
-(void) start;
@end

@implementation MyEngine
...
-(void) start {
        ConflictViewcontroller *cvc = [[ConflictViewController alloc] initWithNibName:@"ConflictViewController" bundle:nil];
        cvc.modalPresentationStyle = UIModalPresentationFormSheet;
        cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        cvc.delegate = self;
        UIWindow *window = [(MyAppDelegate *) [[UIApplication sharedApplication] delegate] window];
        [[window rootViewController] presentModalViewController:cvc animated:YES];
}
@end

ConflictViewController本当に簡単です。ユーザーが決定するのを待つだけで、ユーザーがボタンを押すと、にメッセージを送信し、delegate自分自身を閉じます。

-(IBAction)didSelectConflict:(id)sender {
    UISegmentedControl *seg = (UISegmentedControl*) sender;
    [self.delegate didResolveConflictChoice:seg.selectedSegmentIndex];
    [self dismissModalViewControllerAnimated:YES];
}

すべての接続を確認しました。すべてのデリゲートが正常に機能しています。何が問題になっているのかというと、MyEngineがユーザーの選択を受け取った場合、その実装はdidSelectConflict:すべてのプロパティが失われているため、適切に続行できませんnull

MyEnginepresentsの場合ConflictViewController、プログラムは実行を継続し、start終了すると、プログラムに戻りpressedButton:、このメソッドが閉じられると、MyEngineオブジェクトが解放されます。

私が知りたいのは、これを回避する方法があるかどうかです。誰かがこのようなことを別の方法で行ったことがありますか?ここでの質問は、選択が複雑すぎて使用できない場合に、ユーザーの選択を適切に取得する方法UIAlertViewです。

長い質問で申し訳ありませんが、私はそれを可能な限り単純化しました。お時間をいただきありがとうございます。リンク、コメント、またはあらゆる種類のヘルプをいただければ幸いです。

4

2 に答える 2

2

MyEngine *engineMyEngineオブジェクトを使用したい場合は、IBActionで初期化するのはなぜですか。でグローバル宣言を行い、IBactionInitialViewControllerを呼び出すだけです。[engine start]次に、デリゲートメソッドが選択されたインデックスを返したら、それを最初のView Controllerのグローバルintに適用して、続行できます。それが理にかなっていることを願っています

于 2012-07-13T15:05:28.093 に答える
0

メソッドを次のように開始します

-(void) startWithDelegate:(id)del {
        ConflictViewcontroller *cvc = [[ConflictViewController alloc] initWithNibName:@"ConflictViewController" bundle:nil];
        cvc.modalPresentationStyle = UIModalPresentationFormSheet;
        cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        cvc.delegate = del;
        UIWindow *window = [(MyAppDelegate *) [[UIApplication sharedApplication] delegate] window];
        [[window rootViewController] presentModalViewController:cvc animated:YES];
}

-(IBAction)pressedButton:(id)sender {
    MyEngine *engine = [[MyEngine alloc] init];
    [engine startWithDelegate:self];
}

を実装didResolveConflictChoice:InitialViewController、そこでデリゲート呼び出しを取得します。

または、必要に応じてUIActionSheetを使用できます。

于 2012-07-13T14:53:01.333 に答える