-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
// 委托
[_delegate buyIphone:@"??"];
}else if (buttonIndex == 1){
// 通知
NSDictionary *dic = @{@"boom":@"??"};
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeLabel" object:dic];
}
}
通知
第一個頁面
1.注冊通知
[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(justDoIt:) name:@"changeLabel" object:nil];
2.拿到暗號,做事情(changeLabel為暗號)
-(void)justDoIt:(NSNotification *)obj{
NSDictionary *dic = [obj object];
_notificationOrigionLabel.text = dic[@"boom"];
}
第二個頁面
1:對暗號
在button里寫
NSDictionary * dic = @{@"boom":@"??"};
[[NSNotificationCenter defaultCenter] postNotificationName:@"zhadan" object:dic];
changeLabel為暗號
委托
@protocol BuyIphone6sDelegate <NSObject>
-(void)buyIphone:(NSString *)str;
@property(nonatomic,strong)id<BuyIphone6sDelegate>delegate;
【第二個頁面】
1在第二個頁面寫協(xié)議,寫在interface 上面
@protocol BuyIphone6sDelegate <NSObject>
2.在第二個頁面 實例化協(xié)議的變量
-(void)buyIphone:(NSString *)str;
3.讓協(xié)議變量去做做協(xié)議中的方法
@property(nonatomic,strong)id<BuyIphone6sDelegate>delegate;
在button里實現(xiàn)
UIAlertAction *enter = [UIAlertAction actionWithTitle:@"委托" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[_delegate buyIphone:@"??"];
}];
方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
// 委托
[_delegate buyIphone:@"??"];
【第一個頁面】
1.跳轉(zhuǎn)頁面的時候,簽合同。
vc2.delegate = self; self為vc1
2.在interface中實現(xiàn)這個協(xié)議
@interface DPNViewController ()<BuyIphone6sDelegate>
3.在.m中實現(xiàn)協(xié)議方法
-(void)buyIphone:(NSString *)str{
_delegateOriginLabel.text = str;
}
整理
第一個頁面
//NextViewController是push進入的第二個頁面
//NextViewController.h 文件
//定義一個協(xié)議,前一個頁面ViewController要服從該協(xié)議,并且實現(xiàn)協(xié)議中的方法
@protocol NextViewControllerDelegate <NSObject>
- (void)passTextValue:(NSString *)tfText;
@end
@interface NextViewController : UIViewController
@property (nonatomic, assign) id<NextViewControllerDelegate> delegate;
@end
//NextViewController.m 文件
//點擊Button返回前一個ViewController頁面
- (IBAction)popBtnClicked:(id)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(passTextValue:)]) {
//self.inputTF是該頁面中的TextField輸入框
[self.delegate passTextValue:self.inputTF.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
第二個頁面
//ViewController.m 文件
@interface ViewController ()<NextViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *nextVCInfoLabel;
@end
//點擊Button進入下一個NextViewController頁面
- (IBAction)btnClicked:(id)sender
{
NextViewController *nextVC = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
nextVC.delegate = self;//設(shè)置代理
[self.navigationController pushViewController:nextVC animated:YES];
}
//實現(xiàn)協(xié)議NextViewControllerDelegate中的方法
#pragma mark - NextViewControllerDelegate method
- (void)passTextValue:(NSString *)tfText
{
//self.nextVCInfoLabel是顯示NextViewController傳遞過來的字符串Label對象
self.nextVCInfoLabel.text = tfText;
}