スキップしてメイン コンテンツに移動

Delegate

Delegates or Properties, take your pick.
PROPERTIES
Class A (ViewController) wants to communicate with Class B (ViewController)
Before calling Class B, do the following:
In Class B, create a @property NSString* receivedValue;
In Class A,
#import Class B
then in the method that calls B (either prepareForSegue or didSelectRowAtIndexPath or some method where you present Class B view controller, do the following:
Class B *calledVC = alloc/init
calledVC.receivedValue = ‘whatever value you want to pass’;
then call Class B VC
Done!

DELEGATES
Let’s say Class B wants to notify Class A that something has finished.  In Class B add this above your @interface:
@protocol YourDelegateProtocol <NSObject>
- (void)itemWillBePassed;
@end

Then in the regular Class B @interface
@property (nonatomic, weak) id <YourDelegateProtocol> delegate;
This means that Class B has a delegate protocol declared and anyone who wants to be its delegate needs to adopt this protocol and the methods defined herein.
So now in your Class A, right on the @interface line, add <YourDelegateProtocol> right after the:
@interface ViewController : UIViewController
 And since now Class A implements Class B’s protocol, it must implement the delegate methods, which is only itemWillBePassed in our example.
Now simply, whenever Class B needs to notify Class A, it has to call:
[self itemWillBePassed];
And Class A will know about it in its implemented delegate method!

コメント

このブログの人気の投稿

日本に来た 今三週間

四月六日に日本に来た、今三週間になった。 嬉しくなかった、悲しくなかった。 日本の携帯電話のカメラはとてもいいよ、私は今撮影が好きだ、いつも寮に帰る途中で花とか魚とか写真を撮る。これも生活の趣味だ。 滴は綺麗だ 桜が凋んで落ちる 天気がいいとき、自転車に乗って、食材を買った。 自分で料理をつくる、おいしそうでしょう。 昨日清水寺に行った、天気はとても熱いね。日に焼けすぎたので、腕は今赤い。 側に花があるので、赤い清水寺はもっと美しそうだ。 木の色は艶やかだ。