將alertView 和 actionSheet 封裝在UIAlertController 里面化整為零,使開發者更便利
當我們一味的追求高內聚,低耦合的時候,偉大的蘋果反其道而行之,這也告訴了我們一個道理:
只有水平高了,內聚也就高了,耦合度自然就低了!哈哈,廢話少說,直接上圖:
這是整個 demo 的效果圖:

下面看看 alert 相關東西的實現,直接上代碼:
- (IBAction)alertAction:(id)sender {
UIAlertController * alter = [UIAlertController alertControllerWithTitle:@"通知" message:@"請填寫登陸信息!" // 初始化 alert preferredStyle:UIAlertControllerStyleAlert];
[alter addTextFieldWithConfigurationHandler:^(UITextField * textField) {
/*注意:
為 alert 添加textfield 注意 只有preferredStyle
為UIAlertControllerStyleAlert時候才能添加textfield
在回調的block內配置 textfield
*/
[textField setFrame:CGRectMake(0, 50, 44, 200)];
[textField setTextColor:[UIColor blueColor]];
[textField setPlaceholder:@"NAMEFIELD"];
[textField setClearButtonMode:(UITextFieldViewModeWhileEditing)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
}];
[alter addTextFieldWithConfigurationHandler:^(UITextField * textField) {
[textField setFrame:CGRectMake(0, 100, 60, 200)];
[textField setTextColor:[UIColor blueColor]];
[textField setPlaceholder:@"PASSWORD"];
[textField setSecureTextEntry:YES];
[textField setClearButtonMode:(UITextFieldViewModeWhileEditing)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
}];
[alter addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
/*
在這里添加動作的實現
*/
NSLog(@"%@---%@",alter.textFields[0].text,alter.textFields[1].text);
}]];
[self presentViewController:alter animated:YES completion:nil]; // 顯示 alert
}
代碼擼好了,運行,點擊alertAction 啪的一聲,出現了如下的效果圖

下面看看actionSheet 的實現
- (IBAction)actionSheet:(id)sender {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * action) {
NSLog(@"---拍照獲取!");
// 再次添加實現
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"從相冊獲取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"---從相冊獲取!");
//在此添加實現
}]];
[self presentViewController:alert animated:YES completion:nil];
}
然后運行點擊 ,啪的一聲:

更過的進階技術可以關注公眾號:進階的腳步 回復:學習資料 有驚喜哦

