新浪微博 有道云笔记 麦库 EverNote Pocket Instapaper 更多

01-导航实例-QQ空间Demo示例程序源代码

  1. 01-导航实例-QQ空间.zip
    62.4 KB
  2. // MJLoginViewController.h

    Map
  3. //
  4. //  MJLoginViewController.h
  5. //  01-导航实例-QQ空间
  6. //
  7. //  Created by apple on 13-12-10.
  8. //  Copyright (c) 2013itcast. All rights reserved.
  9. // 登录界面

  10. #import<UIKit/UIKit.h>

  11. @interfaceMJLoginViewController : UIViewController
  12. //登录
  13. - (IBAction)login;
  14. @property(weak,nonatomic)IBOutlet UITextField *qqField;
  15. @property(weak,nonatomic)IBOutlet UITextField *pwdField;

  16. @end
  17. // MJLoginViewController.m

    Map
  18. //
  19. //  MJLoginViewController.m
  20. //  01-导航实例-QQ空间
  21. //
  22. //  Created by apple on 13-12-10.
  23. //  Copyright (c) 2013itcast. All rights reserved.
  24. //

  25. #import"MJLoginViewController.h"
  26. #import"MJAboutViewController.h"
  27. #import"MJHomeViewController.h"

  28. @interfaceMJLoginViewController ()

  29. @end

  30. @implementationMJLoginViewController

  31. - (void)viewDidLoad
  32. {
  33.     [superviewDidLoad];
  34.    
  35.    // 1.标题
  36.    self.title =@"登录界面";
  37.    
  38.    // 2.右边的关于按钮
  39.    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@关于style:UIBarButtonItemStyleBordered target:selfaction:@selector(jump2about)];
  40.    
  41.    // 3.设置返回按钮的文字
  42.    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"style:UIBarButtonItemStyleBordered target:nilaction:nil];
  43. }

  44. #pragma mark跳到关于页面
  45. - (void)jump2about
  46. {
  47.    // 1.创建
  48.     MJAboutViewController *about = [[MJAboutViewController alloc] init];
  49. //    about.title = @"关于------";
  50.    
  51.    // 2.压入栈
  52.     [self.navigationController pushViewController:about animated:YES];
  53. }
  54. #pragma mark弹框
  55. - (void)alert:(NSString *)msg
  56. {
  57.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示"message:msg delegate:nilcancelButtonTitle:@"好的"otherButtonTitles:nil,nil];
  58.     [alert show];
  59. }

  60. #pragma mark登录
  61. - (IBAction)login {
  62.    // 1.判断有无QQ和密码
  63.    if(_qqField.text.length ==0) {//没有QQ
  64.         [self alert:_qqField.placeholder];
  65.        return;
  66.     }
  67.    
  68.    if(_pwdField.text.length ==0) {//没有密码
  69.         [self alert:_pwdField.placeholder];
  70.        return;
  71.     }
  72.    
  73.    // 2.能执行到这里,说明同时输入了QQ和密码。应该跳到下一个界面---首页界面
  74.     MJHomeViewController *home = [[MJHomeViewController alloc] init];
  75.     [self.navigationController pushViewController:home animated:YES];
  76. }
  77. @end
  78.  
  79. // MJAboutViewController.h

  80. //
  81. //  MJAboutViewController.h
  82. //  01-导航实例-QQ空间
  83. //
  84. //  Created by apple on 13-12-10.
  85. //  Copyright (c) 2013itcast. All rights reserved.
  86. // 关于界面

  87. #import<UIKit/UIKit.h>

  88. @interfaceMJAboutViewController : UIViewController
  89. @property(weak,nonatomic)IBOutletUIWebView *webView;

  90. @end
  91. // MJAboutViewController.m

    Map
  92. //
  93. //  MJAboutViewController.m
  94. //  01-导航实例-QQ空间
  95. //
  96. //  Created by apple on 13-12-10.
  97. //  Copyright (c) 2013itcast. All rights reserved.
  98. //

  99. #import"MJAboutViewController.h"

  100. @interface MJAboutViewController ()

  101. @end

  102. @implementation MJAboutViewController
  103. - (void)viewDidLoad
  104. {
  105.     [super  viewDidLoad];
  106.    
  107.    // 1.标题
  108.    self.title =@"关于";
  109.    
  110.    // 2.加载网页
  111.    // 2.1.获得about.html文件的URL
  112.     NSURL *url = [[NSBundle mainBundle] URLForResource:@"about.html"withExtension:nil];
  113.    
  114.    // 2.2.封装一个请求对象
  115.     NSURLRequest *request = [NSURLRequest requestWithURL:url];
  116.    
  117.    // 2.3.加载请求
  118.     [_webView loadRequest:request];
  119. }

  120. @end
  121. // MJEditViewController.h

    Map
  122. //
  123. //  MJEditViewController.h
  124. //  01-导航实例-QQ空间
  125. //
  126. //  Created by apple on 13-12-10.
  127. //  Copyright (c) 2013itcast. All rights reserved.
  128. //

  129. #import<UIKit/UIKit.h>
  130. //@class MJHomeViewController;
  131. @class MJEditViewController, MJPerson;


  132. @protocol MJEditViewControllerDelegate <NSObject>

  133. @optional
  134. //- (void)editViewController:(MJEditViewController *)edit didSaveWithName:(NSString *)name intro:(NSString *)intro;
  135. - (void)editViewController:(MJEditViewController *)edit didSaveWithPerson:(MJPerson *)person;
  136. @end

  137. @interface MJEditViewController : UIViewController
  138. @property(weak,nonatomic)IBOutlet UITextField *nameField;
  139. @property(weak,nonatomic)IBOutlet UITextView *introView;

  140. @property(nonatomic,weak)id<MJEditViewControllerDelegate> delegate;

  141. @property(nonatomic,strong) MJPerson *person;

  142. //@property (nonatomic, strong) MJHomeViewController *home;

  143. @end
  144. // MJEditViewController.m

    Map
  145. //
  146. //  MJEditViewController.m
  147. //  01-导航实例-QQ空间
  148. //
  149. //  Created by apple on 13-12-10.
  150. //  Copyright (c) 2013itcast. All rights reserved.
  151. //

  152. #import"MJEditViewController.h"
  153. #import"MJPerson.h"
  154. //#import "MJHomeViewController.h"

  155. @interfaceMJEditViewController ()

  156. @end

  157. @implementationMJEditViewController

  158. - (void)viewDidLoad
  159. {
  160.     [superviewDidLoad];
  161.    
  162.    // 1,标题
  163.    self.title =@"编辑个人信息";
  164.    
  165.    // 2.右上角的保存按钮
  166.    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存"style:UIBarButtonItemStyleDone target:selfaction:@selector(save)];
  167.    
  168.    // 3.取出模型数据,赋值到对应的子控件上面
  169.     _nameField.text = _person.name;
  170.     _introView.text = _person.intro;
  171. }

  172. //重写set方法在这里无效,因为内部的子控件还没有被创建
  173. //- (void)setPerson:(MJPerson *)person
  174. //
  175. //{
  176. //    _person = person;
  177. //   
  178. //    _nameField.text = person.name;
  179. //   
  180. //    _introView.text = person.intro;
  181. //}

  182. #pragma mark保存
  183. - (void)save
  184. {
  185. //    NSLog(@"%@ - %@", _nameField.text, _introView.text);
  186. //    _home.nameLabel.text = _nameField.text;
  187. //    _home.introView.text = _introView.text;
  188. //   
  189. //   
  190. //    [self.navigationController popViewControllerAnimated:YES];
  191.    
  192.    if([_delegate respondsToSelector:@selector(editViewController:didSaveWithPerson:)]) {
  193.        //封装模型
  194.         MJPerson *p = [MJPerson personWithName:_nameField.text intro:_introView.text];
  195.        //传递模型数据给代理
  196.         [_delegate editViewController:selfdidSaveWithPerson:p];
  197.        
  198.         [self.navigationController popViewControllerAnimated:YES];
  199.     }
  200. }

  201. @end
  202. // MJHomeViewController.h

    Map
  203. //
  204. //  MJHomeViewController.h
  205. //  01-导航实例-QQ空间
  206. //
  207. //  Created by apple on 13-12-10.
  208. //  Copyright (c) 2013itcast. All rights reserved.
  209. //

  210. #import<UIKit/UIKit.h>

  211. @interfaceMJHomeViewController : UIViewController
  212. @property(weak,nonatomic)IBOutletUILabel *nameLabel;
  213. @property(weak,nonatomic)IBOutletUITextView *introView;
  214. - (IBAction)sendStatus;
  215. @property(weak,nonatomic)IBOutletUITableView *tableVIew;

  216. @end
  217. // MJHomeViewController.m

    Map
  218. //
  219. //  MJHomeViewController.m
  220. //  01-导航实例-QQ空间
  221. //
  222. //  Created by apple on 13-12-10.
  223. //  Copyright (c) 2013itcast. All rights reserved.
  224. //

  225. #import"MJHomeViewController.h"
  226. #import"MJPerson.h"
  227. #import"MJStatus.h"
  228. #import"MJStatusViewController.h"
  229. #import"MJEditViewController.h"

  230. @interface MJHomeViewController () <UIActionSheetDelegate, MJEditViewControllerDelegate, MJStatusViewControllerDelegate, UITableViewDataSource>
  231. {
  232.     NSMutableArray *_statuses;//所有的说说
  233. }
  234. @end

  235. @implementation MJHomeViewController
  236. - (void)viewDidLoad
  237. {
  238.     [super viewDidLoad];
  239.    
  240.    // 1.标题
  241.    self.title =@"个人主页";
  242.    
  243.    // 2.右上角的编辑按钮
  244.    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:selfaction:@selector(jump2edit)];
  245.    
  246.    // 3.设置左上角的注销按钮
  247.    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销"style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];
  248.    
  249.     _statuses = [NSMutableArray array];
  250. }

  251. #pragma mark注销
  252. - (void)logout
  253. {
  254.    /*
  255.      cancelButtonTitle黑色
  256.      destructiveButtonTitle :红色
  257.      otherButtonTitles :白色
  258.      */
  259.     UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"是否要注销delegate:self cancelButtonTitle:@""destructiveButtonTitle:@""otherButtonTitles:nil,nil];
  260.    
  261.     [sheet showInView:self.view];
  262. }

  263. #pragma mark - UIActionSheet的代理方法
  264. #pragma mark监听UIActionSheet的按钮点击
  265. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  266. {
  267.    if(buttonIndex ==1return;
  268.    
  269.    //点击
  270.    //栈顶控制器出栈
  271.     [self.navigationController popViewControllerAnimated:YES];
  272. }

  273. #pragma mark跳到编辑页面
  274. - (void)jump2edit  
  275. {
  276.     MJEditViewController *edit = [[MJEditViewController alloc] init];
  277.    
  278.     edit.delegate =self;
  279.    
  280.     edit.person = [MJPerson personWithName:_nameLabel.text intro:_introView.text];

  281. //    edit.view.backgroundColor = [UIColor blueColor];
  282. //    edit.nameField.text = _nameLabel.text;
  283. //    edit.introView.text = _introView.text;
  284.    
  285.     [self.navigationController pushViewController:edit animated:YES];
  286. }

  287. #pragma mark - MJEditViewController代理方法
  288. - (void)editViewController:(MJEditViewController *)edit didSaveWithPerson:(MJPerson *)person
  289. {
  290.    // 1.显示名称
  291.     _nameLabel.text = person.name;
  292.    
  293.    // 2.显示简介
  294.     _introView.text = person.intro;
  295. }

  296. - (IBAction)sendStatus {
  297.    //跳到写说说界面
  298.     MJStatusViewController *status = [[MJStatusViewController alloc] init];
  299.     status.delegate =self;
  300.     [self.navigationController pushViewController:status animated:YES];
  301. }

  302. #pragma mark - MJStatusViewController代理方法
  303. - (void)statusViewController:(MJStatusViewController *)statusController didSendStatus:(MJStatus *)status
  304. {
  305. //    NSLog(@"首页拿到了---%@", status.text);
  306.    
  307.    // 1.将新的说说对象插入数组的最前面
  308.     [_statuses insertObject:status atIndex:0];
  309.    
  310.    // 2.刷新表格
  311.     [_tableVIew reloadData];
  312. }

  313. #pragma mark -数据源方法
  314. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  315. {
  316.    return_statuses.count;
  317. }

  318. #pragma mark每一行显示怎样的cell(内容)
  319. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  320. {
  321.    // 1.定义一个标识
  322.    staticNSString *ID =@"cell";
  323.    
  324.    // 2.去缓存池中取出可循环利用的cell
  325.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  326.    
  327.    // 3.如果缓存中没有可循环利用的cell
  328.    if(cell ==nil) {
  329.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
  330.     }
  331.    
  332.    //取出模型
  333.     MJStatus *s = _statuses[indexPath.row];
  334.    
  335.     cell.textLabel.text = s.text;
  336.    
  337.     NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  338.     fmt.dateFormat =@"yyyy-MM-dd HH:mm:ss";
  339.     cell.detailTextLabel.text = [fmt stringFromDate:s.time];
  340.    
  341.    returncell;
  342. }


  343. @end
  344. // MJPerson.h

    Map
  345. //
  346. //  MJPerson.h
  347. //  01-导航实例-QQ空间
  348. //
  349. //  Created by apple on 13-12-10.
  350. //  Copyright (c) 2013itcast. All rights reserved.
  351. //

  352. #import<Foundation/Foundation.h>

  353. @interfaceMJPerson : NSObject
  354. @property(nonatomic,copy) NSString *name;
  355. @property(nonatomic,copy) NSString *intro;

  356. + (id)personWithName:(NSString *)name intro:(NSString *)intro;
  357. @end
  358. // MJPerson.m

    Map
  359. //
  360. //  MJPerson.m
  361. //  01-导航实例-QQ空间
  362. //
  363. //  Created by apple on 13-12-10.
  364. //  Copyright (c) 2013itcast. All rights reserved.
  365. //

  366. #import"MJPerson.h"

  367. @implementation MJPerson
  368. + (id)personWithName:(NSString *)name intro:(NSString *)intro
  369. {
  370.     MJPerson *p = [[self alloc] init];
  371.     p.name = name;
  372.     p.intro = intro;
  373.    return p;
  374. }
  375. @end
  376. // MJStatus.h

    Map
  377. //
  378. //  MJStatus.h
  379. //  01-导航实例-QQ空间
  380. //
  381. //  Created by apple on 13-12-10.
  382. //  Copyright (c) 2013itcast. All rights reserved.
  383. //

  384. #import<Foundation/Foundation.h>

  385. @interfaceMJStatus : NSObject
  386. @property(nonatomic,copy) NSString *text;
  387. @property(nonatomic,strong) NSDate *time;

  388. + (id)statusWithText:(NSString *)text;
  389. @end
  390. // MJStatus.m

    Map
  391. //
  392. //  MJStatus.m
  393. //  01-导航实例-QQ空间
  394. //
  395. //  Created by apple on 13-12-10.
  396. //  Copyright (c) 2013itcast. All rights reserved.
  397. //

  398. #import"MJStatus.h"

  399. @implementationMJStatus
  400. + (id)statusWithText:(NSString *)text
  401. {
  402.     MJStatus *s = [[selfalloc] init];
  403.     s.text = text;
  404.     s.time = [NSDate date];
  405.    returns;
  406. }
  407. @end
  408. MJStatusViewController.h

    Map

  409. @classMJStatusViewController, MJStatus;

  410. @protocolMJStatusViewControllerDelegate <NSObject>
  411. @optional
  412. - (void)statusViewController:(MJStatusViewController *)statusController didSendStatus:(MJStatus *)status;

  413. @end


  414. @interfaceMJStatusViewController : UIViewController
  415. @property(nonatomic,weak)IBOutletUITextView *statusView;

  416. @property(nonatomic,weak)id<MJStatusViewControllerDelegate> delegate;

  417. @end
  418. // MJStatusViewController.m

    Map
  419. //
  420. //  MJStatusViewController.m
  421. //  01-导航实例-QQ空间
  422. //
  423. //  Created by apple on 13-12-10.
  424. //  Copyright (c) 2013itcast. All rights reserved.
  425. //

  426. #import"MJStatusViewController.h"
  427. #import"MJStatus.h"

  428. @interfaceMJStatusViewController ()

  429. @end

  430. @implementationMJStatusViewController

  431. - (void)viewDidLoad
  432. {
  433.     [superviewDidLoad];
  434.    // 1.标题
  435.     self.title =@"写说说";
  436.    
  437.    // 2.右上角的发布
  438.    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发布"style:UIBarButtonItemStyleDone target:selfaction:@selector(send)];
  439. }

  440. #pragma mark -发布
  441. - (void)send
  442. {
  443.    if([_delegate respondsToSelector:@selector(statusViewController:didSendStatus:)]) {
  444.        //给代理传递说说模型数据
  445.         MJStatus *s = [MJStatus statusWithText:_statusView.text];
  446.        
  447.         [_delegate statusViewController:selfdidSendStatus:s];
  448.        
  449.        //出栈
  450.         [self.navigationController popViewControllerAnimated:YES];
  451.     }
  452. }

  453. @end
  454. // MJAppDelegate.h

    Map
  455. //
  456. //  MJAppDelegate.h
  457. //  01-导航实例-QQ空间
  458. //
  459. //  Created by apple on 13-12-10.
  460. //  Copyright (c) 2013itcast. All rights reserved.
  461. //

  462. #import<UIKit/UIKit.h>

  463. @interfaceMJAppDelegate :UIResponder<UIApplicationDelegate]] > 

  464. @property(strong,nonatomic)UIWindow*window;

  465. @end
  466. // MJAppDelegate.m

    Map
  467. //
  468. //  MJAppDelegate.m
  469. //  01-导航实例-QQ空间
  470. //
  471. //  Created by apple on 13-12-10.
  472. //  Copyright (c) 2013itcast. All rights reserved.
  473. //

  474. #import"MJAppDelegate.h"
  475. #import"MJLoginViewController.h"

  476. @implementation MJAppDelegate

  477. - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  478. {
  479.    self.window= [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
  480.    
  481.    
  482.    MJLoginViewController*login = [[MJLoginViewController alloc]init];
  483.    
  484.    //显示导航控制器
  485.    self.window.rootViewController= [[UINavigationController allocinitWithRootViewController:login];
  486.    
  487.     [self.window makeKeyAndVisible];
  488.    return YES;
  489. }

  490. - (void)applicationWillResignActive:(UIApplication*)application
  491. {
  492.    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  493.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  494. }

  495. - (void)applicationDidEnterBackground:(UIApplication*)application
  496. {
  497.    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  498.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  499. }

  500. - (void)applicationWillEnterForeground:(UIApplication*)application
  501. {
  502.    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  503. }

  504. - (void)applicationDidBecomeActive:(UIApplication*)application
  505. {
  506.    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  507. }

  508. - (void)applicationWillTerminate:(UIApplication*)application
  509. {
  510.    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  511. }

  512. @end

 

 

https://www.evernote.com/shard/s227/sh/a92a13e9-5997-487f-95e7-12d79fd88821/fb1289a310170e55d38903a59d39a941

posted @ 2013-12-25 16:18  iTeaTime(技术清谈)  阅读(932)  评论(0编辑  收藏  举报