1.删除storyboard,建立初始界面
删除storyboard,点击项目第一行,将里面的main interface选项中的main改成空白。
在Appdelegate中的application方法里建立初始界面
self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
self.window.rootViewController = MainViewController()
self.window.makeKeyAndVisible()
2.建立四个分界面的类和一个MainViewController类作为主界面的类
1.Map
class Map:UIViewController{
override func viewDidLoad(){
super.viewDidLoad()
self.BackgroundColor = UIColor.redColor()
}
}
2.Report
class Report:UIViewController{
override func viewDidLoad(){
super.viewDidLoad()
self.BackgroundColor = UIColor.blueColor()
}
}
3.Chat
class Chat:UIViewController{
override func viewDidLoad(){
super.viewDidLoad()
self.BackgroundColor = UIColor.yellowColor()
}
}
4.Personalinfo
class Personalinfo:UIViewController{
override func viewDidLoad(){
super.viewDidLoad()
self.BackgroundColor = UIColor.cyanColor()
}
}
5.MainViewController
class MainViewController:UITabBarViewController{
override func viewDidLoad(){
super.viewDidLoad()
let mapCtrl = Map()
mapCtrl.tabBarItem.title = "区域警情"
let chatCtrl = Chat()
chatCtrl.tabBarItem.title = "聊天室"
let repCtrl = Report()
repCtrl.tabBarItem.title = "警情上报"
let perCtrl = Personalinfo()
perCtrl.tabBarItem.title = "个人信息"
self.ViewControllers = [mapCtrl,chatCtrl,repCtrl,perCtrl]
}
}
3.在主界面显示之后加入引导界面、广告界面及登录界面
1.在主界面显示之后的时机内加入条件语句,判断后进入各不同界面
class MainViewController:UITabBarViewController{
override func viewDidLoad(){
super.viewDidLoad()
let m
apCtrl = Map()
mapCtrl.tabBarItem.title = "区域警情"
let chatCtrl = Chat()
chatCtrl.tabBarItem.title = "聊天室"
let repCtrl = Report()
repCtrl.tabBarItem.title = "警情上报"
let perCtrl = Personalinfo()
perCtrl.tabBarItem.title = "个人信息"
self.ViewControllers = [mapCtrl,chatCtrl,repCtrl,perCtrl]
}
override func viewDidAppear(animated:Bool){
super.viewDidApear(animated)
if prefs.hasAdv= false
let introCtrl = IntroduceViewController()
introCtrl.dismiss = {
if !Profile.isLogin(){
let viewCtrl = LoginViewController()
self.presentViewController(viewCtrl,animated:false,completion:nil)
}
}
self.presentViewController(introCtrl,animated:false,completion:nil)
}
else{
if prefs.hasAdv{
let advCtrl = AdvViewController()
advCtrl.dismiss = {
if !Profile.isLogin(){
let viewCtrl = LoginViewController()
self.presentViewController(viewCtrl,animated:false,completion:nil)
}
}
self.presentViewController(advCtrl,animated:false,completion:nil)
}
}
}
}
2.建立由主界面进入的各不同界面及条件
1.建立一个prefs结构体用于判断是否app是第一次打开,结构体中定义一个静态方法定义初始界面,定义一个静态成员变量用于判断是否进入广告界面
struct Prefs {
static func isFirstBoot() -> Bool {
let us = NSUserDefaults.standardUserDefaults()
if !us.boolForKey("firstBoot") {
us.setBool(true, forKey: "firstBoot")
return true
}
else {
return false
}
}
static func rootViewController() -> UIViewController {
return MainTabBarViewController()
}
static var hasAdv = true
}
2.定义一个结构体用于判断是否登录
struct Profile {
static func isLogin() -> Bool {
return false
}
}
3.定义一个闭包用来捕捉时机
typealias CompletionType = () -> Void
4.引导界面
class IntroduceViewController: UIViewController {
var dismiss: CompletionType!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.redColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.dismissViewControllerAnimated(true, completion: nil)
if let _ = dismiss {
dismiss()
}
}
}
5.广告界面
class AdvViewController: UIViewController {
var dismiss: CompletionType!
var jumpBtn: UIButton!
var timer: NSTimer!
var count = 10
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.greenColor()
jumpBtn = UIButton(type: .Custom)
jumpBtn.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
jumpBtn.center = CGPoint(x: self.view.bounds.size.width / 2, y: self.view.bounds.height / 2)
jumpBtn.setTitle("跳过(10s)", forState: .Normal)
jumpBtn.addTarget(self, action: #selector(didClick), forControlEvents: .TouchUpInside)
self.view.addSubview(jumpBtn)
}
func didClick() {
Prefs.hasAdv = false
timer.invalidate()
self.dismissViewControllerAnimated(false, completion: dismiss)
// if let _ = dismiss {
// dismiss()
// }
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(didTime(_:)), userInfo: nil, repeats: true)
}
func didTime(sender: NSTimer) {
count -= 1
jumpBtn.setTitle("跳过(\(count)s)", forState: .Normal)
if count == 0 {
didClick()
}
}
}