UIkit-UIAlertContent

简单Demo

objectivec 复制代码
//注意!!!必须放在viewController的viewDidAppear里面,viewDidLoad里面不行
- (void)viewDidAppear:(BOOL)animated
{
    // 创建 UIAlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是消息" preferredStyle:UIAlertControllerStyleAlert];
    
    //添加自定义文本框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
         //自定义文本框
        textField.placeholder = @"这是第一个文本框";
        textField.keyboardType = UIKeyboardTypeDefault;
    }];
    
    //添加第二个自定义文本框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
         //自定义文本框
        textField.placeholder = @"这是第二个文本框";
        textField.keyboardType = UIKeyboardTypeDefault;
    }];
    
    UIAlertAction *okAction = [UIAlertAction
        actionWithTitle:@"确定"
                  style:UIAlertActionStyleDefault
                handler:^(UIAlertAction *action) {
                    // 处理确定按钮点击
                    UITextField *textField = alertController.textFields.firstObject;
                    NSString *enteredText = textField.text;
        
                    UITextField *secTextField = alertController.textFields[1];
                    NSString *secText = secTextField.text;
                    // 根据需要处理输入的文本
                }];

    [alertController addAction:okAction];
    
    
    alertController.popoverPresentationController.sourceView = self.view;
    alertController.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds), 1.0, 1.0);


    // 在视图控制器中显示 UIAlertController
    [self presentViewController:alertController animated:YES completion:nil];
    
}
    
}

后面的界面会置灰

position

UIAlertController alertControllerWithTitle:@"标题" message:@"这是消息" preferredStyle:UIAlertControllerStyleAlert\]; UIAlertControllerStyleAlert alert样式在屏幕中间 actionsheet的样式 ![在这里插入图片描述](https://file.jishuzhan.net/article/1730434110284894210/211f44c85e5ed4b4ffece4c0ced5d5b1.webp) actionsheet不能添加文本框 ### 添加文本框 addTextFieldWithConfigurationHandler ![在这里插入图片描述](https://file.jishuzhan.net/article/1730434110284894210/da5bf2238fdcdbf4d989422e8fb66163.webp) 上面代码有事例 ## 其他控件 ### UIStepper ![在这里插入图片描述](https://file.jishuzhan.net/article/1730434110284894210/528ee4d9715a2f14bf1a43805166ea20.webp) ```objectivec - (void)showStepView { // 创建一个标签用于显示数值 self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 300, 200, 30)]; self.label.textColor = [UIColor blackColor]; self.label.text = @"0.0"; [self.view addSubview:self.label]; // 创建一个 UIStepper self.stepper = [[UIStepper alloc] initWithFrame:CGRectMake(50, 350, 0, 0)]; [self.stepper addTarget:self action:@selector(stepperValueChanged) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:self.stepper]; } - (void)stepperValueChanged { self.label.text = [NSString stringWithFormat:@"stepValue:%f",self.stepper.value]; } ```

相关推荐
大熊猫侯佩6 小时前
黑衣人档案:用 Apple Foundation Models + SwiftUI 打造 AI 聊天机器人全攻略
ios·swiftui·ai编程
大熊猫侯佩6 小时前
侠客行・iOS 26 Liquid Glass TabBar 破阵记
ios·swiftui·swift
2501_916007478 小时前
手机使用过的痕迹能查到吗?完整查询指南与步骤
android·ios·智能手机·小程序·uni-app·iphone·webview
從南走到北9 小时前
JAVA国际版同城外卖跑腿团购到店跑腿多合一APP系统源码支持Android+IOS+H5
android·java·ios·微信小程序·小程序
alengan11 小时前
苹果企业签名流程
ios·iphone
Digitally20 小时前
如何在Mac上同步iPhone短信
macos·ios·iphone
2501_915106321 天前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
2501_916008891 天前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview
2501_915921431 天前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
游戏开发爱好者81 天前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview