作為初學(xué)者, 有錯(cuò)誤的地方還請(qǐng)大神們挑出, 本人接受批評(píng), 多謝各位大神了
iOS轉(zhuǎn)場(chǎng): (RootViewController, SecondViewController)
<#(nonnull UIViewController *)#>: 目標(biāo)viewController
animated: 是否存在動(dòng)畫
//Show: 選擇Show,目的地視圖會(huì)被壓入導(dǎo)航棧頂部. 導(dǎo)航條會(huì)提供一個(gè)后退按鈕,用以返回源視圖. 這是最常用的方式.
//Show detail:與Show相似, 但會(huì)替換源視圖. 將沒有導(dǎo)航條和后退按鈕.
[self.navigationController pushViewController:delita animated:YES];
[self.navigationController showViewController:delita sender:<#(nullable id)#>
completion: 轉(zhuǎn)場(chǎng)之后做什么, 是一個(gè)block函數(shù)
//Present Modally: 模態(tài)顯示內(nèi)容.目的地視圖會(huì)從底向上彈出, 通常用于顯示跟頁面連貫性不強(qiáng)的視圖, 比如 添加餐館, 添加用戶(無論在哪個(gè)頁面,都可能會(huì)調(diào)用此功能)
self.navigationController presentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>
Present as popover:iPad中常用,模態(tài)顯示一個(gè)帶箭頭指向圓角矩形彈窗. 類似一個(gè)彈出菜單.
轉(zhuǎn)場(chǎng)傳值:
1. 屬性傳值
//轉(zhuǎn)場(chǎng)方法
- (void)btnAction:(UIButton *)btn{
SecondViewController *sec = [[SecondViewController alloc] init];//獲取目標(biāo)場(chǎng)景
sec.string = self.label.text;//string為目標(biāo)場(chǎng)景中的屬性(NSString)
[self.navigationController pushViewController:sec animated:YES];//轉(zhuǎn)場(chǎng)
}
2. 代理傳值(反向轉(zhuǎn)場(chǎng))
1. 設(shè)置代理
@protocol SecondViewControllerDelegate <NSObject>
@optional
- (void)changeTitle:(NSString *)title;
@end
2. 在SecondViewController.h定義代理
@property (nonatomic, assign) id<SecondViewControllerDelegate>delegate;
3. 在SecondViewController.m使用代理傳值
- (void)backAction:(UIButton *)btn{
if (self.delegate && [self.delegate respondsToSelector:@selector(changeTitle:)]) {//判斷是必須的
[self.delegate changeTitle:self.textField.text];
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
4. 在RootViewController.m中遵循代理并且實(shí)現(xiàn)代理方法
記得讓SecondViewController指定代理人為RootViewController.m
- (void)changeTitle:(NSString *)title{
self.navigationItem.title = title;
}
3. block 傳值
block定義
/**
* //定義一個(gè)參無返回值的block函數(shù)
* void 無返回值
*
* @param ^ block名稱
*
* @^() 無參數(shù)
*/
void (^voidFunc)() = ^(){
NSLog(@"我是一個(gè)代碼塊");
};
//使用block
voidFunc();
/**
* 定義一個(gè)實(shí)現(xiàn)輸出兩個(gè)整數(shù)之和的block函數(shù)
* void 無返回值
* @param (int , int )類型說明
* @param a 參數(shù)
* @param b 參數(shù)
*/
void (^addBlock)(int , int ) = ^(int a, int b){
NSLog(@"a + b = %d", a + b);
};
addBlock(4, 6);
/**
* block命別稱
*
* @param void (^)(int , int) 函數(shù)類型
* @param Block 別名
*/
typedef void (^Block)(int ,int );
//給一個(gè)參數(shù)為兩個(gè)整數(shù), 返回值為整形的block類型命別稱
typedef int (^addTwo)(int ,int );
//block使用
addTwo subBlock = ^(int a, int b){
return a-b;
};
//block中沒有使用局部變量, block存儲(chǔ)在全局區(qū),
//block中如果有使用全局變量, 則存儲(chǔ)在棧區(qū)
subBlock(10, 5);
block傳值步驟
1. 在SecondViewController.h給block命別稱(一般不省略)
typedef void(^titleBlock)(NSString *title);
2. 在SecondViewController.h中定義一個(gè)block屬性
@property (nonatomic, copy) titleBlock myBlock;
//注意: block屬性必須用copy
//block使用全局變量, 存在棧區(qū), 使用copy是把block復(fù)制一份到堆區(qū)
3. 在跳轉(zhuǎn)頁面方法中給block屬性賦值(RootViewController的btnAction:)
//在block中使用局部變量
//__block(MRC)/__weak(ARC)
__weak RootViewController *temp = self;//復(fù)制指針(引用計(jì)數(shù)不變)
//不使用temp而用self, 在下個(gè)界面會(huì)持有self, 引用計(jì)數(shù)會(huì)+1
//使用self容易造成循環(huán)引用(MRC下)
sec.myBlock = ^(NSString *title){
temp.navigationItem.title = title;
};
4. 調(diào)用block屬性傳入?yún)?shù)
- (void)backAction:(UIButton *)btn{
self.myBlock(self.textField.text);
[self.navigationController popToRootViewControllerAnimated:YES];
}
單例模式傳值
1. 新建一個(gè)單例模式, 這里新建一個(gè)類白包含單例模式(User)
//在User.h中定義一些屬性, 這些屬性作為存取值的對(duì)象
@property (nonatomic, strong) NSString *userName;
@property (nonatomic, strong) NSString *passworld;
//設(shè)計(jì)一個(gè)單例模式初始化
//獲取唯一的用戶
+ (User *)defaultUser;
2. 實(shí)例化初始化
+ (User *)defaultUser{
//static修飾的對(duì)象只會(huì)在第一次調(diào)用函數(shù)時(shí)初始化, 之后不再初始化
static User *user = nil;
if(user == nil){//判斷user是否為空, 空則初始化
user =[ [User alloc] init];
}
return user;
}
2. 在RootViewController.m中導(dǎo)入U(xiǎn)ser.h, 同時(shí)在viewWillAppear(界面即將顯示)中獲取單例模式的值
- (void)viewWillAppear:(BOOL)animated{
self.textField.text = [User defaultString].string;
}
3. 在轉(zhuǎn)場(chǎng)按鈕函數(shù)中將值存入U(xiǎn)ser中
- (void)btnAction:(UIButton *)btn{
SecondViewController *sec = [[SecondViewController alloc] init];
//獲取單例
User *u = [User defaultString];
//賦值
u.string = self.textField.text;
[self.navigationController pushViewController:sec animated:YES];
}
注: 如果需要讓RootViewController一開始就顯示, 比如讓textField一開始就顯示"單例模式"
//在viewDidLoad中直接初始化User并存值
self.textField.text = @"單例模式";
[User defaultString].string = self.textField.text;
4. 在SecondViewController中使用User中的string給self.textField.text賦值
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 200, 50)];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.text = [User defaultString].string;
self.textField.delegate = self;
self.textField.textAlignment = NSTextAlignmentCenter;
self.textField.clearButtonMode = UITextFieldViewModeAlways;
[self.view addSubview:self.textField];
5. 在按鈕返回函數(shù)中存儲(chǔ)數(shù)據(jù)
- (void)btnAction:(UIButton *)btn{
User *u = [User defaultString];
u.string = self.textField.text;
[self.navigationController popViewControllerAnimated:YES];//返回上層界面
}