Protocols, delegate

In Objective-C, protocol is simply a name for the groups of methods. A protocol doesn't have instance variables, and it doesn't implement any methods it declares. It just says, any object that conforms to this protocol must implement methods X, Y, and Z.

i.g.

Object A, and B

make A the delegate of B, so

A <XXDelegate>,    in B, we declare id <XXDelegate> delegate; and assign delegate = a.




.h

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




.m

- (void)Cancel

{

    [self.delegateaddItemViewControlDidCancel:self];

}


- (void)Done

{

    ChecklistsItem *item = [[ChecklistsItemalloc]init];

    item.text =self.textField.text;   //!!Get the text from UI

    item.checked = NO;

    

    //close the window .  let the delegate do it

    [self.delegateaddItemViewControlDidDone:selfdidFinishAddingItem:item];

}





Set the delegate pp125

In A's delegate method, when we have reference to B, we will set like this:

b.delegate = self;


Set delegate in delegate

Any time you want one part of your app to notify another part about something, usually in order to update the screen, you want to use delegate. It's the iOS way.

    //invoked by UIKit when a segue from one screen to another is about to be performed

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    if ([segue.identifierisEqualToString:@"AddItem"]) {

        UINavigationController *navigationController = segue.destinationViewController;

        AddItemViewController *controller = (AddItemViewController *)[navigationControllertopViewController];

        controller.delegate = self;

    }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值