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]; } ```

相关推荐
游戏开发爱好者82 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
黑码哥3 小时前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
2501_915106324 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106324 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
熊猫钓鱼>_>5 小时前
移动端开发技术选型报告:三足鼎立时代的开发者指南(2026年2月)
android·人工智能·ios·app·鸿蒙·cpu·移动端
徐同保1 天前
通过ip访问nginx的服务时,被第一个server重定向了,通过设置default_server解决这个问题
ios·iphone
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview
夏幻灵2 天前
HTTPS全面解析:原理、加密机制与证书体
ios·iphone
TheNextByte12 天前
如何在iPhone上恢复已删除的笔记的综合指南
笔记·ios·iphone