一般對我來說學(xué)習(xí)一門開發(fā)語言第一個(gè)要學(xué)的就是輸出和彈出提示框??
在OC中,輸出簡單就是NSLOG了 下面就是彈窗
//初始化彈窗
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"標(biāo)題" message:@"輸出內(nèi)容" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil]];
//彈出提示框
[self presentViewController:alert animated:true completion:nil];
可以簡單的進(jìn)行封裝一下
- (void)showToast:(NSString *) msg {
//初始化彈窗
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"標(biāo)題" message:msg preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil]];
//彈出提示框
[self presentViewController:alert animated:true completion:nil];
}
可以簡單的調(diào)用上面的方法
[self showToast:@"喵喵喵黑界!"];
上面是單一按鈕的情況 如果想使用確定和取消兩個(gè)按鈕的話是這樣
//定義確定和取消按鈕
@property (strong, nonatomic) UIAlertAction *okBtn;
@property (strong, nonatomic) UIAlertAction *cancelBtn;
- (void) showToast2 {
// 初始化對話框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"輸出內(nèi)容" preferredStyle:UIAlertControllerStyleAlert];
// 確定按鈕監(jiān)聽
_okBtn = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
NSLog(@"點(diǎn)擊了確定按鈕");
}];
_cancelBtn =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
NSLog(@"點(diǎn)擊了取消按鈕");
}];
//添加按鈕到彈出上
[alert addAction:_okBtn];
[alert addAction:_cancelBtn];
// 彈出對話框
[self presentViewController:alert animated:true completion:nil];
}