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];
// 設(shè)置根視圖控制器
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];// 創(chuàng)建一個(gè)按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
// 設(shè)置按鈕的frame
btn.frame = CGRectMake(100, 100, 100, 100);
// 設(shè)置按鈕的標(biāo)題
[btn setTitle:@"警示框" forState:UIControlStateNormal];
// 設(shè)置點(diǎn)擊事件
[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];// 點(diǎn)擊此按鈕姐扮,可以跳轉(zhuǎn)到注冊(cè)界面
UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
registerBtn.frame = CGRectMake(100, 150, 50, 50);
[registerBtn setTitle:@"注冊(cè)按鈕" forState:UIControlStateNormal];
[registerBtn addTarget:self action:@selector(jumpRegisterVC:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:registerBtn];
}
-(void)jumpRegisterVC:(UIButton*)sender{
// 初始化注冊(cè)視圖控制器
RegisterViewController *registerVC = [[RegisterViewController alloc]init];
// 模態(tài)出注冊(cè)的視圖控制器
[self presentViewController:registerVC animated:YES completion:^{
NSLog(@"注冊(cè)的視圖控制器以顯示");
}];
}
// 實(shí)現(xiàn) sheet按鈕的點(diǎn)擊事件
-(void)sheetBtnAction:(UIButton*)sender{
// 初始化sheet
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"sheet" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles:@"其余", nil];
// 顯示sheet
[sheet showInView:self.view];
}
// 實(shí)現(xiàn) 新型彈出框 按鈕的點(diǎn)擊事件
-(void)newBtnAction:(UIButton*)sender{
// 警示框控制器 這是iOS8之后出來(lái)的新類(lèi)逛薇,用于替換原來(lái)的UIAlertView 和UIActionSheet
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標(biāo)題" message:@"收紅包" preferredStyle:UIAlertControllerStyleAlert];
// 為警示框添加點(diǎn)擊按鈕
// title: 警示框按鈕標(biāo)題
// style: 按鈕的樣式(枚舉值)
// handler: 點(diǎn)擊該按鈕就會(huì)執(zhí)行的語(yǔ)句(block)
// 取消按鈕
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"我點(diǎn)擊了取消按鈕");
}];
// 確定按鈕
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"我點(diǎn)擊了確定按鈕");
}];
// 將創(chuàng)建好的cancelAction添加到警示框視圖控制器上
[alertController addAction:cancelAction];
[alertController addAction:sureAction];
// 視圖控制器之間切換的一種樣式裕偿,稱(chēng)之為模態(tài)
// <將被廢棄的>[self presentModalViewController:alertController animated:YES];
// 上面那種模態(tài)方式為iOS5.0之前的代碼斋否,下面為iOS5.0之后的模態(tài)新方法
// controller:將要模態(tài)出來(lái)的視圖控制器
// animated:模態(tài)推出視圖控制器的過(guò)程時(shí)候需要?jiǎng)赢?huà)
// completion“模態(tài)推出視圖控制器之后將要執(zhí)行的操作(block)
[self presentViewController:alertController animated:YES completion:^{
NSLog(@"我是警示框視圖控制器叙赚,我已經(jīng)顯示");
}];
}
// 實(shí)現(xiàn)按鈕的點(diǎn)擊事件
-(void)btnAction:(UIButton*)sender{
// 警示框
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"標(biāo)題" message:@"內(nèi)容" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定",@"確定2", nil];
// 顯示alertView
[alertView show];
}
pragma mark ---alertView 的代理方法
//跟蹤點(diǎn)擊的是alertView的哪個(gè)按鈕
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"buttonIndex -- %ld",buttonIndex);
}
-
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];// 當(dāng)程序內(nèi)存吃緊到的時(shí)候业踢,就會(huì)執(zhí)行該方法届慈,其實(shí)系統(tǒng)會(huì)幫我們釋放不再顯示的界面截碴,底下的代碼是我們模擬一下的
// 判斷當(dāng)前控制器的視圖已經(jīng)加載,并且沒(méi)有顯示荞下,這個(gè)時(shí)候伶选,我們就可以釋放它
if ([self isViewLoaded] && self.view.window == nil) {
// 釋放當(dāng)前視圖控制器的視圖
self.view = nil;
}
}
@end
import "RegisterViewController.h"
@interface RegisterViewController ()
@end
@implementation RegisterViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// 添加一個(gè)lable
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
// label上顯示的內(nèi)容
[label setText:@"這里是注冊(cè)界面"];
// 顯示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{
// 只有當(dāng)前的視圖控制器是模態(tài)出來(lái)的時(shí)候,才可以使用此方法取消或者返回上一級(jí)的視圖控制器
// 它和模態(tài)是一一對(duì)應(yīng)的
// 消失的時(shí)候尖昏,是將當(dāng)前視圖從父視圖上移除
[self dismissViewControllerAnimated:YES completion:^{
}];
}