UIProgressView

  • 属性
    • Style
      • Default:默认风格
      • Bar:工具条风格
        +Progress
      • 设置0.0~1.0之间的浮点值,1.0代表全部完成。
        +Progress Tint
      • 已完成进度的颜色
    • Track Tink
      • 设置进度条轨迹颜色。
      • progressImage:设置该进度条完成部分的图片
      • tracjImage:设置进度条的轨道图片
  • ImageView图像拉伸
    • 默认情况下,iOS将会对该图像整体缩放,从而让图片被拉伸、变形。
      +为了精确地控制只对制定区域进行缩放,可通过uIImage来创建“可拉伸”图片。当通过一个UIEdgeInsets结构体(包括left、top、right、bottom)定义图片的拉伸区域。

  • 在NDk的目录下有一个例子\android-ndk\samples\hello-jni
  1. 将它导入到eclipse开发环境中(最好选择cpoy到自己的工作空间)
  2. 在命令行进入该项目的路径,执行ndk-build命令,编译程序
  3. 在eclipse上试运行(注:在4.x的版本要将Dependencies包去掉,貌似这个包是做低版本支持的,在高版本中会出错)
  4. 运行成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//hello-jni.c
# include <string.h>
# include <jni.h>
// <jni.h>用于java的调用
jstring
// 名字即包名加函数名
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz )
{
# if defined(__arm__)
# if defined(__ARM_ARCH_7A__)
# if defined(__ARM_NEON__)
# define ABI "armeabi-v7a/NEON"
# else
# define ABI "armeabi-v7a"
# endif
# else
# define ABI "armeabi"
# endif
# elif defined(__i386__)
# define ABI "x86"
# elif defined(__mips__)
# define ABI "mips"
# else
# define ABI "unknown"
# endif
// env为java与C交互的结构,返回UTF编码的字符串
return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI ".");
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class HelloJni extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText(stringFromJNI() );
setContentView(tv);
}
// 声明一个native方法,即.c的函数
public native String stringFromJNI();
public native String unimplementedStringFromJNI();
static {
System.loadLibrary("hello-jni");
}
}

在adb shell中运行c程序

  • \android-ndk\samples\test-libstdc++下有这样一个例子,我们进行修改后然后在设备上运行它。
  1. 将jni目录下的test-libstl.cpp的文件进行修改
1
2
3
4
5
6
7
8
# include <cerrno>
# include <cstddef>
# include <stdio.h>
int main(void)
{
printf("Zoe\n");
return 0;
}
  1. 在命令行下使用ndk-build编译该项目
  2. 将项目目录下的\libs\armeabi\test-libstl文件拷贝到设备的/data/data目录下(你需要该目录的写权限)
  3. 进入设备的shell,并获取权限
  4. 切换到/data/data目录,修改test-libstl文件的权限,并执行它

编写自己的程序

  1. 在任意目录下,创建jni文件夹
  2. 创建一个你的程序,如:MyDemo.c
1
2
3
4
5
6
7
# include <stdio.h>
void main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
printf("%d+%d+%d=%d\n",a,b,c,a+b+c);
}
  1. 创建Android.mk文件,注意LOCAL_MODULELOCAL_SRC_FILES属性
1
2
3
4
5
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyDemo
LOCAL_SRC_FILES := MyDemo.c
include $(BUILD_EXECUTABLE)
  1. 按上一个例子编译、拷贝、执行

  • 继承了UIScrollView:UIView控件,默认带有滚动条
  • UITextView与UITextField的区别
    • UITextView是一个多行文本框,UITextField只是单行文本
    • UITextView没有继承UIControl控件
    • UITextView继承了UIScrollView,所以它具有UISrollView的功能和行为。

