补充:(昨天晚上,我遇到一个问题,一直没有解决,因而找本身的笔记,发现本身的笔记,很不全面,不少漏点;看到本身的笔记作成这样,我感到很气愤。回去的时候,我就想,本身必定要把这笔记作详细,无论多简单的,都要作一个完整的记录、注释,不要偷懒!)
时间:2015年07月07日
//—————————————————---------------------委托方--------------------------------------------------
// BViewController.h
// 星座运程App
#import <UIKit/UIKit.h>
@class BViewController;//声明BViewController是一个类,由于在协议中要使用它,但它的定义还在协议的下面,所以就要预声明
//一、定义协议
@protocol BViewControllerDelegate <NSObject>//继承NSObject就能够了
//第一个参数必定是委托方,这样就好区分这个委托是谁的,而且可能还要用到它,这里是BViewController这个类
- (void)returnStarInfo:(BViewController *)Bviewc andMssage:(NSString *)msg andTitle:(NSString *)title;
@end
@interface BViewController : UIViewController
//二、引用代理
@property(nonatomic,weak)id<BViewControllerDelegate>delegate;
@property(nonatomic,strong)NSDictionary *starDic;
@end
//三、发送消息
// BViewController.m
// 星座运程App
#import "BViewController.h"
//三、在一个适当的时机,发送消息
- (IBAction)returnStarInfo:(UIButton *)sender {
UIButton *bt=[[UIButton alloc]init];
bt=sender;
NSString *str=[self.starDic objectForKey:bt.titleLabel.text];
//当发生了点击绑定动做的按钮时,发送消息
[self.delegate returnStarInfo:self andMssage:str andTitle:bt.titleLabel.text];
[self.navigationController popViewControllerAnimated:YES];
}
//————————————————————--------------------------------代理方--------------------------------------------
// AViewController.m
// 星座运程App
#import "AViewController.h"
#import
“BViewController.h”//协议就存在这个文件中中
//一、遵照协议
@interface AViewController ()<BViewControllerDelegate>//
协议就存在这个文件中:BViewController.h
@end
//二、设置代理
- (IBAction)selectStar:(UIButton *)sender {
BViewController *bvc=[[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];
bvc.delegate=self;//设置本身为代理方
[self.navigationController pushViewController:bvc animated:YES];
}
//实现方法
-(void)returnStarInfo:(BViewController *)Bviewc andMssage:(NSString *)msg andTitle:(NSString *)title{
self.starInfo.text=msg;
self.title=[title stringByAppendingString:@" --- 12星座"];
}
//-------------------------------------------------------------------------------------------------------