1

iPhoneアプリ開発初心者です。
Objective-C++ と std CPP を使用して、iPhone エミュレータ用のサンプル アプリケーションを 1 つ開発しています。

私のアプリケーションには 2 つのビューがあります。CPP コードからのいくつかのイベントで、最初のビュー コントローラーの次のコードを使用して 2 番目のビューを表示しています。

// Defined in .h file 
secondViewScreenController *mSecondViewScreen;

// .mm file Code gets called based on event from CPP (common interface function between Objective-C++ and CPP code)
mSecondViewScreen = [[secondViewScreenController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:mSecondViewScreen animated:YES];

画面に 2 番目のビューが表示されますが、最初のビュー コントローラーから 2 番目のビュー コントローラーを終了/削除できないという問題があります。

2番目のView Controllerのポインターを使用するか、他の方法を使用して、最初のView Controllerから2番目のView Controllerを削除するにはどうすればよいですか。

2番目のビューを削除するには、2番目のビューコントローラーファイルに次のコードがあり、2番目のビューのボタンクリックイベントで呼び出されます。

// In .mm of second view controller. 
- (IBAction)onEndBtnClicked:(UIButton *)sender
{
   [self dismissModalViewControllerAnimated:NO];
   [self.navigationController popViewControllerAnimated:YES];
}

上記のコードは完全に機能します。秒ビューの終了ボタンをクリックすると、2 番目のビュー コントローラーが画面から削除され、最初のビューに移動します。同じコードを使用して最初のビュー コントローラーから 2 番目のビューを削除するにはどうすればよいですか。

NSNotificationCenter関数を呼び出すために、最初のビューから2番目のビューにイベントを送信するために使用しましたが、機能onEndBtnClickedしていません。

それを行う適切な方法は何ですか?

OSX バージョン: 10.5.8 および Xcode バージョン: 3.1.3

4

2 に答える 2

4

secondViewController で、次のようなプロトコルを作成します。

@protocol SecondViewScreenControllerDelegate <NSObject>

- (void)secondViewScreenControllerDidPressCancelButton:(UIViewController *)viewController sender:(id)sender;

// Any other button possibilities

@end

次に、secondViewController クラスにプロパティを追加する必要があります。

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

これを secondViewController 実装で合成します。

@synthesize delegate = _delegate;

最後に、firstViewController にプロトコルを実装し、提示する前に secondViewController を適切に設定するだけです。

@interface firstViewController : UIViewController <SecondViewScreenControllerDelegate>

...

@implementation firstViewController

    - (void)secondViewScreenControllerDidPressCancelButton:(UIViewController *)viewController sender:(id)sender
    {
         // Do something with the sender if needed
         [viewController dismissViewControllerAnimated:YES completion:NULL];
    }

次に、最初から secondViewController を提示する場合:

UIViewController *sec = [[SecondViewController alloc] init]; // If you don't need any nib don't call the method, use init instead
sec.delegate = self;
[self presentViewController:sec animated:YES completion:NULL];

そして準備ができました。最初から secondViewController を却下したいときはいつでも、次のように呼び出します: (secondViewController 実装内で)

[self.delegate secondViewScreenControllerDidPressCancelButton:self sender:nil]; // Use nil or any other object to send as a sender

最初から使用できる secondViewController のポインターを送信するだけです。その後、問題なく使用できます。C++ は必要ありません。Cocoa では、C++ は必要ありません。ほとんどすべてが Objective-C で実行でき、より動的です。

于 2012-12-24T12:53:24.913 に答える
0

アプリケーションにビューが 2 つしかない場合は、

- (IBAction)onEndBtnClicked:(UIButton *)sender
{
   [self dismissModalViewControllerAnimated:NO];
}

以下の行を削除します。

 [self.navigationController popViewControllerAnimated:YES];

2番目のビューを閉じているので、最初のビューから削除したいのはなぜですか。

于 2012-12-24T12:38:25.623 に答える