天天看點

IOS 菜鳥筆記 之 項目 一

任務目标:

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檔案

IOS 菜鳥筆記 之 項目 一
IOS 菜鳥筆記 之 項目 一

步驟三:輸入名稱,并建立XIB 檔案

IOS 菜鳥筆記 之 項目 一
IOS 菜鳥筆記 之 項目 一

步驟四:去掉預設的MainInterface

IOS 菜鳥筆記 之 項目 一
IOS 菜鳥筆記 之 項目 一

步驟五:修改預設生成的 RootViewController檔案 名稱按自己的喜好便可,修改其為繼承自   UINavigationController

@interface RootController : UINavigationController

@end
           
IOS 菜鳥筆記 之 項目 一

步驟六:編輯 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 目錄下了):

IOS 菜鳥筆記 之 項目 一
IOS 菜鳥筆記 之 項目 一

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實作方式

IOS 菜鳥筆記 之 項目 一
IOS 菜鳥筆記 之 項目 一
IOS 菜鳥筆記 之 項目 一
IOS 菜鳥筆記 之 項目 一

源碼位址:http://download.csdn.net/detail/x498980642/8640169

繼續閱讀