界面跳轉(zhuǎn)及彈窗功能

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:^{
}];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末仰税,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子抽诉,更是在濱河造成了極大的恐慌陨簇,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件迹淌,死亡現(xiàn)場(chǎng)離奇詭異河绽,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)唉窃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)耙饰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人纹份,你說(shuō)我怎么就攤上這事苟跪。” “怎么了蔓涧?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵件已,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我元暴,道長(zhǎng)篷扩,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任茉盏,我火速辦了婚禮鉴未,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鸠姨。我一直安慰自己歼狼,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布享怀。 她就那樣靜靜地躺著,像睡著了一般趟咆。 火紅的嫁衣襯著肌膚如雪添瓷。 梳的紋絲不亂的頭發(fā)上梅屉,一...
    開(kāi)封第一講書(shū)人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音鳞贷,去河邊找鬼坯汤。 笑死,一個(gè)胖子當(dāng)著我的面吹牛搀愧,可吹牛的內(nèi)容都是我干的惰聂。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼咱筛,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼搓幌!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起迅箩,我...
    開(kāi)封第一講書(shū)人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤溉愁,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后饲趋,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體拐揭,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年奕塑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了堂污。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡龄砰,死狀恐怖盟猖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情寝贡,我是刑警寧澤扒披,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站圃泡,受9級(jí)特大地震影響碟案,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜颇蜡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一价说、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧风秤,春花似錦鳖目、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春狸捅,著一層夾襖步出監(jiān)牢的瞬間衷蜓,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工尘喝, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留磁浇,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓朽褪,卻偏偏與公主長(zhǎng)得像置吓,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子缔赠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容