ios alertview otherbuttontitles怎么实现的

ios alertview otherbuttontitles怎么实现的,第1张

一、动态添加Button动态添加Button的效果就是点击之后,生成一个按钮,并为按钮添加点击的方法。1、在xib文件上拖拽添加一个button,标题为:添加button。2、按住ctrl键拖拽到addbuttonViewController.m文件空白处,生成IBAction,填充代码后如下:[cpp] view plaincopyprint?01.- (IBAction)addButton:(id)sender {02.CGRect frame = CGRectMake(90, 200, 200, 60)03.UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]04.someAddButton.backgroundColor = [UIColor clearColor]05.[someAddButton setTitle:@"动态添加一个按钮!" forState:UIControlStateNormal]06.someAddButton.frame = frame07.[someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside]08.[self.view addSubview:someAddButton]09.}- (IBAction)addButton:(id)sender {CGRect frame = CGRectMake(90, 200, 200, 60)UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]someAddButton.backgroundColor = [UIColor clearColor][someAddButton setTitle:@"动态添加一个按钮!" forState:UIControlStateNormal]someAddButton.frame = frame[someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside][self.view addSubview:someAddButton]}3、动态生成的button点击事件方法:生成的button点击d出提示框。[cpp] view plaincopyprint?01.-(void) someButtonClicked{02.UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"03.message:@"您点击了动态按钮!"04. delegate:self05. cancelButtonTitle:@"确定"06. otherButtonTitles:nil]07.[alert show]08.}-(void) someButtonClicked{UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"message:@"您点击了动态按钮!"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil][alert show]}4、编译运行效果 点击按钮后二、监听UIAlertView。1、在上面的代码基础上,在addbuttonViewController.h文件添加委托[cpp] view plaincopyprint?01.#import <UIKit/UIKit.h>02.03.@interface addbuttonViewController : UIViewController<UIAlertViewDelegate>04.- (IBAction)addButton:(id)sender05.06.@end#import <UIKit/UIKit.h>@interface addbuttonViewController : UIViewController<UIAlertViewDelegate>- (IBAction)addButton:(id)sender@end2、在AlertView中多添加两个按钮[cpp] view plaincopyprint?01.-(void) someButtonClicked{02.UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"03.message:@"您点击了动态按钮!"04. delegate:self05. cancelButtonTitle:@"确定"06. otherButtonTitles:@"取消",@"第三项",nil]07.[alert show]08.}-(void) someButtonClicked{UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"message:@"您点击了动态按钮!"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:@"取消",@"第三项",nil][alert show]}效果图:3、在对应的.m文件中实现委托中的方法监听你点击了那个按钮[cpp] view plaincopyprint?01.-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex02.{03.NSLog(@"buttonIndex:%d", buttonIndex)04.}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{NSLog(@"buttonIndex:%d", buttonIndex)}点击AlertView中d出的三个按钮打印的结果:[cpp] view plaincopyprint?01.2012-06-14 16:53:18.516 DynamicAddButton[5645:f803] buttonIndex:102.2012-06-14 16:53:23.652 DynamicAddButton[5645:f803] buttonIndex:203.2012-06-14 16:53:25.701 DynamicAddButton[5645:f803] buttonIndex:004.2012-06-14 16:53:39.900 DynamicAddButton[5645:f803] buttonIndex:12012-06-14 16:53:18.516 DynamicAddButton[5645:f803] buttonIndex:12012-06-14 16:53:23.652 DynamicAddButton[5645:f803] buttonIndex:22012-06-14 16:53:25.701 DynamicAddButton[5645:f803] buttonIndex:02012-06-14 16:53:39.900 DynamicAddButton[5645:f803] buttonIndex:1这样你就知道点了按个按钮了。

我只说一下思路:

这里按钮的位置随机出现这个需求,我们知道控件的位置是一个CGPoint. 横坐标的点x不会超过320,所以你可以使用随机数random%320 来得到320之间的随机出来的数,同样的纵坐标点y不会超过480。这样就可以随机取出了按钮的位置。

还有一个需求是随机出来的5个按钮不能重叠。对于这个需求,可以使用函数CGRectContainPoint来判断,判断新生成出来的点是否是在已生成的控件范围内。

