任务目标:
1.从AController 传数据到 BController;
2.由BController 分别使用Get 和 Post 调用接口请求网络数据;
3.得到所请求的数据后弹出对话框:
提示用户“是否将数据返回给AController”;
点击"确定"关闭对话框,退出BController 并将 接口请求的结果 返回给 AController;
点击"取消"关闭对话框
4.在AController 显示 BController 传回的数据;
任务知识点:
1.UIViewController 直接的基本跳转 2.IOS Get 和 Post 异步请求 3.UIViewController 数据传递 4.UIAlertDialog 的使用以及 UIAlertDialog 代理(delegate ,类似于 JAVA 里面的接口) 5.UILabel 自适应高度 6.UISegmentedControl 的基本使用
任务实现:
1.考虑到 StroyBoard 内容太多,如果团队合作开发容易导致 文件编辑冲突,所以优先考虑使用 xib 来做界面,替换方法为: 步骤一:删掉对应的Main.StroyBoard文件; 步骤二:新建Cocoa Touch文件
步骤三:输入名称,并创建XIB 文件
步骤四:去掉默认的MainInterface
步骤五:修改默认生成的 RootViewController文件 名称按自己的喜好便可,修改其为继承自 UINavigationController
@interface RootController : UINavigationController
@end
步骤六:编辑 AppDelegate.m 添加如下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
RootController *rootController = [[RootController alloc]init];
MainViewController *mainController = [[MainViewController alloc]init];
[rootController addChildViewController:mainController];
self.window.rootViewController = rootController;
[self.window makeKeyAndVisible];
return YES;
}
步骤七:同步骤二,步骤三,新建对应的 BController 的文件 完成后对应的项目结构如下(为了结构清晰,我将一些界面无关的文件移动到 Supporting Files 目录下了):
2.RootController 使用的是 UINavigationViewController 跳转可以使用如下方式:
BController *b =[[BController alloc] init] ;
[self.navigationController pushViewController:b animated:true];
3.由AController 传 数据给 BController 可以直接给 BController 赋值,BController 将数据传回给 AController 可以使用代理 ,因此我定义了如下代理: MainViewController.h
@protocol OnGetData
-(void)sendData:(NSString *) data ;
@end
并在 MainViewController实现了它。 MainViewController.h
@interface MainViewController : UIViewController<OnGetData>
@end
MainViewController.m
-(void)sendData:(NSString *) data {
NSLog(@"data:%@",data);
}
同时在BController中定义: BController.h
@property id<OnGetData> onGetData;
这样,如果将 onGetData 设置为 MainViewController ,由于 MainController 实现了 此代理 ,那么在 BController里面 调用 BController.m
[self.onGetData sendData:self.result];
那么在 MainController 中的对应实现也会 被调用,这样就实现了 B->Main 的数据的回调
4.网络请求的相应实现: BController.m
-(void)POST{
NSURL *url = [[NSURL alloc ]initWithString:@"http://shadowtest.sinaapp.com/adduser.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
NSString* dataStr = @"?name=text&password=111111";
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setHTTPMethod:@"POST"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
self.result= [NSString stringWithFormat:@"这是使用POST请求得到的结果:%@;",str];
[self showAlertView ];
}];
}
-(void)GET{
NSURL *url = [[NSURL alloc]initWithString:@"http://shadowtest.sinaapp.com/"];
NSLog(@"%@",url.host);
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
self.result = [NSString stringWithFormat:@"这是使用Get请求得到的结果:%@;",str];
[self showAlertView ];
}];
}
5.AlertDialog的相应实现:
-(void)showAlertView{
self.alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否将结果传回?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[self.alertView show];
}
6.UILable的自适应高度XIB实现方式
源码地址:http://download.csdn.net/detail/x498980642/8640169