下面总结一下我的第一个iOS项目中天气预报模块遇到的细节问题:
1、当多个不同的控制器要进入同一个控制器,且要保持该控制器的信息和数据是一致的,即是该控制类的同一个对象,该怎么办?
刚开始时,想了很久不知道该怎么办,其实用地个简单的单例方法,就能很容易地解决:
在.h文件声明该单例方法为类方法,在.m文件中声明一个静态的该类类型的变量(用于在单例方法中返回),再实现单例方法:
+ (WeatherMoreCityController *)instanceOfMoreCityController
{
if(moreCityController == nil)
{
moreCityController = [[WeatherMoreCityController alloc] init];
}
return moreCityController;
}
moreCityController就是该静态变量。
2、视图类的subviews属性(NSArray类型),中包含该视图的所有子视图, demo:
int size =[self.subviews count];
for(int i=size-1;i>=0;i--)
{
[self.subviews[i] removeFromSuperview];
}
3、故事板中根导航器导航到一个标签控制器,标签控制器中有两个标签,其控制器是两个导航控制器,两个导航控制器后面分别导航到两个实际控制界面的控制器,代码为:
WeatherPrimarViewController *primarViewController = [[WeatherPrimarViewController alloc] init];
WeatherMoreCityController *moreCityController = [WeatherMoreCityController instanceOfMoreCityController];
UINavigationController *navPrimarViewController = [[UINavigationController alloc] initWithRootViewController:primarViewController];
UINavigationController *navMoreCityController = [[UINavigationController alloc] initWithRootViewController:moreCityController];
UIImage* tabBarImage = [UIImage imageNamed:@"top_bg.png"];
[self.navigationController.navigationBar setBackgroundImage: tabBarImage forBarMetrics:UIBarMetricsDefault];
primarViewItem = [[UITabBarItem alloc]initWithTitle:@"" image:[UIImage imageNamed:@"weather_forecast_on1.png"] tag:0];
moreCityItem = [[UITabBarItem alloc]initWithTitle:@"" image:[UIImage imageNamed:@"weather_more_on1.png"] tag:1];
[primarViewItem setFinishedSelectedImage:[UIImage imageNamed:@"weather_forecast_on1.png"] withFinishedUnselectedImage:[UIImage imageNam ed:@"weather_forecast1.png"]];
[moreCityItem setFinishedSelectedImage:[UIImage imageNamed:@"weather_more_on1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"w eather_more1.png"]];
navPrimarViewController.tabBarItem = primarViewItem;
navMoreCityController.tabBarItem = moreCityItem;
NSArray *controllers = [NSArray arrayWithObjects:navPrimarViewController,navMoreCityController, nil];
tabBarController.viewControllers = controllers;