gpt4 book ai didi

IOS 以编程方式创建 UIAlertViewController

转载 作者:技术小花猫 更新时间:2023-10-29 10:18:39 32 4
gpt4 key购买 nike

我正在使用代码(没有 Storyboard)开发 ViewController。我正在尝试添加和 AlertController

我已经在 .m 中声明了属性

@property (nonatomic, strong) UIAlertController *alertController;

并在loadview方法中初始化

//alertviewController
_alertController = [[UIAlertController alloc]initWithNibName:nil bundle:nil];

并在viewDidLoad中调用alertview:

_alertController = [UIAlertController alertControllerWithTitle:@"Error display content" message:@"Error connecting to server, no local database" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
LandingPageViewController *viewController = [[LandingPageViewController alloc] initWithNibName:nil bundle:nil];
// viewController.showNavBarBackButton = YES;
[[AppDelegate sharedAppDelegate].rootViewController cPushViewController:viewController];
}];
[_alertController addAction:ok];
[self presentViewController:_alertController animated:YES completion:nil];

我不知道为什么没有显示警报。我的代码有些问题。如何以编程方式设置和调用 alertViewController

最佳答案

- (void)logoutButtonPressed
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Logout"
message:@"Are You Sure Want to Logout!"
preferredStyle:UIAlertControllerStyleAlert];

//Add Buttons

UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
[self clearAllData];
}];

UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle no, thanks button
}];

//Add your buttons to alert controller

[alert addAction:yesButton];
[alert addAction:noButton];

[self presentViewController:alert animated:YES completion:nil];
}

关于IOS 以编程方式创建 UIAlertViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38702269/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com