本文基于OC語言翠肘,淺淺探索一下iOS幾種跳轉(zhuǎn)方式。先看一下本人做的思維導(dǎo)圖。
iOS分可視化開發(fā)(storyboard和xib)和代碼開發(fā)兩種方式嚎花。當(dāng)我們只使用某一種開發(fā)方式進(jìn)行開發(fā)的時候畜疾,界面跳轉(zhuǎn)可能不會遇到什么問題赴邻。但是當(dāng)我們混合開發(fā)的時候,界面間的跳轉(zhuǎn)有時候會遇到一些莫名其妙的問題啡捶。而實際開發(fā)項目中姥敛,經(jīng)常因?qū)嶋H需求混合各種開發(fā)方式。因此瞎暑,本人羅列了一下界面間跳轉(zhuǎn)可能遇到的九種情況彤敛。經(jīng)過測試与帆,實際上有三種跳轉(zhuǎn)的寫法。分別是:
1.xxx界面向storyboard界面跳轉(zhuǎn)
2.xxx界面向xib界面跳轉(zhuǎn)
3.xxx界面向代碼界面跳轉(zhuǎn)
現(xiàn)在墨榄,我們來一一測試一下玄糟。xxx界面向storyboard界面跳轉(zhuǎn)。先創(chuàng)建一個工程袄秩,如圖:
然后再新建一個storyboard
storyboard創(chuàng)建完是空白的
我們要拖一個UIViewcontroller進(jìn)storyboard內(nèi)
拖完之后的界面我們就比較熟悉了
再創(chuàng)建一個UIVIewController的類
選中故事板的類阵翎,把剛剛創(chuàng)建的類和storyboard的UIVIewcontroller關(guān)聯(lián)起來
再順便寫一下storyboard的ID,等下會用到
關(guān)聯(lián)完之后我們回到第一個UIViewController寫跳轉(zhuǎn)方法之剧,為了方便展示跳轉(zhuǎn)效果郭卫,我們寫個btn
//故事板跳故事板
UIButton*btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
[self.viewaddSubview:btn];
btn.backgroundColor= [UIColorredColor];
[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchDragInside];
btn.frame=CGRectMake(100,100,100,100);
接下來我們在btn里面寫跳轉(zhuǎn)方法
//故事板--跳到--》故事板
- (void)btnClick
{
UIStoryboard*myStoryboard = [UIStoryboardstoryboardWithName:@"Second"bundle:nil];
UIViewController*secondVC = [myStoryboard ?instantiateViewControllerWithIdentifier:@"secondViewController"];
[selfpresentViewController:secondVCanimated:YEScompletion:nil];
NSLog(@"跳轉(zhuǎn)");
}
這樣就完成了跳轉(zhuǎn),我們運行測試一下試試
這樣我們就實現(xiàn)了xxx界面向storyboard界面跳轉(zhuǎn)猪狈。下面我們來看看另一種跳轉(zhuǎn)方式 xxx界面向xib界面跳轉(zhuǎn)
首先箱沦,我們先創(chuàng)建一個xib界面
創(chuàng)建完,我們開始來準(zhǔn)備跳轉(zhuǎn)雇庙,跳轉(zhuǎn)之前給xib設(shè)置一個背景色谓形,方便區(qū)分
好了,回到最初的ViewController來實現(xiàn)跳轉(zhuǎn)到xib文件去疆前,首先寒跳,導(dǎo)入xib那個類的頭文件,還有再寫個按鈕來實現(xiàn)跳轉(zhuǎn)
#import"XIBViewController.h"
//故事板跳xib
UIButton*btnXIB = [UIButtonbuttonWithType:UIButtonTypeCustom];
[self.viewaddSubview:btnXIB];
btnXIB.backgroundColor= [UIColorblueColor];
[btnXIBaddTarget:selfaction:@selector(btnXIBClick)forControlEvents:UIControlEventTouchDragInside];
btnXIB.frame=CGRectMake(100,210,100,100);
來實現(xiàn)跳轉(zhuǎn)一下
//故事板--跳到--》xib
- (void)btnXIBClick
{
XIBViewController*xibVC=[[XIBViewControlleralloc]initWithNibName:@"XIBViewController"bundle:nil];
[selfpresentViewController:xibVCanimated:YEScompletion:nil];
}
我們來運行測試一下效果童太。
藍(lán)色按鈕是跳轉(zhuǎn)到xib界面的,我們點擊一下看看
點擊后實現(xiàn)跳轉(zhuǎn)胸完。我們這樣就實現(xiàn)了xxx界面向xib界面跳轉(zhuǎn)书释。最后我們再來看看最普遍的跳轉(zhuǎn)方式 xxx界面向代碼界面跳轉(zhuǎn)。
同樣的赊窥,先創(chuàng)建一個UIViewController爆惧,我們修改一下它的背景顏色為綠色,然后進(jìn)行跳轉(zhuǎn)
self.view.backgroundColor=[UIColorgreenColor];
回到UIViewController來寫跳轉(zhuǎn)方式
#import"CodeViewController.h"
//故事板跳code
UIButton*btnCode = [UIButtonbuttonWithType:UIButtonTypeCustom];
[self.viewaddSubview:btnCode];
btnCode.backgroundColor= [UIColorpurpleColor];
[btnCodeaddTarget:selfaction:@selector(btnCodeClick)forControlEvents:UIControlEventTouchDragInside];
btnCode.frame=CGRectMake(100,320,100,100);
//故事板--跳到--》code
- (void)btnCodeClick
{
CodeViewController*codeVC = [[CodeViewControlleralloc]init];
[selfpresentViewController:codeVCanimated:YEScompletion:nil];
}
運行測試一下
點擊跳轉(zhuǎn)按鈕
成功實現(xiàn)了xxx界面向代碼界面跳轉(zhuǎn)锨能。以上就是iOS界面的幾種不同開發(fā)界面之間的跳轉(zhuǎn)方式扯再。希望對看到此博客的人有所幫助。