iOS开发之八:UISlider、UISegmentedControl、UIPageControl的使用

本文的三种控件,用的也非常多,而我也是经常图懒,而去打开原来的项目去拷贝,现在记录一下,就不用去项目中去找这些控件的用法了。

一、UIActivityIndicatorView 的使用

UIActivityIndicatorView 俗称“风火轮”,也有人称之为菊花,安装黑苹果系统时,远景论坛上都称之为菊花。大笑 它主要是用来告诉用户当前正在加载数据,让用户等待一下。长这个样子的:

它的常用属性和方法也比较少:

// 设置风格
@property(nonatomic) UIActivityIndicatorViewStyle
activityIndicatorViewStyle;
// 停止时,隐藏视图,默认为YES
@property(nonatomic) BOOL hidesWhenStopped;
// 修改颜色,注意版本问题
@property (readwrite, nonatomic, retain) UIColor *color
// 开始动画
- (void)startAnimating;
// 停止动画
- (void)stopAnimating;
// 判断动画的状态(停止或开始)
- (BOOL)isAnimating;
使用UIActivityIndicatorView  的示例代码如下:

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = CGPointMake(160, 200);
[activityView startAnimating];
activityView.hidesWhenStopped = NO;
[self.window addSubview:activityView];
[activityView release];
    
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(test:) userInfo:activityView repeats:NO];
    
 //状态栏也显示风火轮
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
把定时器的方法也记录一下吧,设置的是3秒之后隐藏“风火轮”。

- (void)test:(NSTimer *)timer
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    
    UIActivityIndicatorView *activityView = [timer userInfo];
    [activityView stopAnimating];
注意:苹果自带的“风火轮”效果有时候不能满足我们的需要,我们可以用第三方的框架 MBProgressHUD,它有多种效果,可以附带图片,或者附带文字,还可以改装成安卓里的toast。至于MBProgressHUD的使用,我就不介绍了,给个传送门:MBProgressHUD的使用,这是别人写的。里面还有github的下载地址。

推荐一个demo网站:code4app,里面也有MBProgressHUD的使用demo----》demo资料


二、UIAlertView的使用

UIAlertView 是用来提示用户,供用户选择的,我印象里,它是会阻塞线程的。而且窗口的级别非常高。
直接上代码好了:
//UIAlertView 初始化
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题" message:@"提示文本信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
[alertView release];
上面的delegate参数设置时,需要实现UIAlertViewDelegate中的方法:
#pragma mark - AlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"buttonIndex : %d", buttonIndex);
}
我们可以根据buttonIndex来区分用户点击的是哪一按钮,来执行不同的操作。

三、UIActionSheet的使用
这里需要注意的是,actionSheet showInView这个方法,将其添加的位置不对,会造成有时候点击没有反应的情况。
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"destructive" otherButtonTitles:@"other1", @"other2", @"other3", @"other3", nil] autorelease];
[actionSheet showInView:self.window];// 这里我经常这样写:[actionSheet showInView:[UIApplication shareApplication].keyWindow];
[actionSheet release];
同理如果你需要根据不同按钮触发不能的操作的话,也是要实现其delegate方法。
#pragma mark - ActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"clickedButtonAtIndex : %d", buttonIndex);
}
我们可以根据buttonIndex来区分用户点击的是哪一按钮,来执行不同的操作。

原文地址:https://www.cnblogs.com/wanghang/p/6298896.html