首先,装了xcode,然后就开始熟悉xcode的开发环境,了解stroyboard,xib,和纯代码编写。
我老大先给我布置了一个登录注册页面,实现页面跳转和弹框提醒。
了解程序启动原理,view的显示和添加,UI基础控件等。
了解在Appdelegate.m文件和ViewController.m文件等文件里添加什么代码实现纯代码编写。
1/Appdelegate.m里关键代码:
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1.创建window
_window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];
_window.backgroundColor = [UIColorwhiteColor];
[_windowmakeKeyAndVisible];
// 2.创建根视图控制器
ViewController *view = [[ViewControlleralloc] init];
_window.rootViewController = view; // Override point for customization after application launch.
return YES;
}
2/ViewController.m里的关键代码:
#import "ViewController.h"
#import "RightViewController.h"
#import "NewViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)loadView{
[super loadView];
NSLog(@"Hello");
}
- (void)viewDidLoad {
[superviewDidLoad];
UILabel *label=[[UILabelalloc] initWithFrame:CGRectMake(100,100, 320, 40)];
label.text=@"用户名:";
[self.viewaddSubview:label];
UITextField *textfield=[[UITextFieldalloc] initWithFrame:CGRectMake(165,100, 150, 40)];
textfield.keyboardType=UIKeyboardTypeNamePhonePad;
textfield.borderStyle=UITextBorderStyleRoundedRect;
textfield.clearButtonMode=UITextFieldViewModeWhileEditing;
textfield.secureTextEntry=NO;
textfield.placeholder=@"请输入用户名";
textfield.textColor=[UIColorblackColor];
[self.viewaddSubview:textfield];
UILabel *label2=[[UILabelalloc] initWithFrame:CGRectMake(100,150, 320, 40)];
label2.text=@"密码:";
[self.viewaddSubview:label2];
UILabel *label3=[[UILabelalloc] initWithFrame:CGRectMake(180,195, 100, 40)];
label3.text=@"记住密码";
label3.textColor=[UIColorblueColor];
[self.viewaddSubview:label3];
UITextField *textfield2=[[UITextFieldalloc] initWithFrame:CGRectMake(165,150, 150, 40)];
textfield2.keyboardType=UIKeyboardTypeNamePhonePad;
textfield2.borderStyle=UITextBorderStyleRoundedRect;
textfield2.clearButtonMode=UITextFieldViewModeWhileEditing;
textfield2.placeholder=@"请输入密码";
textfield2.secureTextEntry=YES;
textfield2.textColor=[UIColorblackColor];
[self.viewaddSubview:textfield2];
UISwitch *rembSwitch = [[UISwitchalloc] initWithFrame:CGRectMake(260,200, 100, 40)];
rembSwitch.on=YES;
[rembSwitch addTarget:selfaction:@selector(onSwitch)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:rembSwitch];
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(100,240, 200, 40);
[button setTitle:@"登录"forState:UIControlStateNormal];
[button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
[self.viewaddSubview:button];
[button addTarget:selfaction:@selector(jumpSuccess)forControlEvents:UIControlEventTouchUpInside];
UIButton *button2=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button2.frame=CGRectMake(100,280, 200, 40);
[button2 setTitle:@"注册"forState:UIControlStateNormal];
[button2 setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
[self.viewaddSubview:button2];
[button2 addTarget:selfaction:@selector(jumpNew)forControlEvents:UIControlEventTouchUpInside];
}
- (void)onSwitch{
NSLog(@"1");
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)jumpSuccess{
RightViewController *rightvc=[[RightViewControlleralloc]init];
rightvc.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[selfpresentViewController:rightvc animated:YEScompletion:^{
}];
//弹窗提醒,去掉注释可用
/* UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登录成功" message:@"" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
// [alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
*/
}
-(void)jumpNew{
NewViewController *newvc=[[NewViewControlleralloc]init];
newvc.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[selfpresentViewController:newvc animated:YEScompletion:^{
}];
}
@end
3/RightViewController.m关键代码:#import "ViewController.h"
#import "RightViewController.h"
@interface RightViewController ()
@end
@implementation RightViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
UILabel *txtNSString=[[UILabelalloc] initWithFrame:CGRectMake(170,150, 320, 40)];
txtNSString.backgroundColor=[UIColorclearColor];
txtNSString.textColor=[UIColorredColor];
txtNSString.text=@"hello,";
[self.viewaddSubview:txtNSString];
UIButton *okButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
okButton.frame=CGRectMake(150,250, 100, 40);
[okButton setTitle:@"OK"forState:UIControlStateNormal];
[okButton addTarget:selfaction:@selector(close)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:okButton];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)close{
[selfdismissViewControllerAnimated:YEScompletion:^{
}];
}
@end
#import "ViewController.h"
#import "NewViewController.h"
@interface NewViewController ()
@end
@implementation NewViewController
- (void)viewDidLoad {
[superviewDidLoad];
UILabel *labelNew=[[UILabelalloc] initWithFrame:CGRectMake(100,100, 320, 40)];
labelNew.text=@"用户名:";
[self.viewaddSubview:labelNew];
UITextField *textfieldNew=[[UITextFieldalloc] initWithFrame:CGRectMake(165,100, 150, 40)];
textfieldNew.keyboardType=UIKeyboardTypeNamePhonePad;
textfieldNew.borderStyle=UITextBorderStyleRoundedRect;
textfieldNew.clearButtonMode=UITextFieldViewModeWhileEditing;
textfieldNew.placeholder=@"请输入用户名";
textfieldNew.secureTextEntry=NO;
textfieldNew.textColor=[UIColorblackColor];
[self.viewaddSubview:textfieldNew];
UILabel *label2New=[[UILabelalloc] initWithFrame:CGRectMake(100,150, 320, 40)];
label2New.text=@"密码:";
[self.viewaddSubview:label2New];
UITextField *textfield2New=[[UITextFieldalloc] initWithFrame:CGRectMake(165,150, 150, 40)];
textfield2New.keyboardType=UIKeyboardTypeNamePhonePad;
textfield2New.borderStyle=UITextBorderStyleRoundedRect;
textfield2New.clearButtonMode=UITextFieldViewModeWhileEditing;
textfield2New.placeholder=@"请输入密码";
textfield2New.secureTextEntry=YES;
textfield2New.textColor=[UIColorblackColor];
[self.viewaddSubview:textfield2New];
UIButton *saveButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
saveButton.frame=CGRectMake(150,250, 100, 40);
[saveButton setTitle:@"保存"forState:UIControlStateNormal];
[saveButton addTarget:selfaction:@selector(saveRegister)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:saveButton];
UIButton *ok2Button=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
ok2Button.frame=CGRectMake(150,300, 100, 40);
[ok2Button setTitle:@"返回"forState:UIControlStateNormal];
[ok2Button addTarget:selfaction:@selector(back)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:ok2Button];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)back{
[selfdismissViewControllerAnimated:YEScompletion:^{
}];
}
-(void)saveRegister{
UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"保存信息"message:@""preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];
UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[selfpresentViewController:alertController animated:YEScompletion:nil];
}
@end