- 01-导航实例-QQ空间.zip
62.4 KB
-
// MJLoginViewController.h
Map |
- //
- // MJLoginViewController.h
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- // 登录界面
- #import<UIKit/UIKit.h>
- @interfaceMJLoginViewController : UIViewController
- //登录
- - (IBAction)login;
- @property(weak,nonatomic)IBOutlet UITextField *qqField;
- @property(weak,nonatomic)IBOutlet UITextField *pwdField;
- @end
-
// MJLoginViewController.m
Map |
- //
- // MJLoginViewController.m
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJLoginViewController.h"
- #import"MJAboutViewController.h"
- #import"MJHomeViewController.h"
- @interfaceMJLoginViewController ()
- @end
- @implementationMJLoginViewController
- - (void)viewDidLoad
- {
- [superviewDidLoad];
-
- // 1.标题
- self.title =@"登录界面";
-
- // 2.右边的“关于”按钮
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@“关于" style:UIBarButtonItemStyleBordered target:selfaction:@selector(jump2about)];
-
- // 3.设置返回按钮的文字
- self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"style:UIBarButtonItemStyleBordered target:nilaction:nil];
- }
- #pragma mark跳到“关于”页面
- - (void)jump2about
- {
- // 1.创建
- MJAboutViewController *about = [[MJAboutViewController alloc] init];
- // about.title = @"关于------";
-
- // 2.压入栈
- [self.navigationController pushViewController:about animated:YES];
- }
- #pragma mark弹框
- - (void)alert:(NSString *)msg
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示"message:msg delegate:nilcancelButtonTitle:@"好的"otherButtonTitles:nil,nil];
- [alert show];
- }
- #pragma mark登录
- - (IBAction)login {
- // 1.判断有无QQ和密码
- if(_qqField.text.length ==0) {//没有QQ
- [self alert:_qqField.placeholder];
- return;
- }
-
- if(_pwdField.text.length ==0) {//没有密码
- [self alert:_pwdField.placeholder];
- return;
- }
-
- // 2.能执行到这里,说明同时输入了QQ和密码。应该跳到下一个界面---首页界面
- MJHomeViewController *home = [[MJHomeViewController alloc] init];
- [self.navigationController pushViewController:home animated:YES];
- }
- @end
-
-
// MJAboutViewController.h
|
- //
- // MJAboutViewController.h
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- // 关于界面
- #import<UIKit/UIKit.h>
- @interfaceMJAboutViewController : UIViewController
- @property(weak,nonatomic)IBOutletUIWebView *webView;
- @end
-
// MJAboutViewController.m
Map |
- //
- // MJAboutViewController.m
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJAboutViewController.h"
- @interface MJAboutViewController ()
- @end
- @implementation MJAboutViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- // 1.标题
- self.title =@"关于";
-
- // 2.加载网页
- // 2.1.获得about.html文件的URL
- NSURL *url = [[NSBundle mainBundle] URLForResource:@"about.html"withExtension:nil];
-
- // 2.2.封装一个请求对象
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
- // 2.3.加载请求
- [_webView loadRequest:request];
- }
- @end
-
// MJEditViewController.h
Map |
- //
- // MJEditViewController.h
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<UIKit/UIKit.h>
- //@class MJHomeViewController;
- @class MJEditViewController, MJPerson;
- @protocol MJEditViewControllerDelegate <NSObject>
- @optional
- //- (void)editViewController:(MJEditViewController *)edit didSaveWithName:(NSString *)name intro:(NSString *)intro;
- - (void)editViewController:(MJEditViewController *)edit didSaveWithPerson:(MJPerson *)person;
- @end
- @interface MJEditViewController : UIViewController
- @property(weak,nonatomic)IBOutlet UITextField *nameField;
- @property(weak,nonatomic)IBOutlet UITextView *introView;
- @property(nonatomic,weak)id<MJEditViewControllerDelegate> delegate;
- @property(nonatomic,strong) MJPerson *person;
- //@property (nonatomic, strong) MJHomeViewController *home;
- @end
-
// MJEditViewController.m
Map |
- //
- // MJEditViewController.m
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJEditViewController.h"
- #import"MJPerson.h"
- //#import "MJHomeViewController.h"
- @interfaceMJEditViewController ()
- @end
- @implementationMJEditViewController
- - (void)viewDidLoad
- {
- [superviewDidLoad];
-
- // 1,标题
- self.title =@"编辑个人信息";
-
- // 2.右上角的保存按钮
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存"style:UIBarButtonItemStyleDone target:selfaction:@selector(save)];
-
- // 3.取出模型数据,赋值到对应的子控件上面
- _nameField.text = _person.name;
- _introView.text = _person.intro;
- }
- //重写set方法在这里无效,因为内部的子控件还没有被创建
- //- (void)setPerson:(MJPerson *)person
- //
- //{
- // _person = person;
- //
- // _nameField.text = person.name;
- //
- // _introView.text = person.intro;
- //}
- #pragma mark保存
- - (void)save
- {
- // NSLog(@"%@ - %@", _nameField.text, _introView.text);
- // _home.nameLabel.text = _nameField.text;
- // _home.introView.text = _introView.text;
- //
- //
- // [self.navigationController popViewControllerAnimated:YES];
-
- if([_delegate respondsToSelector:@selector(editViewController:didSaveWithPerson:)]) {
- //封装模型
- MJPerson *p = [MJPerson personWithName:_nameField.text intro:_introView.text];
- //传递模型数据给代理
- [_delegate editViewController:selfdidSaveWithPerson:p];
-
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- @end
-
// MJHomeViewController.h
Map |
- //
- // MJHomeViewController.h
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<UIKit/UIKit.h>
- @interfaceMJHomeViewController : UIViewController
- @property(weak,nonatomic)IBOutletUILabel *nameLabel;
- @property(weak,nonatomic)IBOutletUITextView *introView;
- - (IBAction)sendStatus;
- @property(weak,nonatomic)IBOutletUITableView *tableVIew;
- @end
-
// MJHomeViewController.m
Map |
- //
- // MJHomeViewController.m
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJHomeViewController.h"
- #import"MJPerson.h"
- #import"MJStatus.h"
- #import"MJStatusViewController.h"
- #import"MJEditViewController.h"
- @interface MJHomeViewController () <UIActionSheetDelegate, MJEditViewControllerDelegate, MJStatusViewControllerDelegate, UITableViewDataSource>
- {
- NSMutableArray *_statuses;//所有的说说
- }
- @end
- @implementation MJHomeViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- // 1.标题
- self.title =@"个人主页";
-
- // 2.右上角的编辑按钮
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:selfaction:@selector(jump2edit)];
-
- // 3.设置左上角的注销按钮
- self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销"style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];
-
- _statuses = [NSMutableArray array];
- }
- #pragma mark注销
- - (void)logout
- {
- /*
- cancelButtonTitle:黑色
- destructiveButtonTitle :红色
- otherButtonTitles :白色
- */
- UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"是否要注销”delegate:self cancelButtonTitle:@"否"destructiveButtonTitle:@"是"otherButtonTitles:nil,nil];
-
- [sheet showInView:self.view];
- }
- #pragma mark - UIActionSheet的代理方法
- #pragma mark监听UIActionSheet的按钮点击
- - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if(buttonIndex ==1) return;
-
- //点击“是”
- //栈顶控制器出栈
- [self.navigationController popViewControllerAnimated:YES];
- }
- #pragma mark跳到编辑页面
- - (void)jump2edit
- {
- MJEditViewController *edit = [[MJEditViewController alloc] init];
-
- edit.delegate =self;
-
- edit.person = [MJPerson personWithName:_nameLabel.text intro:_introView.text];
- // edit.view.backgroundColor = [UIColor blueColor];
- // edit.nameField.text = _nameLabel.text;
- // edit.introView.text = _introView.text;
-
- [self.navigationController pushViewController:edit animated:YES];
- }
- #pragma mark - MJEditViewController代理方法
- - (void)editViewController:(MJEditViewController *)edit didSaveWithPerson:(MJPerson *)person
- {
- // 1.显示名称
- _nameLabel.text = person.name;
-
- // 2.显示简介
- _introView.text = person.intro;
- }
- - (IBAction)sendStatus {
- //跳到写说说界面
- MJStatusViewController *status = [[MJStatusViewController alloc] init];
- status.delegate =self;
- [self.navigationController pushViewController:status animated:YES];
- }
- #pragma mark - MJStatusViewController代理方法
- - (void)statusViewController:(MJStatusViewController *)statusController didSendStatus:(MJStatus *)status
- {
- // NSLog(@"首页拿到了---%@", status.text);
-
- // 1.将新的说说对象插入数组的最前面
- [_statuses insertObject:status atIndex:0];
-
- // 2.刷新表格
- [_tableVIew reloadData];
- }
- #pragma mark -数据源方法
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return_statuses.count;
- }
- #pragma mark每一行显示怎样的cell(内容)
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // 1.定义一个标识
- staticNSString *ID =@"cell";
-
- // 2.去缓存池中取出可循环利用的cell
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
-
- // 3.如果缓存中没有可循环利用的cell
- if(cell ==nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
- }
-
- //取出模型
- MJStatus *s = _statuses[indexPath.row];
-
- cell.textLabel.text = s.text;
-
- NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- fmt.dateFormat =@"yyyy-MM-dd HH:mm:ss";
- cell.detailTextLabel.text = [fmt stringFromDate:s.time];
-
- returncell;
- }
- @end
-
- //
- // MJPerson.h
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<Foundation/Foundation.h>
- @interfaceMJPerson : NSObject
- @property(nonatomic,copy) NSString *name;
- @property(nonatomic,copy) NSString *intro;
- + (id)personWithName:(NSString *)name intro:(NSString *)intro;
- @end
-
- //
- // MJPerson.m
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJPerson.h"
- @implementation MJPerson
- + (id)personWithName:(NSString *)name intro:(NSString *)intro
- {
- MJPerson *p = [[self alloc] init];
- p.name = name;
- p.intro = intro;
- return p;
- }
- @end
-
- //
- // MJStatus.h
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<Foundation/Foundation.h>
- @interfaceMJStatus : NSObject
- @property(nonatomic,copy) NSString *text;
- @property(nonatomic,strong) NSDate *time;
- + (id)statusWithText:(NSString *)text;
- @end
-
- //
- // MJStatus.m
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJStatus.h"
- @implementationMJStatus
- + (id)statusWithText:(NSString *)text
- {
- MJStatus *s = [[selfalloc] init];
- s.text = text;
- s.time = [NSDate date];
- returns;
- }
- @end
-
MJStatusViewController.h
Map |
- @classMJStatusViewController, MJStatus;
- @protocolMJStatusViewControllerDelegate <NSObject>
- @optional
- - (void)statusViewController:(MJStatusViewController *)statusController didSendStatus:(MJStatus *)status;
- @end
- @interfaceMJStatusViewController : UIViewController
- @property(nonatomic,weak)IBOutletUITextView *statusView;
- @property(nonatomic,weak)id<MJStatusViewControllerDelegate> delegate;
- @end
-
// MJStatusViewController.m
Map |
- //
- // MJStatusViewController.m
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJStatusViewController.h"
- #import"MJStatus.h"
- @interfaceMJStatusViewController ()
- @end
- @implementationMJStatusViewController
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- // 1.标题
- self.title =@"写说说";
-
- // 2.右上角的发布
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发布"style:UIBarButtonItemStyleDone target:selfaction:@selector(send)];
- }
- #pragma mark -发布
- - (void)send
- {
- if([_delegate respondsToSelector:@selector(statusViewController:didSendStatus:)]) {
- //给代理传递说说模型数据
- MJStatus *s = [MJStatus statusWithText:_statusView.text];
-
- [_delegate statusViewController:selfdidSendStatus:s];
-
- //出栈
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- @end
-
- //
- // MJAppDelegate.h
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<UIKit/UIKit.h>
- @interfaceMJAppDelegate :UIResponder<UIApplicationDelegate]] >
- @property(strong,nonatomic)UIWindow*window;
- @end
-
- //
- // MJAppDelegate.m
- // 01-导航实例-QQ空间
- //
- // Created by apple on 13-12-10.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJAppDelegate.h"
- #import"MJLoginViewController.h"
- @implementation MJAppDelegate
- - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
- {
- self.window= [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
-
-
- MJLoginViewController*login = [[MJLoginViewController alloc]init];
-
- //显示导航控制器
- self.window.rootViewController= [[UINavigationController alloc] initWithRootViewController:login];
-
- [self.window makeKeyAndVisible];
- return YES;
- }
- - (void)applicationWillResignActive:(UIApplication*)application
- {
- // 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.
- // 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.
- }
- - (void)applicationDidEnterBackground:(UIApplication*)application
- {
- // 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.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- }
- - (void)applicationWillEnterForeground:(UIApplication*)application
- {
- // 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.
- }
- - (void)applicationDidBecomeActive:(UIApplication*)application
- {
- // 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.
- }
- - (void)applicationWillTerminate:(UIApplication*)application
- {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- }
- @end
https://www.evernote.com/shard/s227/sh/a92a13e9-5997-487f-95e7-12d79fd88821/fb1289a310170e55d38903a59d39a941