警告框-系统自带

不显示标题的方法

1
2
3
4
5
///当我用这个代码创建一个UIAlertController时,我没有标题间距。
/// 而不是传空
[UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];

actionsheet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
weak var weakSelf = self // 弱引用
let alertController = UIAlertController()
alertController.view.tintColor = UIColor.HWColorWithHexString(hex: "2E2E2E", alpha: 1) // 修改颜色
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let directMessagesAction = UIAlertAction(title: "私信", style: .default) { (action) in

}
let focusOnAction = UIAlertAction(title: "关注", style: .default) { (action) in

}
alertController.addAction(focusOnAction)
alertController.addAction(directMessagesAction)
alertController.addAction(cancelAction)
weakSelf!.present(alertController, animated: true, completion: nil)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-(void)creatActionSheet {
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"选择对象" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

/*
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,
UIAlertActionStyleCancel, 取消按钮
UIAlertActionStyleDestructive 破坏性按钮,比如:“删除”,字体颜色是红色的
} NS_ENUM_AVAILABLE_IOS(8_0);

*/
// 创建action,这里action1只是方便编写,以后再编程的过程中还是以命名规范为主
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"A类" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"B类" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消");
}];

//把action添加到actionSheet里
[actionSheet addAction:action1];
[actionSheet addAction:action2];
[actionSheet addAction:action3];

//相当于之前的[actionSheet show];
[self presentViewController:actionSheet animated:YES completion:nil];
}

警告框oc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (void)showBleAlert {

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
message:@"请打开蓝牙进行操作"
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"前往设置" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSURL *settingUrl = [NSURL URLWithString:@"App-Prefs:root=General&path=Bluetooth"];
[[UIApplication sharedApplication] openURL:settingUrl];
}];

UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {}];

[alert addAction:cancel];
[alert addAction:defaultAction];

[[[[UIApplication sharedApplication] keyWindow ] lw_currentTopVC] presentViewController:alert animated:YES completion:nil];
}

警告框swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func showLogoutAlert() {
let vc = UIApplication.shared.keyWindow?.lw_currentTopVC()
if !(vc is UIAlertController || vc is MXLoginVC)
{
let aler = UIAlertController(title: "提示", message: "当前账号在别处已登录", preferredStyle: .alert)
let update = UIAlertAction(title: "重新登录", style: .default, handler: {(sender) in
MXHHandleLoginData.handleLogoutData()
MXEHChangeRootVC.changeToLogin()
})
aler.addAction(update)

let cancel = UIAlertAction(title: "取消", style: .cancel, handler: { (sender) in
MXHHandleLoginData.handleLogoutData()
})
aler.addAction(cancel)

UIApplication.shared.keyWindow?.lw_currentTopVC()?.present(aler, animated: true, completion: nil)
}
}

警告输入框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
func showInpuAlert()
{
let alertController = UIAlertController(title: "", message: nil, preferredStyle: UIAlertController.Style.alert)
alertController.addTextField(configurationHandler: { (textField: UITextField!) -> Void in
textField.placeholder = "请输入您的兑换码"

// 添加监听代码,监听文本框变化时要做的操作
NotificationCenter.default.addObserver(self, selector: #selector(self.alertTextFieldDidChange), name: UITextField.textDidChangeNotification, object: textField)
})
let cancelAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel, handler: nil)
let okAction = UIAlertAction(title: "确认", style: UIAlertAction.Style.default , handler: { (action: UIAlertAction!) -> Void in
let login = (alertController.textFields?.first)! as UITextField
let str = login.text

self.childVC.requestExchangeQuan(code:str ?? "")

NotificationCenter.default.removeObserver(self, name: UITextField.textDidChangeNotification, object: nil)
})

okAction.isEnabled = false
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}

/// 监听文字改变
@objc func alertTextFieldDidChange()
{
let alertController = self.presentedViewController as! UIAlertController?
if (alertController != nil) {
let login = (alertController!.textFields?.first)! as UITextField
let okAction = alertController!.actions.last! as UIAlertAction
if (!(login.text?.isEmpty)!) {
okAction.isEnabled = true
} else {
okAction.isEnabled = false
}
}
}