下面是swift书写的时候的两个方法,其实这里不是教大家怎么样写的这个问题,我是想通过这两个不同语言的进行的一个比较,向大家找他们之间“想法”上的一些相同点,这样子我们学习swift的时候,就可以更加的游刃有余。我们熟悉OC这门语言,找到他么想法上的相同点了,你也就可以利用OC来学习swift了。
private func addChildViewControllers() { tabBar.tintColor = UIColor.orangeColor() zxaddChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home") zxaddChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center") zxaddChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover") zxaddChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile") } private func zxaddChildViewController(vc: UIViewController, title: String, imageName: String) { vc.title = title vc.tabBarItem.image = UIImage(named: imageName) addChildViewController(UINavigationController(rootViewController: vc)) }
下面是我们熟悉的OC的写法
HomeViewController * home =[[HomeViewController alloc]init]; home.title=@"首页"; home.tabBarItem.title=@"首页"; UINavigationController * na = [[UINavigationController alloc]initWithRootViewController:home]; HomeViewControllerone * home1 =[[HomeViewControllerone alloc]init]; home1.title=@"首页1"; home1.tabBarItem.title=@"首页2"; UINavigationController * na1 = [[UINavigationController alloc]initWithRootViewController:home1]; HomeViewControllertwo * home2 =[[HomeViewControllertwo alloc]init]; home2.title=@"首页3"; home2.tabBarItem.title=@"首页3"; UINavigationController * na2 = [[UINavigationController alloc]initWithRootViewController:home2]; HomeViewControllerT * home3 =[[HomeViewControllerT alloc]init]; home3.title=@"首页4"; home3.tabBarItem.title=@"首页4"; UINavigationController * na3 = [[UINavigationController alloc]initWithRootViewController:home3]; self.viewControllers=@[na,na1,na2,na3];
通过上面简单的对比,希望你能看到他们思想上的相同点,有些东西你悟出来的比我告诉你的更好!!!