2.按钮的要有边框和背景色。你需要引入一个<QuartzCore.framework>来设置

button.layer.borderWidth=1

button.layer.borderColor=[UIColor grayColor]

button.tinColor=[UIColor blueColor]

3.使用代码生成的按钮,你标记某个按钮的时候,可以使用tag属性

button1.tag=1

button2.tag=2

这样你可以根据这个tag值找到你要 *** 作的button

UIButton *button1=(UIButton *)[self.view viewWithTag:1]

IOS 实现背景滑动

1、在很多APP中,我们都可以看见那些特效绚丽的滑动选项条,那么如何才能够简单,快速的实现那样的效果呢

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

NSMutableArray *btnArray

NSMutableArray *titleArray

}

@property (nonatomic,strong) UIView *customView

@property (nonatomic,strong) UIView *backView

@property (nonatomic,strong) UIButton *myButton

-(void)myButtonClcik:(id)sender

@end

第二步:在我们的额viewdidload方法中,或者自定义一个方法中创建我么的界面元素。《这里我引日了QuartzCore框架,是为了使用其layer属性》

[cpp] view plaincopy

#import "ViewController.h"

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize customView

@synthesize backView

@synthesize myButton

//每行显示的button个数

#define kSelectNum 6

- (void)viewDidLoad

{

[super viewDidLoad]

// Do any additional setup after loading the view, typically from a nib.

//创建背景视图,并设置背景颜色或者图片

customView = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 900, 60)]

customView.backgroundColor = [UIColor blackColor]

//设置customView的样式,变为圆角

customView.layer.cornerRadius = 15.0f

customView.layer.masksToBounds = YES

//将customView add 到当前主View中

[self.view addSubview:customView]

//创建button的背景视图

backView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 95, 50)]

backView.backgroundColor = [UIColor blueColor]

//设置为圆角。以免造成重叠显示

backView.layer.cornerRadius = 15.0f

backView.layer.masksToBounds = YES

//将backView视图add到customView中

[customView addSubview:backView]

//创建button,首先button的个数是不固定的,因此我们需要动态的生成button

//创建数组,保存button的title

btnArray = [[NSMutableArray alloc]init]

titleArray = [[NSMutableArray alloc]initWithObjects:@"热播大片",@"最新更新",@"最热观看",@"美剧大片",@"韩剧频道",@"综艺娱乐", nil]

//动态生成button

for (int i = 0i <kSelectNumi ++){

myButton = [UIButton buttonWithType:UIButtonTypeCustom]

myButton.titleLabel.font = [UIFont boldSystemFontOfSize:20.0f]

[myButton setTitle:[titleArray objectAtIndex:i] forState:UIControlStateNormal]

[myButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]

[myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]

[myButton setFrame:CGRectMake(i%(kSelectNum + 1)*140+5, 5, 95, 50)]

[myButton addTarget:self action:@selector(myButtonClcik:) forControlEvents:UIControlEventTouchUpInside]

myButton.tag = i

[btnArray addObject:myButton]

[customView addSubview:myButton]

//设置默认选择的button.title的颜色

if(i == 0){

myButton.selected = YES

}

}

}

第三步:我们为button添加按钮点击事件,同时设置背景色滑动特效。

[cpp] view plaincopy

- (void)myButtonClcik:(id)sender{

//NSString *selectedBtn = [NSString stringWithFormat:@"%@",[titleArray objectAtIndex:button.tag]]

//UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:selectedBtn delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]

//[alert show]

//添加动画过度效果

[UIView beginAnimations:@"slowGlide" context:nil]

[UIView setAnimationDuration:0.3f]

//设置每次只能选择一个button

UIButton *button = (UIButton *)sender

if(!button.selected){

for (UIButton *eachBtn in btnArray) {

if(eachBtn.isSelected){

[eachBtn setSelected:NO]

}

}

[button setSelected:YES]

//设置点击那个按钮,那个按钮的背景改变为backView的颜色

[backView setFrame:button.frame]

}

[UIView commitAnimations]

}

最后成型,我们就可以根据我们的样式需要进行调整了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11515308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存