2

さて、基本的には、アプリケーションのメイン メニューであるメイン ビュー コントローラーがあります。ユーザーをテーブル ビューに移動するボタンがあり、メイン ビューに適用する必要があるものを選択します。問題は、メイン ビュー コントローラーがセグエから作成されているのか、アプリケーションの開始から作成されているのかを判断する方法がわからないことです。これを確認する方法はありますか?viewDidLoad メソッドがチェックするブール値または文字列を設定してから、prepareForSegue で変更する必要がありますか?

4

2 に答える 2

3

それで、あなたが何であるかをよりよく理解したので、より完全な答えを与えることができます. 本当に探しているのは、デリゲートとプロトコルに関連するパターンです。これは、親 (またはデリゲート) コントローラーに関する実際の詳細を知る必要なく、viewController 間でデータを送信する方法です。これはあなたがやりたいことです。

わかりやすくするmainViewControllerために、ルート コントローラーとtableViewControllerUITableViewController サブクラスのインスタンスの 2 つの名前をコントローラーに使用します。

.h。あなたtableViewControllerのプロトコルをセットアップしたいと思うでしょう:

@protocol SingleSelectionDelegate
- (void)selectionHasBeenChosenWithOption:(NSString *)selection;
@end

@interface MyTableViewControllerSubclass : UITableViewController
// properties and method declarations

// this is your property you will use to send data back to your delegate (in this case your mainViewController)
@property (weak, nonatomic) id<SingleSelectionDelegate> selectionDelegate;
@end

次に、.m(または.h)にmainViewControllerこれを追加する必要があります:

// this imports the interface and also the protocol that you want
#import "MyTableViewControllerSubclass.h"

// the <Single...> part says you conform to this protocol and implement the methods it requires (in this case selectionHasBeenChosenWithOption:)
@interface MainViewController <SingleSelectionDelegate>
@end

@implementation MainViewController

// here is where you need to implement the required method
- (void)selectionHasBeenChosenWithOption:(NSString *)selection {
    // do what you want with the selection, assign it to a property, call other methods, pass it to other delegates, or whatever else.

    // now that you have your information you want the focus to come back to you so you
    [self.navigationController popToViewController:self animated:YES]; // works even if not the root
}


// you also need to set yourself as tableViewController's selectionDelegate, if you are using Storyboards you will do this in the prepareForSegue.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.destinationViewController isKindOfClass:[MyTableViewControllerSubclass class]]) {
    // you could also check a specific segue's identifier but this makes sense because if it is this subclass then it will have a selectionDelegate property

    // assign yourself as the delegate
    MyTableViewControllerSubclass *destination = (MyTableViewControllerSubclass *)segue.destinationViewController
    destination.selectionDelegate = self;
}

@end

次に、selectionDelegate の情報を取得したら、最後の手順としてデリゲート メソッドを呼び出します。これは で行われtableViewControllerます.m。この場合、私はそれを行いますtableView:didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // getting the data you want to send back
    NSString dataToSendBack = [self.myArray[indexPath.row] description];

    // sending the data to your delegate (in this case mainViewController)
    [self.selectionDelegate selectionHasBeenChosenWithOption:dataToSendBack];

    // mainViewController is handling the popping so we do not need to do anything with the navigation stack
}

それが基本的にあなたが求めているものです。ここではほとんどすべてをテキスト エディターで入力したため、構文エラーが発生する可能性があります。私に知らせてください、私はそれらを修正することができます. これは、あらゆる場所で使用することになるため、iOS 開発で非常に重要な概念です。それをよく学び、他のチュートリアルも探すことができます。私が言ったように、あなたはそれらのトンを見つけるでしょう!初めて学んだときは圧倒されたのを覚えていますが、今では当たり前のようになっています。

于 2013-06-28T00:58:54.530 に答える
1

をチェックすることもできますが[UIApplication sharedApplication] delegate] window] rootViewController]、おそらくプロパティを使用するだけです。

于 2013-06-27T22:09:29.070 に答える