UIScrollView支持的属性

  • 控制显示区域的属性
    • contentSize:该属性是一个CGSize类型的值,制定显示内容的完整宽度和完整高度。
    • contentInset:该属性是一个UIEdgeInsets类型的值,表示所需要内容在上下左右的留白。
    • contentOffset:该属性是一个CGPoint值,表示可视区域在显示内容上滚动的距离。
  • 属性设置
    1.Scrollers
    • Shows Horizontal Scrllers:当用户水平滚动该控件时,该控件会显示水平滚动条。
    • Shows Vertical Scrollers: 当用户垂直滚动该控件时,该控件会显示垂直滚动条。
    • Scrolling Enabled:控件能否滚动。
    • Paging Enabled:是否分页。
    • Direction Lock Enabled:第一次滚动后,不允许向其他方向滚动。
  1. Bounce
    • Bounce :弹回效果。
    • Bounce Horizontally:水平弹回效果。
    • Bounce Vertically:垂直弹回效果。
  2. Zoom
    • Min 最小的可缩放比例。
    • Max 最大的可缩放比例。
  3. Touch
    • Bounces Zoom:内容缩放是否有弹性。
    • Delays Content Touches:真正确定滚动意图才去处理触碰手势。
    • Cancellable Content Touches:勾选后,如果该UIScrollView中的内容已经跟踪用户手势触碰动作的情况下,且用户拖动手指足以启动一个滚动事件,该UISrollView控件会调用touchesCancelled:withEvent:方法,并将该手指拖动事件当成滚动该UISroll控件。如果不勾选,UIScrollView控件已经跟踪某个手指动作,将不会理会其他的手指动作。
  • 使用委托对象处理UITextView事件
    • 委托对象必须实现UITextViewDelegate协议,协议定义了如下方法。
    1. -textViewShouldBeginEdting:将要开始编辑内容时回调。
    2. -textViewDidBeginEdting:开始编辑内容时回调。
    3. -textViewShouldEndEdting:将要结束时回调。
    4. -textViewDidEndEdting:结束编辑时回调。
    5. -textView:shouldChangeTextInRange:replacementText:指定范围内的文本内容将要被替换是回调。
    6. -textViewDidChange:文本内容发生改变时回调。
    7. -textViewDidChangeSelection:文本内某些文本改变时回调。
  • 实例

  1. 创建控制器. File->New File->Iphone OS->Cocoa Touch Class->UIViewController subclass;
  2. 创建xib. File->New File->Iphone OS->User Interface->View XIB
  3. 绑定controller和view. 用Interface Builder打开xxx.xib, 点击Files’ Owner, 在Identity Inspector里面的Class Identity, 选择Step 1创建的控制器类, 接着拖拽File’s Owner到View中, 选择Outlets->view.先选中file’s owner(这个很重要)


定制iOS应用图标

  • 需要57X57、114X114、120X120的图片作为图标。
  • 需要320X480、640X960、640X1136的素材作为启动画面。

UIImageView

  • 一个显示图片控件,直接继承了UIView基类,没有继承UIControl,不能接受用户输入,不能与用户交互。
  • 常用属性
    image:访问或设置该控件显示的图片
    highlighteImage:访问或设置该控件出于高亮状态时显示的图片
  • UIImage还可以使用动画显示一组图片
    animationImages:访问或者设置该UIImageView需要动画显示的多张图片,该属性的值为NSArray对象。
    highlightedAnimationImages:访问或设置该UIImageView高亮状态下需要动画显示的多张图片,该属性的值为NSArray对象。
    animationDuration:访问或设置该UIImageView的动画持续时间。
    animationRepeatCout:访问或设置该UIImageView的动画重复次数。
    startAnimating:开始播放动画。
    stopAnimating:停止播放动画。
    isAnimationg:该方法判断该UIImageView是否正在播放动画。
  • 缩放模式
    Scale To Fill:不保持纵横比缩放图片,使图片完全适应该UIImageView控件。
    Aspect Fit:保持纵横比缩放图片,使图片的长边能完全显示出来。也就是说,可以完整地将图片显示出来。
    Aspect Fill:保持纵横比缩放图片,只保证短边完全显示出来。
    Center:不缩放图片,只显示图片的中间区域。
    Top:不缩放图片,只显示图片的顶部区域。
    Bottom:不缩放图片,只显示图片的底部区域。
    Left:不缩放图片,只显示图片的左边区域。
    Right:不缩放图片,只显示图片的右边区域。
    Top Right:不缩放图片,只显示图片的右上边区域。
    Top Left:不缩放图片,只显示图片的左上边区域。
    Bottom Left:不缩放图片,只显示图片的左下边区域。
    Bottom Right:不缩放图片,只显示图片的右下边区域。

UIAlertView(警告框)

  • UIAlertView
    • 显示在屏幕中央的弹出式警告框。
    • 基本用法
      1. 创建UIAlertView,指定标题、消息内容、包含多少个按钮、指定UIAlertViewDelegate委托对象。
      2. 调用UIAlertView显示出来。
      3. 实现UIAlertViewDelegate协议中的方法。
    • 实例代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    -(void)viewDidLoad
    {
    [super viewDidLoad];
    }
    -(IBAction)clicked:(id)sender
    {
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"提示"
    message:@"警告框信息"
    deledgate:self
    cancelButtonTitle:@"确定"
    otherButtonTitles:@"按钮一",@"按钮二",@"按钮三",nil];
    [alert show];
    }
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    NSString* msg = [NSString stringWithFormat:@"你点击了第%d按钮",buttonIndex];
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"提示"
    message:msg
    delegate:nil
    cancelButtonTitle:@"确定"
    otherButtonTitles:nil];
    [alert show]
    }
    • UIAlertViewDelegate协议中常用的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex:
