UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 200, 50)];
btn.backgroundColor = [UIColor redColor];
[self.view addSubview:btn];
为按钮添加触发事件
[btn addTarget:self action:@selector(youTouchMe) forControlEvents:UIControlEventTouchUpInside];
触发方法实现
-(void)youTouchMe
{
NSLog(@"别摸我”);
新建提示框
UIAlertController *control = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"是否购买" preferredStyle:UIAlertControllerStyleAlert];
提示框选项1
UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"买" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"买了");
}];
提示框选项2
UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"不买" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"不买,没钱");
}];
添加到提示框
[control addAction:act1];
[control addAction:act2];
展现
[self presentViewController:control animated:YES completion:^{
NSLog(@"弹出来了");
}];
}
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 200, 50)];
btn.backgroundColor = [UIColor redColor];
[self.view addSubview:btn];
为按钮添加触发事件
[btn addTarget:self action:@selector(youTouchMe) forControlEvents:UIControlEventTouchUpInside];
触发方法实现
-(void)youTouchMe
{
NSLog(@"别摸我”);
新建提示框
UIAlertController *control = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"是否购买" preferredStyle:UIAlertControllerStyleActionSheet];
提示框选项1
UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"买" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"买了");
}];
提示框选项2
UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"不买" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"不买,没钱");
}];
提示框3(红色选项)
UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"先付款" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"先给定金");
}];
添加到提示框
[control addAction:act1];
[control addAction:act2];
[control addAction:act3
展现
[self presentViewController:control animated:YES completion:^{
NSLog(@"弹出来了");
}];
}