大綱
一、使用屬性在窗體中的傳值
1.正向傳值:將值從vc1傳至vc2
項(xiàng)目:AttributeTransferValue0316
方法:使用屬性
步驟:
1.在vc2中設(shè)置屬性(拿到vc1中取值癣漆,再傳回vc2礁鲁。誰需要照捡,誰設(shè)屬性涧偷。需要什么類型莽鸭,就要聲明什么類型的屬性)
2.給屬性賦值
3.使用屬性保存的值
2.反向傳值:將值從vc2傳至vc1
項(xiàng)目:TransferBack0316
方法1(不建議使用):使用屬性傳值
原因:通過索引獲取vc數(shù)組中的vc1時(shí)劣摇,容易出錯(cuò)。
二茁裙、自定義AlertView
注意:
①全局變量塘砸,本類中使用;屬性呜达,本類和其他類都可使用谣蠢。
②若父視圖的alpha不為1粟耻,無論子視圖是否設(shè)置自己的alpha查近,子視圖alpha=父視圖的alpha。
1.1 聲明屬性挤忙、初始化方法
1.2 實(shí)現(xiàn)init和show方法
2.1設(shè)置屬性(水桶)
2.2為屬性賦值(裝水)
2.3 獲取屬性(水桶)
正文
一霜威、使用屬性在窗體中的傳值
1.正向傳值:將值從vc1傳至vc2
項(xiàng)目:AttributeTransferValue0316
方法:使用屬性
步驟:
1.在vc2中設(shè)置屬性(拿到vc1中取值,再傳回vc2册烈。誰需要戈泼,誰設(shè)屬性婿禽。需要什么類型,就要聲明什么類型的屬性)
2.給屬性賦值
3.使用屬性保存的值
源碼:
文件1:AppDelegate.m
ViewController1 *vc1 = [[ViewController1 alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc1];
self.window.rootViewController = nav;
文件2:ViewController1.m
@interface ViewController1 ()
{
__weak IBOutlet UITextField *_textFieldVc1;
}
@end
@implementation ViewController1
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"vc1";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
//vc1 --> vc2
- (IBAction)nextClick:(UIButton *)sender
{
ViewController2 *vc2 = [[ViewController2 alloc]init];
//傳值第二步:給屬性賦值
//情況1:使用字符串
//在創(chuàng)建對(duì)象之后大猛,在push之前
vc2.strVc2 = _textFieldVc1.text;
[self.navigationController pushViewController:vc2 animated:YES];
}
文件3:ViewController2.h
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
//哪個(gè)類需要取值扭倾,就在哪個(gè)類中設(shè)置屬性,用來保存值挽绩。
//nonatomic:非原子性的
//copy:與內(nèi)存管理有關(guān)
@property (nonatomic,copy)NSString *strVc2;
@property (nonatomic,retain)NSArray *arrStr;
//描述屬性的關(guān)鍵字(與內(nèi)存管理有關(guān))
//字符串:copy
//基本數(shù)據(jù)類型膛壹,結(jié)構(gòu)體:assign
//其他類型:retain
@end
文件4:ViewController2.m
@interface ViewController2 ()
{
__weak IBOutlet UILabel *_labVc2;
}
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"vc2";
//傳值第三步:使用屬性保存的值
//情況1:使用字符串
_labVc2.text = self.strVc2;
}
文件5:ViewController1.xib
文件6:ViewController2.xib
2.反向傳值:將值從vc2傳至vc1
項(xiàng)目:TransferBack0316
方法1(不建議使用):使用屬性傳值
原因:通過索引獲取vc數(shù)組中的vc1時(shí),容易出錯(cuò)唉堪。
NSArray *vcArr = [self.navigationController viewControllers];
ViewController1 *vc1 = [vcArr objectAtIndex:0];
源碼:
文件1:AppDelegate.m
ViewController1 *vc1 = [[ViewController1 alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc1];
self.window.rootViewController = nav;
文件2:ViewController1.h
@interface ViewController1 : UIViewController
{
__weak IBOutlet UILabel *_label;
}
@property (nonatomic,copy)NSString *str;
@end
文件3:ViewController1.m
//將要顯示
- (void)viewWillAppear:(BOOL)animated
{
_label.text = self.str;
}
- (IBAction)nextClick:(id)sender
{
ViewController2 *vc2 = [[ViewController2 alloc]init];
[self.navigationController pushViewController:vc2 animated:YES];
}
文件4:ViewController2.h
@interface ViewController2 : UIViewController
{
__weak IBOutlet UITextField *_pwTF;
}
@end
文件5:ViewController2.m
//vc2-->vc1
- (IBAction)backClick:(id)sender
{
NSArray *vcArr = [self.navigationController viewControllers];
ViewController1 *vc1 = [vcArr objectAtIndex:0];
vc1.str = _pwTF.text;
[self.navigationController popToRootViewControllerAnimated:YES];
}
文件6:ViewController1.xib
文件7:ViewController2.xib
二模聋、自定義AlertView
注意:
①全局變量,本類中使用唠亚;屬性链方,本類和其他類都可使用。
②若父視圖的alpha不為1灶搜,無論子視圖是否設(shè)置自己的alpha祟蚀,子視圖alpha=父視圖的alpha。
1.1 聲明屬性占调、初始化方法
文件:MyAlertView.h
//容器暂题,水桶
@property (nonatomic,retain)ViewController *vc;
//自定義初始化方法
- (id)initWithTitle:(NSString *)title withMessage:(NSString *)message cancleBtnTitle:(NSString *)cancleBtnTitle otherBtnTitle:(NSString *)otherBtnTitle;
//展示
- (void)showView;
1.2 實(shí)現(xiàn)init和show方法
文件:MyAlertView.m
- (id)initWithTitle:(NSString *)title withMessage:(NSString *)message cancleBtnTitle:(NSString *)cancleBtnTitle otherBtnTitle:(NSString *)otherBtnTitle
{
self = [super init];
if (self)
{
//1.設(shè)置位置大小
self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
//不可直接設(shè)置self的透明度,否則會(huì)影響self上的子控件的透明度
// self.alpha = 0.5;
//2.1 半透明的view
UIView *gray = [[UIView alloc]initWithFrame:self.frame];
gray.backgroundColor = [UIColor grayColor];
gray.alpha = 0.5;
[self addSubview:gray];
//3.添加view
UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(30, HEIGHT/3.0, WIDTH-60, HEIGHT/3.0)];
whiteView.backgroundColor = [UIColor whiteColor];
//圓角
whiteView.layer.cornerRadius = 20;
whiteView.clipsToBounds = YES;
[self addSubview:whiteView];
//3.1 標(biāo)題
UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WHITEVIEW_WIDTH, WHITEVIEW_HEIGHT/4.0)];
titleLab.text = title;
titleLab.textAlignment = NSTextAlignmentCenter;
[whiteView addSubview:titleLab];
//3.2 提示信息
UILabel *messageLab = [[UILabel alloc]initWithFrame:CGRectMake(0, WHITEVIEW_HEIGHT/4.0, WHITEVIEW_WIDTH, WHITEVIEW_HEIGHT/4.0)];
messageLab.text = message;
messageLab.textAlignment = NSTextAlignmentCenter;
[whiteView addSubview:messageLab];
//3.3 取消按鈕
UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cancleBtn.frame = CGRectMake(0, WHITEVIEW_HEIGHT/2.0, WHITEVIEW_WIDTH/2.0, WHITEVIEW_HEIGHT/2.0);
[cancleBtn setTitle:cancleBtnTitle forState:UIControlStateNormal];
[cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
cancleBtn.tag = 1;
[cancleBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[whiteView addSubview:cancleBtn];
//3.3 確定按鈕
UIButton *otherBtn = [UIButton buttonWithType:UIButtonTypeCustom];
otherBtn.frame = CGRectMake(WHITEVIEW_WIDTH/2.0, WHITEVIEW_HEIGHT/2.0, WHITEVIEW_WIDTH/2.0, WHITEVIEW_HEIGHT/2.0);
[otherBtn setTitle:otherBtnTitle forState:UIControlStateNormal];
[otherBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
otherBtn.tag = 2;
[otherBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[whiteView addSubview:otherBtn];
}
return self;
}
//按鈕點(diǎn)擊事件
- (void)buttonClick:(UIButton *)sender
{
//如果是確定
if (sender.tag == 2)
{
//改變顏色
self.vc.view.backgroundColor = [UIColor redColor];
}
NSLog(@"%@",sender);
[self removeFromSuperview];
}
//展示
- (void)showView
{
UIWindow *window = [UIApplication sharedApplication].delegate.window;
[window addSubview:self];
}
2.1設(shè)置屬性(水桶)
文件:MyAlertView.h
@property (nonatomic,retain)ViewController *vc;
2.2為屬性賦值(裝水)
文件:ViewController.m
- (IBAction)deleteClick:(id)sender
{
MyAlertView *myAlertView = [[MyAlertView alloc]initWithTitle:@"ChangeColor" withMessage:@"是否改變顏色" cancleBtnTitle:@"取消" otherBtnTitle:@"確定"];
//將本類對(duì)象self傳給myAlertView.vc
myAlertView.vc = self;
[myAlertView showView];
}
2.3 獲取屬性(水桶)
文件:MyAlertView.m
//按鈕點(diǎn)擊事件
- (void)buttonClick:(UIButton *)sender
{
//如果是確定
if (sender.tag == 2)
{
//改變顏色
self.vc.view.backgroundColor = [UIColor redColor];
}
NSLog(@"%@",sender);
[self removeFromSuperview];
}