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

相关推荐
linweidong14 小时前
实战救火型 从 500MB 降到 50MB:高频业务场景下的 iOS 内存急救与避坑指南
macos·ios·objective-c·cocoa·ios面试·nstimer·ios面经
山里看瓜16 小时前
解决 iOS 上 Swiper 滑动图片闪烁问题:原因分析与最有效的修复方式
前端·css·ios
网络研究院16 小时前
苹果修复了iOS系统中两个被定向攻击利用的零日漏洞
macos·ios·cocoa
如此风景17 小时前
IOS SwiftUI 全组件详解
ios
雾散声声慢17 小时前
解决 iOS 上 Swiper 滑动图片闪烁问题:原因分析与最有效的修复方式
前端·css·ios
QuantumLeap丶20 小时前
《Flutter全栈开发实战指南:从零到高级》- 24 -集成推送通知
android·flutter·ios
YungFan20 小时前
iOS开发之MetricKit监控App性能
ios·swiftui·swift
二流小码农21 小时前
鸿蒙开发:上架困难?谈谈我的上架之路
android·ios·harmonyos
图图大恼21 小时前
在iOS上体验Open-AutoGLM:从安装到流畅操作的完整指南
人工智能·ios·agent
笑尘pyrotechnic1 天前
[iOS原理] Block的本质
ios·objective-c·cocoa