标签 swift 下的文章

ios键盘的弹出与收回

1.弹出
这里以UITextField为例,其它控件应该也是一样的
选中控件,在右侧的属性面板(show the attributes inspector)找到Keyboard Type设置成自己想要的键盘形式
2.收回

//只有呼出键盘的对象才能收回键盘,所以这里调用了两次
        textFieldA.resignFirstResponder()
        textFieldB.resignFirstResponder()
//上面的例子只有两个设置了弹出键盘的控件,要是有很多的话可以用下面的代码,一句搞定
        self.view.endEditing(true)

ios应用启动流程

程序启动过程如下:
UIApplicationMain 函数调用创建一个 UIApplication 对象及程序代理对象(本例为 AppDelegate)
UIApplication 对象扫描 Info.plist 文件,将其中 Mainstoryboard file base name 所指定的 Storyboard 文件装入(通常为:MainStoryboard.storyboard)
UIApplication 对象从程序代理对象中获取窗口对象UIWindow(或创建一个UIWindow 新实例并将其与程序代理对象相关联)
将Storyboard 文件中 initial view controller 属性所指定的UIViewController 实例化,并将它赋予为 UIWindow 的root view controller
向程序代理对象发送 application:didFinishLaunchingWithOptions: 消息,以便程序员做自己的初始化工作

UIViewController的实例化过程中初始化了View和View下的其它控件对象,

swift中控件和代码绑定

1.事件绑定
如果是手写的话,代码要遵照这种格式

@IBAction func buttonClick(sender:AnyObject)
{

}

直接在控件上面右键,在右键菜单中把要绑定的时间拖动到编写的函数上面,如果没有编写代码,直接拖动到类当中,填一下方法名,xcode就会自动创建方法的代码
2.把控件绑定到方法的变量上
在控件上面右键,在右键菜单中找到New Reference Outlet拖动到类中,填好变量名即可,会生成如下代码

@IBOutlet weak var button: UIButton!

往项目里面添加文件,文件夹的时候Create groups和Create folder references的区别

Create groups:在创建文件夹之后,其实只是创建了一个虚拟目录,便于组织文件,在实际安装的时候,会把文件夹下面的文件都放到应用程序根目录
Create folder references:这个就是真实的创建了文件夹了
感觉Create groups是个很方便的东西啊,在新建项目的时候勾选这个话,之后在项目里面改变位置也不会有什么问题

使用Cocapods管理开源库

1.初始化pod,进入到项目的目录下执行

pod init

执行完成之后会出现Podfile文件
2.编辑Podfile文件

# Uncomment this line to define a global platform for your project
# platform :ios, '8.0'
# Uncomment this line if you're using Swift
# use_frameworks!

target 'MagicBox' do
platform:ios,'7.0'#所有库支持的ios最低版本
pod 'AFNetworking','~>2.3.1'#开源库版本号
pod 'SVProgressHUD','~>1.0'
pod 'Cordova','~>3.5.0'
pod 'GCJSONKit','~>1.5.0'
pod 'CordovaPlugin-console','~>0.2.10'
pod 'CordovaPlugin-device','~>0.2.11'
pod 'CordovaPlugin-network-information','~>0.2.3'
end

target 'MagicBoxTests' do

end

target 'MagicBoxUITests' do

end

3.安装开源库

pod install

执行成功之后会显示

Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (2.3.1)
Installing Cordova (3.5.0)
Installing CordovaPlugin-console (0.2.10)
Installing CordovaPlugin-device (0.2.11)
Installing CordovaPlugin-network-information (0.2.3)
Installing GCJSONKit (1.5.0)
Installing SVProgressHUD (1.1.3)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `DemoApp.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 7 dependencies from the Podfile and 7 total pods installed.

4.打开创建的DemoApp.xcworkspace,在桥接文件里面加入引入库文件的代码即可

#import "JSONKit.h"
#import "SVProgressHUD.h"
#import "CDV.h"
#import "AFNetworking.h"
#import "CDVLogger.h"
#import "CDVDevice.h"
#import "CDVConnection.h"