ios swift 多个界面,viewcontroller应该怎么写呢?或者说怎么控制?不用storyboard
你是说不同界面的跳转吗?
跳转可以这样写
let viewCro = ViewController.init()
// 假如用xib的话,用下面这句初始化
// let viewCro = ViewController.init(nibName:nibName, bundle:bundle)
if (self.navigationController != nil) {
self.navigationController!.pushViewController(viewCro, animated: true)
} else {
self.presentViewController(viewCro, animated: true, completion: nil)
}
不是跳转,是多个界面,不用storyboard怎么生成?
虽然相隔那么长的时间了,自己也在学习,在说也有很多新手在学习,希望这个代码能帮助学习
在AppDelegate文件下面的 application 方法加上这个代码
//创建window
var window: UIWindow?
window = UIWindow(frame: UIScreen.mainScreen().bounds);
window?.backgroundColor = UIColor.whiteColor();
let mainTabController = MainViewController()
window?.rootViewController = mainTabController;
//显示窗口
window?.makeKeyAndVisible();
return true
---------------------------
新建一个文件名为MainViewController .swift 文件
import UIKit
class MainViewController: UITabBarController {
override func loadView() {
super.loadView();
addChildViewControllers();
}
func addChildViewControllers() {
addChildNavAndTabBar("CartTableViewController",title: "购物车",imageName: "tabbar_cart");
addChildNavAndTabBar("MemberController",title: "我",imageName: "tabbar_fans");
}
/**
初始化子控制器
:param: childControllerName 需要初始化的子控制器
:param: title 初始化的标题
:param: imageName 初始化的图片
*/
func addChildNavAndTabBar(childControllerName: String,title:String,imageName:String) {
//动态获取命名空间
let namespace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String;
let cls:AnyClass = NSClassFromString(namespace + "." + childControllerName)!;
// 告诉编译器真实类型是UIViewController
let vcCls = cls as! UIViewController.Type;
let vc = vcCls.init();
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor(red: 255/255, green: 105/200, blue: 0.0/255, alpha: 1.0)]
//创建底部导航
vc.title = title;
vc.tabBarItem.setTitleTextAttributes(titleDict as? [String : AnyObject], forState: UIControlState.Highlighted)
vc.tabBarItem.image = UIImage(named: imageName);
vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted");
// 创建导航控制器
let nav = UINavigationController()
nav.addChildViewController(vc)
addChildViewController(nav)
}
}