import "AppDelegate.h"
import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// 设置根视图控制器
ViewController *viewControiller = [[ViewController alloc]init];
[self.window setRootViewController:viewControiller];return YES;
}
import "ViewController.h"
import "RegisterViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];// 创建一个按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
// 设置按钮的frame
btn.frame = CGRectMake(100, 100, 100, 100);
// 设置按钮的标题
[btn setTitle:@"警示框" forState:UIControlStateNormal];
// 设置点击事件
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
// 显示按钮
[self.view addSubview:btn];// 新型弹出框按钮
UIButton *newBtn = [UIButton buttonWithType:UIButtonTypeSystem];
newBtn.frame = CGRectMake(100, 200, 150, 50);
[newBtn setTitle:@"新型弹出框按钮" forState:UIControlStateNormal];
[newBtn addTarget:self action:@selector(newBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:newBtn];// 弹出UIActionSheet的按钮
UIButton *sheetBrn = [UIButton buttonWithType:UIButtonTypeSystem];
sheetBrn.frame = CGRectMake(100, 300, 150, 40);
[sheetBrn setTitle:@"弹出sheet" forState:UIControlStateNormal];
[sheetBrn addTarget:self action:@selector(sheetBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sheetBrn];// 点击此按钮,可以跳转到注册界面
UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
registerBtn.frame = CGRectMake(100, 150, 50, 50);
[registerBtn setTitle:@"注册按钮" forState:UIControlStateNormal];
[registerBtn addTarget:self action:@selector(jumpRegisterVC:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:registerBtn];
}
-(void)jumpRegisterVC:(UIButton*)sender{
// 初始化注册视图控制器
RegisterViewController *registerVC = [[RegisterViewController alloc]init];
// 模态出注册的视图控制器
[self presentViewController:registerVC animated:YES completion:^{
NSLog(@"注册的视图控制器以显示");
}];
}
// 实现 sheet按钮的点击事件
-(void)sheetBtnAction:(UIButton*)sender{
// 初始化sheet
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"sheet" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其余", nil];
// 显示sheet
[sheet showInView:self.view];
}
// 实现 新型弹出框 按钮的点击事件
-(void)newBtnAction:(UIButton*)sender{
// 警示框控制器 这是iOS8之后出来的新类,用于替换原来的UIAlertView 和UIActionSheet
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"收红包" preferredStyle:UIAlertControllerStyleAlert];
// 为警示框添加点击按钮
// title: 警示框按钮标题
// style: 按钮的样式(枚举值)
// handler: 点击该按钮就会执行的语句(block)
// 取消按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"我点击了取消按钮");
}];
// 确定按钮
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"我点击了确定按钮");
}];
// 将创建好的cancelAction添加到警示框视图控制器上
[alertController addAction:cancelAction];
[alertController addAction:sureAction];
// 视图控制器之间切换的一种样式,称之为模态
// <将被废弃的>[self presentModalViewController:alertController animated:YES];
// 上面那种模态方式为iOS5.0之前的代码,下面为iOS5.0之后的模态新方法
// controller:将要模态出来的视图控制器
// animated:模态推出视图控制器的过程时候需要动画
// completion“模态推出视图控制器之后将要执行的操作(block)
[self presentViewController:alertController animated:YES completion:^{
NSLog(@"我是警示框视图控制器,我已经显示");
}];
}
// 实现按钮的点击事件
-(void)btnAction:(UIButton*)sender{
// 警示框
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"标题" message:@"内容" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",@"确定2", nil];
// 显示alertView
[alertView show];
}
pragma mark ---alertView 的代理方法
//跟踪点击的是alertView的哪个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"buttonIndex -- %ld",buttonIndex);
}
-
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];// 当程序内存吃紧到的时候,就会执行该方法,其实系统会帮我们释放不再显示的界面,底下的代码是我们模拟一下的
// 判断当前控制器的视图已经加载,并且没有显示,这个时候,我们就可以释放它
if ([self isViewLoaded] && self.view.window == nil) {
// 释放当前视图控制器的视图
self.view = nil;
}
}
@end
import "RegisterViewController.h"
@interface RegisterViewController ()
@end
@implementation RegisterViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// 添加一个lable
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
// label上显示的内容
[label setText:@"这里是注册界面"];
// 显示label
[self.view addSubview:label];// 返回到根视图控制器的按钮
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[backBtn setFrame: CGRectMake(200, 180, 60, 40)];
[backBtn setTitle:@"返回按钮" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
-(void)backAction:(UIButton*)sender{
// 只有当前的视图控制器是模态出来的时候,才可以使用此方法取消或者返回上一级的视图控制器
// 它和模态是一一对应的
// 消失的时候,是将当前视图从父视图上移除
[self dismissViewControllerAnimated:YES completion:^{
}];
}