ObjectAを作成するViewControllerがあります。次に、ObjectAはObjectBを作成します。ObjectBは、View Controllerからいくつかの値を取得する必要があります(オブジェクトの作成後に変更できる値が必要です)。
値を取得するための最良の方法を見つけようとしています。私が最初に考えたのはプロトコルです。これまで、オブジェクト内にプロトコルを作成しただけです。
#import <UIKit/UIKit.h>
@protocol AnalysisTypeTableDelegate;
@interface AnalysisTypeTableViewController : UITableViewController
@property (weak, nonatomic) id <AnalysisTypeTableDelegate> delegate;
@property (strong, nonatomic) NSArray *dataSource;
@property (nonatomic) BOOL allowRefresh;
@end
@protocol AnalysisTypeTableDelegate <NSObject>
-(void)returnAnalysisType:(NSString *)type;
@end
プロトコルクラスを作成するにはどうすればよいですか(新しいファイルの作成、Objective-Cプロトコルなど)?
では、どうすればそれらをリンクできますか?ObjectAにはプロトコルがありますが、次のようなことをしますか?
// View controller and objectB both conform to myProtocol
// view controller creates objectA
myObjectA.delegate = self
// when objectA creates objectB
myObjectB.delegate = self.delegate
または、必要な値を取得するためのより良い方法はありますか?
編集:
私はこのようなことをする必要があると思います:
objectAのプロトコル:
@protocol objectADelegate
-(NSDictionary)requestViewController;
@end
objectBのプロトコル:
@protocol objectBDelegate
-(NSDictionary *)requestObjectA;
-(void)updateList:(NSSarray *)list;
@end
myObjectBで
-(NSDictionary *)requestObjectA {
NSDictionary *extents = [self.delegate requestObjectA];
}
-(void)serverCall {
// make server call, get list
...
// update myObjectA with new list
[self.delegate updateList:newList];
myObjectAで
-(NSDictionary *)requestObjectA {
return [self.delegate requestViewController];
}
-(void)updateList:(NSArray)list {
// updates list
}
ビューコントローラで
-(NSDictionary *)requestViewController;
return self.mapkit.exents;
}