iT邦幫忙

0

初學 IOS App 開發,換頁困難

GHH 2019-10-21 23:50:145709 瀏覽

初學 IOS,一個簡單的換頁功能一直搞不定

https://ithelp.ithome.com.tw/upload/images/20191021/20119398dGEPhEZUjw.png
App 一進來是右邊的 MainViewController(對到 Main.storyBorad 白色背景),

想要點了按鈕進到左邊 HomeViewController(對到 Home.storyBorad 藍色背景),

但是點了之後只出現半透明的遮罩(最左邊模擬器的畫面

附上兩個 ViewController 程式碼

import UIKit

class MainViewController: UIViewController {
    
    @IBOutlet weak var testBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loaaading the view.
    }
    
    @IBAction func itemClick(_ sender: UIButton) {
        
        print("itemClick")
        
        let home = HomeViewController.init()
        
        self.present(home, animated: true, completion: nil)
    }
}
import UIKit

class HomeViewController: UIViewController {
    
    @IBOutlet weak var homeLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        print("HomeViewController ViewDidload")

    }
    
}

我在 HomeViewController viewDidLoad 印 Log 是有東西的,代表有進去

再看 Home.storyBorad
https://ithelp.ithome.com.tw/upload/images/20191021/20119398LMflDegfty.png

右上角的 class 對到 HomeViewController
這樣應該也是有把 Home.storyBorad 和 HomeViewController 關聯起來
為什麼沒有畫面 求解

為什麼不是滿版這個問題……
我猜想你是跑在iOS 13吧。
這是iOS 13的新特性,卡片式頁面。
解決方法:將目標頁面modalPresentationStyle這個屬性改成fullScreen即可
不管是用程式碼或是在storyboard的viewController頁面設置都行。
storyboard的話,點擊ViewController的UI後,在show the Attributes inpector頁籤中找到presentation就可以選擇屬性。選Full Screen(記得是要改目標頁面的)
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
3
badgameshowtw
iT邦新手 4 級 ‧ 2019-10-22 10:08:58
最佳解答

這有兩種做法

  • 使用code present跳轉
  • storyBorad 直接控制跳轉

相信應該是希望有圖形化介面
所以新增兩個storyBorad
這邊簡單講解一下跳轉

1. present 跳轉

Xib

如果你要寫Code 下去跳轉
又希望ViewController有圖形化介面可以拉物件
可以再新增ViewController畫面
打勾新增Xib 這樣也是可以使用介面

https://ithelp.ithome.com.tw/upload/images/20191022/201122712L0RJkYKmZ.png

跳轉

寫法跟你的一樣就可以了
直接可以跳轉過去
圖形化介面在xib裡面操作就可以簡單顯示

@IBAction func clickBtn(_ sender: Any) {
    self.present(NewViewController(), animated: true, completion: nil)
}

https://ithelp.ithome.com.tw/upload/images/20191022/20112271MNDvzTCJFE.png

Demo

2. storyBorad 跳轉

如果你想storyBorad跳到storyBorad有點奇怪
storyBorad裡面本來就是可以融入很多ViewController
這邊也是簡易教學

拉ViewController物件到你的同一storyBorad

我怕你看不懂就是storyBorad有兩個ViewController

https://ithelp.ithome.com.tw/upload/images/20191022/20112271LkmC5VhWKU.png

https://ithelp.ithome.com.tw/upload/images/20191022/20112271l1jvzfpMNr.png

將ViewController class指定到你新增的ViewController

你拉好的ViewController是沒靈魂的
可以將它填入你想要的class名稱

https://ithelp.ithome.com.tw/upload/images/20191022/20112271Yz5aiZLxBv.png

storyBorad 使用 show 跳轉

將你原來的Button 按著鍵盤control拉一條到新的ViewController
然後使用show 就可跳轉了

https://ithelp.ithome.com.tw/upload/images/20191022/201122712JJD9M4cWy.png

Demo

最後

這兩種方式都可以跳轉
也可以使用自己新增的ViewController裡面的程式碼

Demo

懶得寫的話 我寫好了直接下載看吧
Github:
https://github.com/Bgihe/PresentTest

看更多先前的回應...收起先前的回應...
GHH iT邦新手 1 級 ‧ 2019-10-22 20:38:58 檢舉

感謝回答,可以問一下為什麼換過去的那一頁不是滿版的嗎

iOS 13 把present
變成這種有重疊式的ViewController
有點像是Safari手機版 書籤效果
如何關閉我還沒研究 哈哈

badgameshowtw
將目標頁面modalPresentationStyle這個屬性改成fullScreen即可

感謝 學到一招筆記筆記
modalPresentationStyle該不會是iOS 13後新增的參數
這樣我就要版控去調整了

badgameshowtw
這個並不是iOS 13後新增的屬性。
而是原本的automatic屬性的預設變成不是fullscreen了。

0
Lacie
iT邦新手 4 級 ‧ 2019-10-22 00:58:12

安安你好,我也是 iOS 新手,可能解決不了你的問題 XD(留給後面的高手)
但你想跨 Storyboard 換頁的話可以嘗試使用 Storyboard Reference:

Reference 選 Home 那個 Storyboard

設置 Seque 及 identifier

按下 Button 裡的程式碼

Demo

1
海綿寶寶
iT邦大神 1 級 ‧ 2019-10-22 08:05:36

現在是
MainViewController 在 Main.storyboard 裡
HomeViewController 在 Home.storyboard 裡
請先改成
MainViewController 和 HomeViewController 都在 Main.storyboard 裡
再看看結果如何

一般來說
一個 app 只需要一個 storyboard (頂多加一個 launch.storyboard)

一個app只需要一個storyboard會累死記憶體喔!
好啦!正確的做法是:
同樣系統功能面的東西在同一個storyboard。
因為這樣做的好處連viewController都可以確實的分類。
寫出來的東西才不會混亂。

GHH iT邦新手 1 級 ‧ 2019-10-22 20:32:17 檢舉

同樓上,因為 Android 都是一個畫面對到一個 class,所以對這種很多畫面都塞在同一個 storyboard 的做法很不習慣。

0

我在想是呼叫storyboard中的viewController出錯吧!
我這個code是在有navigation下的寫法。
如果是present你改成present的寫法就行了。

@IBAction func nextPage(_ sender: UIButton) {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let destination: DetailVC = sb.instantiateViewController(withIdentifier: "DetailVC") as! DetailVC 
        self.navigationController?.pushViewController(destination, animated: true)
    }

https://ithelp.ithome.com.tw/upload/images/20191022/20072133DhUZd1mqGR.png

記得內頁面要如這樣設置indentity的storyboard ID

我要發表回答

立即登入回答