频道栏目
首页 > 资讯 > IOS > 正文

iOS 协议的继承

13-12-23        来源:[db:作者]  
收藏   我要投稿

今天看了一个pdf文档,了解到了 协议的继承,特别做了个小demo测试了一下,下面是代码


OneDelegate.h

@protocol OneDelegate 

@required

- (void)backStr:(NSString *)str;

@end


TwoDelegate.h

@protocol TwoDelegate 

@optional

- (void)backInt:(int)i;

@end

ViewController.h

#import "ViewController2.h"

@interface ViewController : UIViewController 

@end


ViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
	
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 50, 200, 100);
    button.backgroundColor = [UIColor yellowColor];
    [button setTitle:@"into VC2" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)push{
    ViewController2 *vc = [[ViewController2 alloc]init];
    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:^{
    
    }];
}

//OneDelegate
- (void)backStr:(NSString *)str{
    NSLog(@"%@",str);
}

//TwoDelegate
- (void)backInt:(int)i{
    NSLog(@"%d",i);
}

ViewController2.h
#import 
#import "two.h"

@interface ViewController2 : UIViewController

@property(nonatomic ,unsafe_unretained)id delegate;

@end

ViewController2.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
	UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 50, 200, 100);
    button.backgroundColor = [UIColor yellowColor];
    [button addTarget:self action:@selector(backStrBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.frame = CGRectMake(50, 200, 200, 100);
    button2.backgroundColor = [UIColor blueColor];
    [button2 addTarget:self action:@selector(backIntBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
}

- (void)backStrBtn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(backStr:)]) {
        [self.delegate backStr:@"这个是代理1的方法"];
    }
}

- (void)backIntBtn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(backInt:)]) {
        [self.delegate backInt:2];
        NSLog(@"这是继承的方法");
    }
}


相关TAG标签
上一篇:android布局管理器之TableLayout
下一篇:coco2d-x核心类之CCScene与CClayer
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站