//当用户点击该警告框中某个按钮时激发该方法,buttonIndex参数代表用户单击按钮的索引,索引从0开始。
-(void)willPresenAlertView:(UIAlertView *)alertView:
//当该警告框将要显示出来时将会激发该方法。
-(void)didPresenAlertView:(UIAlertView *)alertView:
//当该警告框将要显示出来后将会激发该方法。
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView:
//当该警告框第一个非Cancel按钮被启用时激发该方法。
-(void)alertView:(UIAlertView *)alerView willDismissWithButtonIndex:(NSInteger)buttonIndex:
//当用户单击某个按钮将要隐藏该警告框时激发该用法。
-(void)alertViewCancel:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInterger)buttonIndex:
//当用户通过单击某个按钮完全隐藏该警告框时激发该方法。
-(void)alertViewCancel:(UIAlertView *)alertView:
//当该警告框被取消时(如用户单击了Home键)激发该方法。
+ 带输入框的UIAlertView
    - UIAlertView支持actionSheetStyle属性,用于设置该UIAlert的风格,支持如下枚举值:
    
    `UIAlertViewStyleDefault`:默认的警告框风格。
    `UIAlertViewStyleSecureTextInput`:警告框中包含一个密码输入框。
    `UIAlertViewStylePlainTextInput`:警告框中包含一个普通的输入框。
    `UIAlertViewStyleLoginAndPasswordInput`:警告框中包含用户名、密码两个输入框。
