提示框的种类有很多,废话不多说,直接上代码
一、文本提示框
运行结果如下:
代码实现如下:
@interface ViewController ()
// 添加方法
- (IBAction)add;
// 移除方法
- (IBAction)remove;
// 商品容器
@property (weak, nonatomic) IBOutlet UIView *shopsView; @property (weak, nonatomic) IBOutlet UIButton *removeBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UILabel *hudLabel; @property (nonatomic, strong)NSMutableArray *shops;
@end @implementation ViewController // 控制器的view创建完毕就会调用
// 该方法只会调用一次
- (void)viewDidLoad
{
[super viewDidLoad]; // 一开始就让提示框隐藏
self.hudLabel.alpha = 0.0;
} - (IBAction)add
{
/**********************计算X/Y**************************/
// 商品的宽高
CGFloat shopWidth = ;
CGFloat shopHeight = ; // 总行数和列数
int totalCol = ; // 计算间隙
CGFloat ColMargin = (self.shopsView.frame.size.width - (totalCol * shopWidth)) / (totalCol - );
CGFloat RowMargin = ColMargin; // 获取绿色控件的子控件
NSUInteger index = self.shopsView.subviews.count; // 1.计算行号
int row = index / ;
// 2.计算当前行的Y值
// Y = 行号 * (商品的高度 + 间隙)
CGFloat shopY = row * (shopHeight + RowMargin); // 3.计算列号
int col = index % ;
// 4.计算当前列的X值
CGFloat shopX = col * (shopWidth + ColMargin); /**********************创建商品**************************/ // 1.加载XIB
XMGShopView *shopView = [XMGShopView shopView]; // 2.设置frame
CGRect tempFrame = shopView.frame;
tempFrame.origin.x = shopX;
tempFrame.origin.y = shopY;
shopView.frame = tempFrame; // 3.设置数据
NJShop *shop = self.shops[index];
shopView.shop = shop; // 2.添加子控件
[self.shopsView addSubview:shopView]; /**********************添加商品**************************/ // 6.控制删除按钮是否可以点击
self.removeBtn.enabled = YES; // 7.控制添加按钮是否可以点击
self.addBtn.enabled = self.shopsView.subviews.count < self.shops.count; // 8.控制提示框是否显示
if (self.addBtn.enabled == NO) {
[self showHUDWithText:@"商品柜已经满了, 不要再买买买了..."];
}
} - (IBAction)remove
{ NSLog(@"%@", self.shops[]); // 1.取出父控件中最后一个控件
UIView *subView = self.shopsView.subviews.lastObject;
// 2.移除最后一个控件
[subView removeFromSuperview]; // 3.控制删除按钮是否可以点击
self.removeBtn.enabled = self.shopsView.subviews.count > ; // 4.控制添加按钮是否可以点击
self.addBtn.enabled = YES; // 5.控制器提示框是否显示
if (self.removeBtn.enabled == NO) { self.hudLabel.text = @"商品柜已经空了, 继续买买买...";
// 显示提示框
[UIView animateWithDuration:1.0 animations:^{
self.hudLabel.alpha = 1.0;
} completion:^(BOOL finished) { [UIView animateWithDuration:1.0 delay:2.0 options:kNilOptions animations:^{
self.hudLabel.alpha = 0.0;
} completion:nil];
}]; [self showHUDWithText:@"商品柜已经空了, 继续买买买..."];
}
} - (void)showHUDWithText:(NSString *)text
{
// 1.设置需要展示的内容
self.hudLabel.text =text; // 2.显示提示框
[UIView animateWithDuration:1.0 animations:^{
self.hudLabel.alpha = 1.0;
} completion:^(BOOL finished) {
// 3.隐藏提示框
[UIView animateWithDuration:1.0 delay:2.0 options:kNilOptions animations:^{
self.hudLabel.alpha = 0.0;
} completion:nil];
}];
} // 重写getter方法
- (NSMutableArray *)shops
{
if (_shops == nil) { // 1.获取plist文件的绝对路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
// 2.根据路径加载plist文件
NSArray *tempArr = [NSArray arrayWithContentsOfFile:path]; // 3.将数组中所有的字典转换为模型
_shops = [NSMutableArray array];
for (NSDictionary *dict in tempArr) {
NJShop *shop = [[NJShop alloc] init];
shop.name = dict[@"name"];
shop.icon = dict[@"icon"];
[_shops addObject:shop];
}
}
return _shops;
}
@end
二、系统自带的提示框(HUD)
1.UIAlertView
代码如下:
- (void)useAlert
{
// 1.UIAlerView
// 如果看到iOS提供的方法中有... 就代表是一个可变参数,可以传一个或者多个 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"哥是标题" message:@"姐是正文..." delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; [alert show];
}
#pragma mark - UIAlertViewDelegate
// 只要UIAlertView上的按钮被点击, 就会调用
// alertView:谁触发事件就会把谁传递进来
// ButtonAtIndex: 当前被点击按钮的索引
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonInde
{
// 取消 = 0, 确定 = 1 , 其他 = 2 , Other = 3 以此类推
switch (buttonInde) {
case :
NSLog(@"点击了取消");
break;
case :
NSLog(@"点击了确定");
break;
case :
NSLog(@"点击了其他");
break;
case :
NSLog(@"点击了Other");
break;
default:
break;
}
}
2. UIActionSheet
- (void)useActionSheet
{
// 2.UIActionSheet
// 1.创建UIActionSheet
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"哥是标题" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其它", @"Other", nil];
// 2.显示UIActionSheet
[sheet showInView:self.view];
} #pragma mark - UIActionSheetDelegate
// 只要UIActionSheet上的按钮被点击就会调用
// actionSheet:谁触发事件就会把谁传递进来
// clickedButtonAtIndex:当前被点击按钮的索引
- (void)actionSheet:(nonnull UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%ld", buttonIndex);
}
注意以上两中方法都要设置代理,那么分别要遵守
<UIAlertViewDelegate, UIActionSheetDelegate>协议
3. UIAlertController
- (void)useAlertControllerAlert
{
// 1.创建alert控制器
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"哥是标题" message:@"姐是正文..." preferredStyle:UIAlertControllerStyleAlert]; // 2.添加一个按钮
// Title : 按钮上显示的文字
// style: 按钮的样式
// handler: 点击按钮之后的回调
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {
NSLog(@"点击了确定按钮");
}]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {
NSLog(@"点击了取消按钮");
}]; [alertVc addAction:action1];
[alertVc addAction:action2]; // 3.添加三个输入框
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
textField.text = @"用户名";
NSLog(@"textField");
}];
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
textField.secureTextEntry = YES;
NSLog(@"textField");
}];
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
NSLog(@"textField");
}]; // 4.presentViewController弹出一个控制器
[self presentViewController:alertVc animated:YES completion:nil]; }
三、自定义提示框
实现代码如下:
- (void)useCustomHUD
{
// 1.创建父控件
UIView *cover = [[UIView alloc] init];
cover.backgroundColor = [UIColor colorWithRed: green: blue: alpha:0.5];
cover.frame = CGRectMake(, , , );
cover.center = self.view.center; // 修改父控件为圆角
cover.layer.cornerRadius = ;
[self.view addSubview:cover]; // 2.创建菊花
// 菊花有默认的尺寸
// 注意: 虽然有默认的尺寸, 但是要想显示必须让菊花转起来
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activity.center = CGPointMake(cover.frame.size.width * 0.5, );
[activity startAnimating]; [cover addSubview:activity];
// 3.创建UILabel
UILabel *label = [[UILabel alloc] init];
// label.backgroundColor = [UIColor purpleColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"正在拼命加载中...";
label.frame = CGRectMake(, cover.frame.size.height - , cover.frame.size.width, );
[cover addSubview:label]; }
除了以上列出的几种方法外,还可以使用第三方框架,比如MBP