UISlider
UISlider 就是我们看到的滑动条,让用户能够以可视化的方式设置指定范围内的值。和按钮一样,滑块也能响应事件,还可像文本框一样被读取。如果希望用户对滑块的调整立刻影响应用程序,则需要让他触发操作。
好了废话不说,上代码
self.slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 20)];/**< 初始化 */
self.slider.backgroundColor = [UIColor yellowColor];/**< 设置slider的背景颜色 */
self.slider.minimumValue = 0;/**< 设置最小的值 */
self.slider.maximumValue = 100;/**< 设置最大的值 */
self.slider.value = 50;/**< 设置初始的值 */
/**< Tips:下面说的当前值,是slider.value,在UI中显示为:在白色的圈左边的是小于当前值,在白色的圈右边的是大于当前值 */
self.slider.minimumValueImage = [UIImage imageNamed:@" "];/**< 设置小于当前值的图片 */
self.slider.maximumValueImage = [UIImage imageNamed:@" "];/**< 设置大于当前值的图片 */
self.slider.minimumTrackTintColor = [UIColor redColor];/**< 设置小于当前值的颜色 */
self.slider.maximumTrackTintColor = [UIColor greenColor];/**< 设置大于当前值的颜色 */
self.slider.thumbTintColor = [UIColor grayColor];/**< 设置滑动圈的颜色 */
[self.slider addTarget:self action:@selector(sliderByUser) forControlEvents:UIControlEventValueChanged];/**< 设置监听动作,当slider的值发生变化的时候 */
UISwitch
开关(UISwitch)提供了一个简单的开/关UI元素,类似于传统的物理开关,开关的可配置选项很少,应将其用于处理布尔值。我们使用其Value Changed事件来检测开关切换,并通过属性on或实例方法isOn来获取当前值
直接上代码:
/**< 开关的bounds固定为:宽=51,高=31 */
UISwitch *swt = [[UISwitch alloc]initWithFrame:CGRectMake(0, 60, 0, 0)];
[self.view addSubview:swt];
swt.onTintColor = [UIColor redColor];/**< 开关打开时,的填充颜色,默认为绿色 */
swt.tintColor = [UIColor greenColor];/**< 开关外圈的线的颜色 */
swt.thumbTintColor = [UIColor blueColor];/**< 开关按钮的颜色 */
swt.on = YES;/**< 开关是否是打开的(默认为NO) */
swt.onImage = [UIImage imageNamed:@" "];/**< 开关打开时的图片 */
swt.offImage = [UIImage imageNamed:@" "];/**< 开关关闭时的图片 */
[swt addTarget:self action:@selector(switchByUser:) forControlEvents:UIControlEventValueChanged];/**< 给开关监听事件,当值发生变化的时候 */
UIAlertView
UIAlertView就是所谓的”提示框”或者说”警告框”
直接上代码说 :
- alert的属性
/**< UIAlertView不需要加载在视图上,显示提示款的方法是show */
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"我想要个女朋友" delegate:nil cancelButtonTitle:@"不给就揍你" otherButtonTitles:@"送一个林巧雯", nil];/**< 快速创建一个alert,并给其设置标题,信息,取消按钮,其他按钮的文字,设置代理方法 */
alert.title = @"你想要什么?";/**< 设置提示框的标题 */
alert.message = @"我想要个女朋友";/**< 设置提示框的信息 */
#warning 为什么UIAlertView挂代理不用遵守UIAlertViewDelegate协议呢?
alert.delegate = self; /**< 设置代理 */
NSLog(@"%ld",alert.numberOfButtons);/**< 提示款按钮的数量,这里打印2,这个属性是只读的 */
// alert.cancelButtonIndex = 3;
NSLog(@"%ld",alert.cancelButtonIndex);/**< 取消按钮的index,默认情况下,如果没有取消按钮index=-1,如果有取消按钮index=0 */
NSLog(@"%ld",alert.firstOtherButtonIndex);/**< 如果没有设置其他按钮,那么firstOtherButtonIndex默认为-1,这个属性是只读的 */
NSLog(@"%d",alert.visible);/**< 提示框是否可见,只读 */
/**
* alertViewStyle 有多种类型
* typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
* UIAlertViewStyleDefault = 0, 默认情况
* UIAlertViewStyleSecureTextInput, 会出现password的输入文本
* UIAlertViewStylePlainTextInput, 会出现类似留言的输入文本
* UIAlertViewStyleLoginAndPasswordInput 会出现login、password的输入文本
* };
*/
alert.alertViewStyle = UIAlertViewStyleDefault;
[alert show];/**< 将alert显示到视图上 */
alert代理方法
- (void)willPresentAlertView:(UIAlertView *)alertView/**< before animation and showing view */ /**< 在动画和显示视图之前调用,第一个被调用 */
{
NSLog(@"动画还没有出来我就有了");
}
- (void)didPresentAlertView:(UIAlertView *)alertView /**< after animation */ /**< 在动画之后调用,第二个被调用 */
{
NSLog(@"动画结束了啊?那我出来吧!");
}
// Called when a button is clicked. The view will be automatically dismissed after this call returns
/**< 点击按钮时触发,视图将自动调用之后被返回,第三个被调用 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%ld",buttonIndex);
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex /**< before animation and hiding view */ /**< 在隐藏试图和动画之前调用,将要消失,第四个被调用 */
{
NSLog(@"你们都消失了。。等等我");
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex /**< after animation */ /** 在动画之后调用,已经消失,第五个被调用 */
{
NSLog(@"终于赶上你们了");
}
// Called after edits in any of the default fields added by the style
/**< 设置是否允许第一个按钮不是取消按钮 */
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return YES;
}
/**< 当用户按下HOME键的时候,回调此方法,用户点击Cancel按钮的时候不会回调此方法 */
- (void)alertViewCancel:(UIAlertView *)alertView
{
NSLog(@"我在哪里?");
}
Tips:
1. iOS8 之后就不推荐使用了,现在推荐使用UIAlertController
2. 实现了就叫遵守协议,并不是在<加了才叫遵守>,动态语言特性,关键点在于delegate设置了
UIActionSheet
可以参考banyingli的博客
UIActionSheet的属性
#pragma mark - 属性
UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"你叫我干嘛" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定?" otherButtonTitles:@"你说谁是小个子", nil];/**< 快速创建一个actionsheet */
action.title = @" 我是谁 "; /**< 设置标题 */
action.delegate = self; /**< 挂代理 */
action.numberOfButtons; /**< 按钮的个数,只读,NSInteger */
action.cancelButtonIndex; /**< 如果没有实现 "-actionSheetCancel:" 代理方法,我们假装这个按钮已经被点击。默认值为-1 */
action.destructiveButtonIndex; /**< 设置destructive(红色)按钮。默认的值是-1,-1的意思是没有设置,如果只有一个按钮,这个按钮会被忽略 */
action.firstOtherButtonIndex; /**< 如果没有设置其他按钮的title,或者没有使用initWithTitle: 方法,那么值为-1 */
action.visible; /**< 是否可见 */
/**
* actionSheetStyle
* default is UIActionSheetStyleAutomatic. ignored if alert is visible
* typedef NS_ENUM(NSInteger, UIActionSheetStyle) {
* UIActionSheetStyleAutomatic = -1, // take appearance from toolbar style otherwise uses 'default'
* UIActionSheetStyleDefault = UIBarStyleDefault,
* UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,
* UIActionSheetStyleBlackOpaque = UIBarStyleBlackOpaque,
* };
*/
action.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
#pragma mark - 加载到视图上的方法
[action showInView:self.view];/**< 将actionsheet显示到视图上 */
/**
- (void)showFromToolbar:(UIToolbar *)view;
- (void)showFromTabBar:(UITabBar *)view;
- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
- (void)showInView:(UIView *)view;
*/
UIActionSheet的代理方法
#pragma mark -- UIActionSheetDelegate --
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex/**< 点击按钮时被调用,第三个被调用 */
{
NSLog(@"1");
}
// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
/**< 当用户按下home键时调用 */
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
NSLog(@"2");
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet// before animation and showing view /**< 在动画和视图显示之前调用,第一个被调用 */
{
NSLog(@"3");
}
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet// after animation /**< 在动画完成之后调用,第二个被调用 */
{
NSLog(@"4");
}
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex// before animation and hiding view /**< 点击按钮后,在动画和视图被隐藏之前调用,第四个被调用 */
{
NSLog(@"5");
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex// after animation /**< 点击按钮后,在动画结束之后调用,第五个被调用 */
{
NSLog(@"6");
}
UIAlertController
在iOS8之后推荐使用UIAlertController,而不是使用UIAlertView和UIActionSheet
A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method.
In addition to displaying a message to a user, you can associate actions with your alert controller to give the user a way to respond. For each action you add using the addAction: method, the alert controller configures a button with the action details. When the user taps that action, the alert controller executes the block you provided when creating the action object. Listing 1 shows how to configure an alert with a single action.
/**< UIAlertControllerStyleAlert alert类型
UIAlertControllerStyleActionSheet actionSheet类型
*/
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
/**<
handler : 是一个block类型的,在block里面可以写点击按钮时的操作
*/
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
/**< addAction:(UIAlertAction)添加一个alertAction,一个alertAction就是一个按钮 */
[alert addAction:defaultAction];
/**< UIAlertActionStyleDefault 默认的类型
UIAlertActionStyleCancel 取消类型
UIAlertActionStyleDestructive 警告类型(红色的字体加粗)
*/
UIAlertAction *notDefaultAction = [UIAlertAction actionWithTitle:@"取消取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"取消,我就测试一下");
}];
[alert addAction:notDefaultAction];
/**< 你在设置完AlertAction和Style之后,目前用这个方法加载
类似于UIAlertView里的show方法
类似于UIActionSheet里的showInView方法
completion : block类型 block里面的内容与与弹框同时显示
*/
[self presentViewController:alert animated:YES completion:^{
NSLog(@"我没有点击吧?");
}];