+ 实例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-(void)viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)clicked:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"登录"
message:@""
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定",nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert textFieldAtIndex:1].keyboardType = UIKeyboardTypeNumberPad;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInterger)buttonIndex
{
if(buttonIndex == 1){
UITextField* nameField = [alertView textFieldAtIndex:0];
UITextField* passField = [alertView textFieldAtIndex:1];
NSString* msg = [NSString stringWithFormat:"输入的用户名为:%@,密码为%@",nameField.text,passField.text];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
}
-(void)willPresentAlerView:(UIAlertView *)alertView
{
for(UIView * view in alertView.subViews)
{
if([view isKindOfClass[UILabel class])
{
UILabel* label = (UILabel*)view;
label.textAlignment=UITextAlignmentLeft;
}
}
}

UIActionSheet

  • 显示在底部的按钮列表,用户通过单击某个按钮来表明自己的态度。

    • 风格(actionSheetStyle):

    UIActionSheetStyleDefault:默认风格,灰色背景上显示白色文字。
    UIActionSheetStyleBlackTranslucent:在透明的黑色背景上显示白色文字。
    UIActionSheetStyleBlackOpaque:在纯黑的背景上显示白色文字

  • 示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-(void)viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)clicked:(id)sender{
UIActionSheet* sheet = [[UIActionSheet alloc]
initWithTitle:@"请确认是否确认"
delegate:self
cancelButton:@"取消"
destrutiveButtonTitle:@"确定"
otherButtonTitle:@"按钮一",@"按钮二",nil];
sheet.actionSheetStyle = UIActionSheetStyleAutomaic;
[sheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:[NSString stringWithFormat:@"你单击了第%d个按钮",buttonIndex
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}

Mac版

  1. 到Google的官网下载Andorid NDK

  2. 解压到你要存放的位置

  3. 开启终端,输入命令pico .bash_profile

  4. 添加NDK的绝对路径
    export PATH=${PATH}:/Users/userName/Documents/android-ndk-r9c
    A_NDK_ROOT=/Users/userName/Documents/android-ndk-r8d
    export A_NDK_ROOT

    • 注:图片中有SDK的部分,具体路径视个人情况而定。
  5. 测试:关闭终端后重启,输入ndk-build,出现以下提示,证明配置成功

Windows版

  1. 到Google的官网下载Andorid NDK
  2. 解压到你要存放的位置
  3. 计算机-属性-高级系统设置-高级-环境变量-系统环境变量-编辑Path变量-添加NDK的存放路径
  4. 测试:在命令行中输入ndk-build,出现以下提示,证明配置成功

  • UINavigationBar
    • 一个导航栏一般包括四个对象:UINavigationController、UINavigationBar、UIViewControllerUINavigationItem;其中UINavigationItem存放在UINavigationBar上。
  • 导航栏结构剖析图

  • 定制标题视图
    • 通过NavigationItem的titleView属性,定制标题视图,titleView属性是一个视图类,因此可以添加一个UIView的实例,也可以添加UIView的子类,也可以在UIView的实例中添加子视图。
    1
    2
    3
    4
    UIView *cView = [[UIView alloc]initWithFrame:CGRectMake(0,0,160,44)];
    cView = [UIColor redColor];
    self.navigationItem.titleView = cView;
    [cView release];
  • 定制左、右栏目
    • NavigationItem实例有一个leftBarButtonItem和reightBarButtonItem,而这两个属性又是UIBatButtonItem的实例,可以通过初始化实例,定制。
    • UIBarButtonItem类提供了常用的四个初始化方法,通过这些不同的初始化方法,用户可以得到不同风格的Item。
1
2
3
4
5
6
7
8
// 初始化一个UIBarButtonItem的实例,初始化一个系统的Item
-(id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem taget:(id)target action:(SEL)action;
// 初始化一个带图片的UIButtonItem实例
-(id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
// 初始化一个自带标题的UIBarButtonItem实例
-(id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
// 初始化一个自定义视图
-(id)initWithCustomView:(UIView *)customView;
  • 设置风格
1
2
3
4
// 设置导航栏的风格为黑色
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
// 设置导航栏为透明
self.navigationController.navigationBar.translucent = YES;
  • 设置颜色
1
2
3
4
5
6
// 设置导航栏的颜色
self.navigationController.navigationBar。tintColor = [UIColor redColor];
// 设置自定义颜色,注意每一个颜色的范围是0~1之间
[UIColor colorWithRed:0/255.0 green:225/255.0 blue:122/255.0 alpha:1];
// 以图片作为颜色,注意这里是无法设置NavigationBar
[UIColor colorWithPatternImage:[UIImage imageNamed:@"img.png"]];
  • 隐藏返回按钮
1
2
3
// 隐藏返回按钮,后者带动画效果
[self.navigationItem setHidesBackButton:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
  • 设置prompt属性
    • 导航栏的prompt属性,通过navigationItem来设置,其主要作用是用于提示用户。比如,用户正在请求网络数据时,提示用户数据正在被加载,待加载完成后可以将它的值设置为nil,取消显示。

工具栏

  • 创建UIToolBar的实例
1
2
3
4
5
6
7
UIToolBar *toolBar = [[UIToolBar alloc]initWithFrame:CFrectMake(0,460-88,320,44)];
// 初始化UIToolBar的UIBarButtonItem实例,与UINavigationItem中左、右栏目是相同的
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"天气" style:UIBarButtonItemStyleBordered target:self action:nil];
// 向UIToolBar添加UIBarButtonItem
NSArray *itemArray = [NSArray arrayWithObjects:item1,nil];
// 设置UIToolBar的间隔,前者是item之前选择一个合适的列宽,后者用户可以自定义item之间的列宽,需要通过UIBar实例中的width属性来设置。
UIBarButtonSystemItemFlexibleSpace|UIBarButtonSystemItemFixedSpace
  • 导航控制器中的UIToolBar
    • 在导航控制器中会带有一个UIToolBar的实例,但默认是隐藏的,如果需要显示,需要通过[self.navigationController setToolbarHidden:No animated:YES]将其打开。
    • 导航控制器只拥有一个UIToolBar实例,但UIToolBar所拥有的UIBarButtonItem实例,由视图控制器管理。
1
2
3
4
// 将UIBarButtonItem放入数组中,最后添加至UIToolBar中,self表示视图控制器
[self setToolbarItems:itemArray animated:YES];
// 以下代码UIBarButtonItem不会出现在UIToolBar中,且toolbar是制度属性
[self.navigationController.toolbar setItem:itemsArray animated:YES];
1
2
3
4
5
6
7
8
9
10
11
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,460-44-44,320,44)];
toolBar.barStyle = UIBarStyleDefault;
[self.view addSubView:toolBar];
[toolBar release];
UIBarButtonItem *addItem = [[UIBarButton alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *saveItem = [[UIBarButton alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:nil];
UIBarButtonItem *titleItem = [[UIBarButton alloc]initWithTitle:@"title" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *imageItem = [[UIBarButton alloc]initWithImage:[UIImage imageNamed:@"1"] style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *flexibleItem = [[UIBar]
NSArray *items = @[addItem,saveItem,titleItem,imageItem];
[toolBar setItems:items animated:Yes];

  • UIControl
    • 作用:具有事件处理的控件的父类
    • 事件响应的3种形式:基于触摸、基于值、基于编辑
  • 常用方法
1
2
-(void)addTarget:(id)taget action:(SEL)action forControlEvent:(UIControlEvents)controlEvents //添加一个事件
-(void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents // 移除某一个事件
  • 事件处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 按下时触发
UIControlEventTouchDown
// 多击触发
UIControlEventTouchDownRepeat
// 控件内拖动触发
UIControlEventTouchDragInside
// 控件外拖动触发
UIControlEventTouchDrageOutside
// 当触摸从控件外拖动到控件内部时
UIControlEventTouchDragEnter
// 当触摸从控件内拖动到控件外部时
UIControlEventTouchDragExit
// 控件之内触摸抬起时
UIControlEventTouchupInside
// 控件之外触摸抬起时
UIControlEventTouchUpOutside
// 触摸事件取消
UIControlEventTouchCancel
// 当控件的值发生改变时
UIControlEventTouchValueChanged
// 文本控件中开始编辑
UIControlEventEditingDidBegin
// 文本控件中的文本被改变
UIControlEventEditingChanged
// 文本孔家中编辑结束
UIControlEventEditingDidEnd
// 文本孔家中通过按下回车键结束编辑时
UIControlEventEditingDidOnExit
// 所有触摸事件
UIControlEventAlltouchEvents
// 所有编辑事件
UIControlEventAllEditingEvents
// 所有事件
UIControlEventAllEvents

导航控制器

  • 基本概念
    +UINavigationController用于构建分层应用程序的主要工具,管理着多个内容视图的换入(压入)和换出(弹出)。自身提供了视图切换的动画效果。
    • 它的父类是UIViewController,是所有视图控件器的基类。
    • 导航控制器以栈的形式来实现。
  • 栈的基本概念和性质
    • 栈是一种先进后出(后进先出)的数据结构,导航控制器以栈的形式来管理视图控制器,任何视图控制器都可以放入栈中。
    • 向栈添加一个对象的操作称为入栈(push)
    • 第一个入栈的对象叫做基栈
    • 最后一个入栈的对象叫栈顶
    • 栈删除一个对象的操作叫做出栈(pop)
    • 当前现实点视图控制器,即为栈顶。选择”返回”时,这个视图控制器就出栈了。
  • 基本样式
    • 蓝色部分:导航控制器的导航栏(NavigationBar)
    • 橙色部分:控制器包含的内容视图
    • 绿色部分:导航控制器的工具栏(UIToolBar,默认是隐藏的)
  • 元素尺寸
  • 代码示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 控制器的初始化,为控制器添加导航控制器
RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:rootVC];
// 子控制器设置title,显示在导航栏上的标题
self.title = @"根控制器";
// 控制器之间的导航
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
// 隐藏(显示)导航栏、工具栏目
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.navigationController setToolbarHidden:NO animated:YES];
// 延时调用hidden方法
[self performSelector:@selector(hidden) withObject:nil afterDelay:0.3];
  • Demo代码片段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// AppDelegate.m
// didFinshLunchingWithOptions()
// ...
RootViewcController * rootViewController = [[RootViewcController alloc] init];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:rootViewController];
self.window.rootViewController = navigation;
[rootViewController release];
[navigation release];
//RootController.m
-(void)loagView
{
UIView *baseView = [[UIView alloc] initWithFrame:[UIScreen mainScreen] applicationFrame];
baseView.backgroundColor = [UIColor purpleColor];
self.view = baseView;
[baseView release];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Push" forState:UIControlStateNormal];
[button setFrame:CGRectMake(90,100,140,35)];
[button addTaget:self action:@selector(pushVC) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
-(void)pushVC
{
SencondViewController *secondVC = [[SencondViewController alloc] init];
[self navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
//SencondViewController.m
-(void)loagView
{
UIView *baseView = [[UIView alloc] initWithFrame:[UIScreen mainScreen] applicationFrame];
baseView.backgroundColor = [UIColor orangeColor];
self.view = baseView;
[baseView release];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"HiidenOrShow" forState:UIControlStateNormal];
[button setFrame:CGRectMake(90,150,140,35)];
[button addTaget:self action:@selector(hiddenOrShow) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];

UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[back setTitle:@"backRootVC" forState:UIControlStateNormal];
[back setFrame:CGRectMake(90,200,140,35)];
[back addTaget:self action:@selector(hiddenOrShow) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:back];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setToolBarHidden:YES animated:YES];
}
-(void)hiddenOrShow
{
if(self.navigationController.toolbarHidden)
{
[self.navigationController setToolBarHidden:NO animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}else{
[self.navigationController setToolBarHidden:YES animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
-(void)backRootVC
{
[self.navigationController popViewControllerAnimated:YES];
}
  • 导航控制器常用属性
1
2
3
4
5
6
7
8
9
10
// 获取到在栈中最顶层的视图控制器
@property(nonatomic,readonly,retain)UIViewController *topViewController;
// 获取到在斩重当前显示的视图控制器
@property(nonatomic,readonly,retain)UIViewController *visibleViewController;
// 在栈中当前视图控制器
@property(nonatomic,copy)NSAraay *viewControllers;
// 隐藏导航栏,默认不隐藏
@property(nonatomic,getter=isNavigationBarHidden)BOOL navigationBarHidden;
// 获取到导航栏
@property(nonatomic,readonly)UINavigationBar *navigatuonBar;
  • 导航控制器常用方法
1
2
3
4
5
6
7
8
9
10
// 初始化一个根视图控制器,在栈的最底层
-(id)initWithRootViewController:(UIViewController *)rootViewController;
// 压入一个新的视图控制器
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
// 弹出一个新的视图控制器
-(UIViewController *)popViewControllerAnimated:(BOOL)animated;
// 弹出到指定的视图控制器
-(NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
// 回到跟视图控制器
-(NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

UIButton

  • 常用属性

1.Type
+ Custom:选择此属性外观主要依靠自定义实现。
+ System:系统能够默认风格。
+ Detail Disclosure:详情标示。
+ Info Light:显示“i”图标的图形按钮。
+ IndoDark:显示“i”图标的图形按钮。
+ Add Contact:显示黑色“+”图标图形按钮。
2. State Config
- 按钮的状态
+ 默认状态(Default)
+ 高亮状态(Highlighted)
+ 选中状态(Selected)
+ 禁用状态(Disable)
3. Title
- 可以选择Plain和Attributed文本方式
- 制定显示的字符信息
4. Font
- 文本字体、大小、字体风格
5. Text Color
- 文本的颜色
6. Shadow Color
- 阴影颜色
7. Image
- 设置为图片按钮,Title属性将不会起作用。
8. Background
- 设置背景图片
9. Shadow Offset
- 文本和阴影的偏移量
10. Link Break
- 省略方式
11. Edge
- Content:内容作为边界
- Title:文本作为边界
- Image:图片作为边界
12. Inset
- 边界距离

UIAlert And UIActionSheet

1
2
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cencelButtonTitle:@"cancel" otherButtonTitles:@"other",nil];
[alertView show];
1
2
3
4
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWIthTitle:@"title" delegate:nil cancelButtonTitle:@"cancel"
destructiveButtonTitle:@"destrutive"
otherButtonTitles:@"other1",nil];
[actionSheet show];

UIButton

  • UIButton
    • 作用:响应用户的点击事件的View
  • 常用方法
1
2
3
4
5
6
7
8
9
10
// 设置指定状态对应的标题文本
-(void)setTitle:(NSString *)title forState:(UIControlState)state;
// 设置指定状态对应的标题颜色
-(void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
// 设置指定状态对应的显示图片
-(void)setImage:(UIImage *)image forState:(UIControlState)state;
// 设置指定状态对应的背景图片
-(void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
// 为按钮添加事件
-(void)addtarget:(id *)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
  • UIButton状态
1
2
3
4
5
6
UICtrolStateNormal          // 正常状态
UICtrolStateHighlighted // 高亮状态
UICtrolStateDisabled // 禁用状态
UICtrolStateSelected // 选中状态
UICtrolStateApplication
UICtrolStateReserved
  • 事件处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 用户按下时触发
UIControlEventTouchDown
// 点击计数大于1的触发
UIControlEventTouchDownRepeat
// 当触摸在控件内部拖动时触发
UIControlEventTouchDragInside
// 当触摸在控件之外拖动时触发
UIControlEventTouchDragDutside
// 当触摸从控件之外拖到内部时
UIControlEventTouchDragEnter
// 当触摸从控件内部拖到外部时
UIControlEventTouchDragExit
// 控件之内触摸抬起时
UIControlEventTouchUpInside
// 控件之外触摸抬起时
UIControlEventTouchUpOutside
// 触摸取消时间,设备被上锁或者电话呼叫打断
UIControlEventTouchCanel
  • 代码示例
1
2
3
4
5
6
7
UIButton *button = [UIButton buttonWithType:UIButttonTypeRoundedRect];
[button setTitle:@"Normal" forState:UIControlNormal];
[button setTitle:@"Highlighted" forState:UIControlHighlighted];
[button setTitle:@"Disabled" forState:UIControlDisabled];
[button setTitle:@"Selected" forState:UIControlSelected];
button.frame = CGRectMake(90,100,140,40);
[self.window.addSubview:button];

图片视图

  • UIImageView

    • 作用:专用于显示图片的视图
  • 常用属性和方法

1
2
3
4
5
6
7
8
9
10
// 初始化图片
-(id)initWithImage:(UIImage *)image;
// 初始化高亮的图片
-(id)initWithImage:(UIImage *)image hightlightedImage:(UIImage *)highlightedImage
// 点语法设置图片
@property(nonatomic,retain)UIImage *image;
// 点语法设置高亮图片
@property(nonatomic,retain)UIImage *highlightedImage;
// 是否打开用户交互,默认为NO
@preperty(nonatomic,getter=isUserInteractionEnabled)BOOL userInteractionEnabled;
  • 示例代码
1
2
3
4
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
imageView.image = [UIImage imageNamed:@"t.png"];
imageView.highlightImage = [UIImage imageNamed:@"t1.png"];
imageView.userInteractionEnabled = YES;

UIActivityIndicatorView

  • UIActivityIndicatorView
    • 作用:提示用户当前页面正在加载数据
  • 常用属性和方法
1
2
3
4
5
6
7
8
9
10
11
12
// 设置风格
@property(nonatomic)UIActivityIndicatorViewStylr;
// 停止时、隐藏视图,默认为YES
@property(nonatomic)BOOL hideWhenStopped;
// 修改颜色、注意版本问题
@property(readWrite,nonatomic,retain)UIColor *color;
// 开始动画
-(void)startAnimating;
// 停止动画
-(void)stopAnimating;
// 判断动画的状态(停止或开始)
-(BOOL)isAnimating;
  • 示例代码
1
2
3
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(0,0,100,100);
[activityView startAnimating];

滑动控件

  • UISlider视图
    • 作用:控制系统声音,或者表示播放进度等等
  • 常用属性
1
2
3
4
5
6
7
8
9
10
11
12
// 设置获取slider的value值
@property(nonatomic)float value;
// 设置slider的最小值
@property(nonatomic)float minimumValue;
// 设置slider的最大值
@property(nonatomic)float maximumValue;
// 设置图片
@property(nonatomic,retain)UIImage *minimumValueImage;
// 设置图片
@property(nonatomic,retain)UIImage *maximumValueImage;
// 设置slider的value值,是否存在动画
-(void)setValue:(float)value animated:(BOOL)animated;
  • 示例代码
1
2
3
4
5
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20,0,150,25)];
[slider addTarget:self action:@selector(sliderAction:) forControlEvent:UIControlEventValueChanged];
slider.maxmumValue = 100;
slider.minmumValue = 0;
slider.value = 50;

分段控件

  • UISegmentedControl
    • 作用:分段控件、页面切换等
  • 示例代码
1
2
3
4
5
6
NSArray *array = [NSArray arrayWithObjects:@"选择",@"搜索",@"工具",nil];
UISegmentedControl *segmentCtrl = [[UISegmentedControl alloc] initWithItems:array];
segmentCtrl.frame = CGRectMake(20,0,150,25);
segmentCtrl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentCtrl.selectedSegmentIndex = 0;
[segmentCtrl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

分页控件

  • UIPageControl
    • 作用:通常和UIScrollView连用,提示用户当前显示页数
  • 常用属性和方法
1
2
3
4
5
6
7
8
// 共有几个分页“圆圈”
@property(nonatomic)NSInteger numberOfPages;
// 显示当前的页
@property(nonatomic)NSInteger currentPage;
// 只存在一页时,是否隐藏,默认为YES
@property(nonatomic)BOOL hidesForSinglePage;
// 刷新视图
-(void)updateCurrentPageDisplay;
  • 示例代码
1
2
3
4
5
UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0,100,320,40)];
pageControl.numberOfPages = 10;
pageControl.currentPage = 2;
pageControl.backgroundColor = [UIColor grayColor];
[pageControl addTarget:self action:@selector(change:) froControlEvents:UIControlEventValueChanged];

UITextField

  • 常用代理方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 将要开始输入时调用
-(BOOL)textFieldShoudBeginEdting:(UITextField *)textField{
return YES;
}
// 将要输入结束时调用
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
// 清除文字按钮点击事件
-(BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
// 键盘上的return按钮
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}

UILabel

  • UILabel
    • 作用:显示文本
  • 常用属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 设置文本内容,默认为nil
@property(nonatomic,copy)NSString *text;
// 设置字体大小
@property(nonatomic,retain)UIFont *font;
// 设置字体颜色
@property(nonatomic,retain)UIColor *textColor;
// 设置阴影,默认没有阴影,若果设置需要设置偏移量
@property(nonatomic,retain)UIColor *shadowColor;
// 设置偏移量
@property(nonatomic)CGSize shadowOffset;
// 文本内容对齐方式
@property(nonatomic)NSTextAlignment textAlignment;
// 当文本超出frame时,文本截取的方式
@property(nonatomic)NSLineBreakMode lineBreakMode;
// 文本选中时,高亮的颜色
@property(nanatomic,retain)UIColor *highlightedTextColor;
// 是否存在高亮,默认为nil
@property(nanatomic,getter=isHighlighted)BOOL highlighted;
// 交互是否打开,默认为NO
@property(nanatomic,getter=isUserInteractionEnabled)BOOL userInteractionEnabled;
  • 示例代码
1
2
3
4
5
6
7
8
9
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(90,100,140,40)];
label.text = @"text";
label.backgroundColor = [UIColor redColor];
label.textAligment = NSTextAligmentCenter;
label.textColor = [UIColor blueColor];
label.shadowColor = [UIColor yellowColor];
label.shadowOffset = CGSizeMake(-2,2);
[self.window addSubview:label];
[label release];

  • 代理是一种简单而功能强大的设计模式,这种模式用于一个对象“代表”另外一个对象和程序中其他的对象进行交互。 主对象(这里指的是delegating object)中维护一个代理(delegate)的引用并且在合适的时候向这个代理发送消息。这个消息通知“代理”主对象即将处理或是已经处理完了某一个事件。这个代理可以通过更新自己或是其它对象的UI界面或是其它状态来响应主对象所发送过来的这个事件的消息。或是在某些情况下能返回一个值来影响其它即将发生的事件该如何来处理。代理的主要价值是它可以让你容易的定制各种对象的行为。注意这里的代理是个名词,它本身是一个对象,这个对象是专门代表被代理对象来和程序中其他对象打交道的。
1
2
3
4
5
6
7
8
9
10
11
12
13
// RootViewController.h
# import "ModalViewContorller.h"
@optional
//optional 强制必须实现的方法
-(void)changeLabelText:(NSString *)text;
@end
@interface ModalViewController:UIViewController
{
@private
UITextField *textFiled;
}
@property(nonatomic,assign)id <ModalViewControllerDelegate> delegate;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// RootViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = 101;
button.frame = CGRectMake(320/2-140/2,200,140,40);
[button setTile:@"进入" forState:UIControlStateNormal];
[button addTarget:self action:@selector(presentModalVC) forControlEvent:UIControlEventTouchUpInside];
[self.view addSubiew:button];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(90,100,140,40)];
label.text = @"hello world";
label.tag = 102;
label.textAligment = NSTextAligmentCenter;
[self.view addSubView:label];
}
-(void)presentModalVC
{
ModalViewController *modalVc = [[ModalViewController alloc] init];
modalVc.delegate = self;
// 动画效果
modalVc.modalTranstionSyle = UIModalTransitionStylePatialCurl;
if([[UIDevice currentDevice].systemVersion floatValue] < 6.0){
[self.presentModalViewController:modalVc animated:YES];
}else{
[self.presentModalViewController:modalVc animated:YES completion:^{
NSLog(@"call back");
}];
[modalVC relese];
}
}
-(void)changeLabelText:(NSString *)text
{
UILabel *label = (UILable *)[self.view viewWithTag:102];
label.text = text;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
// ModalViewController.h
@protocol ModalViewControllerDelegate <NSObject>
@optional
//optional 强制必须实现的方法
-(void)changeLabelText:(NSString *)text;
@end
@interface ModalViewController:UIViewController
{
@private
UITextField *textFiled;
}
@property(nonatomic,assign)id <ModalViewControllerDelegate> delegate;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//ModalViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];

*textField = [[UIText alloc] initWithFrame:CGRectMake(70,100,100,30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(320/2-140/2,100,140,40);
[button setTitle:@"dismiss" forState:UIControlEvent:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)dismiss
{
if([self.delegate respondsToSelector:@selector(changeLabelText:)]){
[self.delegate changeLabelText:textField.text];
}
// 将模态视图关闭
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"dismiss")];
}];
}

利用通知

1
2
3
// 在通知中心注册一条名为ChangeLabelTextNotification的通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLabelText:) name:@"ChangeLabelTextNotification" object:nil];
// deallco时需要删除
1
2
3
4
5
6
-(void)changeLabelText:(NSNotification *)notification
{
id text = notification.object;
UILabel *label = (UILabel *)[self.view viewWithTag:102];
label.text = text;
}
1
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeLabelTextNotification" object:_textField.text]