Skip to content

Commit

Permalink
dev:优化好登录注册代码,需要在跳转的时候传递数据
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyufeng1991 committed Nov 13, 2015
1 parent 2a84896 commit 20104cd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 51 deletions.
1 change: 0 additions & 1 deletion Oncenote/BmobOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@

@interface BmobOperation : NSObject

- (void)addUserToUserTable:(NSString*)tableName username:(NSString*)username password:(NSString*)password todo: (void(^)(BOOL isSuccessful, NSError *error)) todo;
- (void)addNoteToNoteTable:(NSString*)tableName userId:(NSString*)userId noteText:(NSString*)noteText;
@end
22 changes: 0 additions & 22 deletions Oncenote/BmobOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,8 @@
#import "BmobOperation.h"
#import <BmobSDK/Bmob.h>


@implementation BmobOperation

// - (void)showPromptDialog:(NSString*)title andMessage:(NSString*)message andButton:(NSString*)buttonTitle andAction:(void (^ __nullable)(UIAlertAction *action)) handler{
//
// //尝试使用新的弹出对话框;
// UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// [alertController addAction:[UIAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:handler]];
// //弹出提示框;
// [self presentViewController:alertController animated:true completion:nil];
// }


//(void (^)(id obj, NSUInteger idx, BOOL *stop))block


//往_User表增加一个用户;
- (void)addUserToUserTable:(NSString*)tableName username:(NSString*)username password:(NSString*)password todo: (void(^)(BOOL isSuccessful, NSError *error)) todo{
BmobObject *user = [BmobObject objectWithClassName:tableName];
[user setObject:username forKey:@"username"];
[user setObject:password forKey:@"password"];
[user saveInBackgroundWithResultBlock: todo];
}

//插入一条笔记到Note表,包括2个字段,userId,noteText;
- (void)addNoteToNoteTable:(NSString*)tableName userId:(NSString*)userId noteText:(NSString*)noteText{

Expand Down
20 changes: 5 additions & 15 deletions Oncenote/LoginViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@

#import "LoginViewController.h"
#import "MainViewController.h"
#import "BmobOperation.h"
#import <BmobSDK/Bmob.h>

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;



@end

@implementation LoginViewController
Expand All @@ -30,27 +27,20 @@ - (void)viewDidLoad {

#pragma mark - 所有的按钮点击;
- (IBAction)loginButtonPressed:(id)sender {




NSString *username = [self.usernameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

[BmobUser loginWithUsernameInBackground:username password:password block:^(BmobUser *user, NSError *error) {

if (error) {
//错误处理
NSLog(@"登录失败");
}else{
NSLog(@"登录成功,username:%@,password:%@,userId:%@",user.username,user.password,user.objectId);

UIViewController *mainViewController = [[UIViewController alloc] init];

mainViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
[self presentViewController:mainViewController animated:true completion:^{
//todo;
}];
UIViewController *mainViewController = [[UIViewController alloc] init];
mainViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
[self presentViewController:mainViewController animated:true completion:^{
//todo;
}];
}

}];
Expand Down
24 changes: 11 additions & 13 deletions Oncenote/RegisterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "RegisterViewController.h"
#import "BmobOperation.h"
#import <BmobSDK/Bmob.h>

@interface RegisterViewController ()

Expand All @@ -30,24 +30,22 @@ - (IBAction)createAccountButtonPressed:(id)sender {

if (![username isEqual: @""] && ![password isEqual: @""]) {
//用户名密码同时不为空,才可以进行注册;
BmobOperation *addUser = [[BmobOperation alloc] init];
[addUser addUserToUserTable:@"_User" username:username password:password todo:^(BOOL isSuccessful, NSError *error) {
//todo;
BmobUser *user = [[BmobUser alloc]init];
[user setUsername:username];
[user setPassword:password];
[user signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
if (isSuccessful) {
[self showPromptDialog:@"提示" andMessage:@"创建账户成功,请登录!" andButton:@"确定" andAction:^(UIAlertAction *action) {

NSLog(@"注册成功");
[self showPromptDialog:@"提示" andMessage:@"注册成功,请登录" andButton:@"确定" andAction:^(UIAlertAction *action) {
//
}];
} else {
NSLog(@"注册失败%@",error);
}else{
NSLog(@"注册失败");
}
}];

}else{
[self showPromptDialog:@"提示" andMessage:@"用户名密码不能为空!" andButton:@"确定" andAction:^(UIAlertAction *action) {

}];
NSLog(@"请输入信息");
}

}

#pragma mark - 触摸屏幕隐藏键盘
Expand Down

0 comments on commit 20104cd

Please sign in to comment.