2016-07-23 206 views
1

我有一個MainViewController.swift,它連接到一個Xib文件(稱爲MainViewController)。 MainViewController連接到一個MenuViewController.swift及其Xib(稱爲MenuViewController)。該MainViewController使用下面的代碼調用MenuViewContoller,Swift - 如何從故事板中的視圖控制器加載xib文件?

override func viewDidLoad() { 
    super.viewDidLoad() 
    ... 
    menuBtn.addTarget(self, action: #selector(callMenuModal), forControlEvents: .TouchUpInside) 
    .... 

} 
func callMenuModal() { 
    let menuVC = MenuViewController() 
    menuVC.delegate = self 
    menuVC.modalPresentationStyle = .OverCurrentContext 
    presentViewController(menuVC, animated: false, completion: nil) 
} 
....... 
func backFromMenu() { 
    self.dismissViewControllerAnimated(true, completion: nil) 
    } 
... 
... 

我的朋友們做了一個故事板有很多塞格斯和ViewControllers的。

當從故事板上的ViewController按下按鈕時,我必須現在加載我的.xib文件及其相應的視圖控制器。我看到一個解決方案的高和低,並找不到一個。

以前我直接從AppDelegate調用我的xib,所以我不必做任何事情。我曾在故事板上只有一個視圖控制器,我在委託中使用的代碼下面給出了整個應用程序的應用程序委託,

import UIKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    window = UIWindow(frame: UIScreen.mainScreen().bounds) 

    let mainViewController = MainViewController(nibName: "MainViewController", bundle: nil) 

    window?.rootViewController = mainViewController 
    window?.makeKeyAndVisible() 
    return true   
} 
........rest of app delegate.... 

但我不知道如何加載廈門國際銀行文件,從故事板中的不同視圖控制器或從ViewController中按下按鈕(IBAction)。

+0

如果你不能做這個簡單的任務,那麼你需要閱讀和理解更多[Apple提供的基本教程。](https://developer.apple.com/library/prerelease/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/) – Jeff

回答

1

解決它...這是正確的在我的眼前,你猜我不得不問的問題寫我自己的答案,哈哈,我很聰明,我把這個代碼添加到故事板的視圖控制器,它就像一個魅力。

import UIKit 

class ViewController: UIViewController, communicationControllerM { 

    @IBOutlet weak var Btn: UIButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     Btn.addTarget(self, action: #selector(callMenuModal), forControlEvents: .TouchUpInside) 
    } 

    func callMenuModal() { 
     let mainVC = MainViewController() 
     mainVC.delegate = self 
     mainVC.modalPresentationStyle = .OverCurrentContext 
     presentViewController(mainVC, animated: false, completion: nil) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func backFromM() { 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 
} 

在我的廈門國際銀行視圖控制器我加了一個回車操作

playBtn.addTarget(self, action: #selector(goBackToMainVC), forControlEvents: .TouchUpInside) 
self.delegate?.backFromM() 

謝謝你們!

+1

關於像'Btn'和'backFromM'這樣的符號名稱,我建議您閱讀[Swift API Design關於縮寫的指導原則](https://swift.org/documentation/api-design-guidelines/#general-conventions)。 – Jeff

0

您加載一個視圖控制器及其相關的筆尖文件正是你已經在做它在你的代碼的方式:

let mainViewController = MainViewController(nibName: "MainViewController", bundle: nil) 

實例化視圖控制器,當你現在或推到它,視圖將從筆尖加載並放入界面。

3

您無法直接從故事板繼續訪問您的MainViewController。但是你可以加載並顯示它,就像你的應用代理一樣。

你想如何顯示MainViewController?將它推到導航堆棧上,以模態方式呈現,還有其他東西?我假設你正在使用UINavigationController並且你想把它推到堆棧上。

在朋友的UIViewController類,添加一個方法來加載和展示你的MainViewController

func presentMainViewController() { 
    let mainVC = MainViewController(nibName:"MainViewController", bundle:nil) 
    self.navigationController.pushViewController(mainVC, true); 
    // you could present it another way, such as: 
    // self.presentViewController(mainVC, true, nil) 
    // to present it as a modal 
} 
+0

非常感謝你;)它的工作原理。 –

0

作爲一類變量分配此:

var nibName: NibViewClass? 

其分配給爲筆尖由對應的類。

裏面,無論你要加載的筆尖視圖控制器,通話

nibName = NSBundle.mainBundle().loadNibNamed("NibView", owner: self, options: nil) as? NibViewClass 

確保「NibView」 ==筆尖UI文件的名稱。然後,您需要將其添加爲一個子視圖,像這樣:

if nibName != nil { 
    self.view.addsubview(nibName!) 

    //add this line to make it appear where you want it. 
    nibName.frame = CGRectMake(xPosition, yPositionOffScreen, nibWidth, nibHeight) 
} 

現在,這個會做大部分的工作適合你,但我會建議動畫它。有幾種不同的方式來做到這一點,但一個我經常用的就是像這樣:

UIView.animateWithDuration(0.7, delay: 1.0, options: .CurveEaseIn, animations: { 
     nibName.frame.origin.y = yPositionOnScreen 
    }, completion: { finished in 
     //code to run after nib finishes movement 
    }) 

在任何時候,你分配nibName後,它對應的類,你可以從你的viewController內轉讓其局部變量。如果您喜歡,也可以選擇使用x位​​置將筆尖製作爲動畫。我將使用相同的函數,並將y值顛倒過來,以便在完成後將其返回屏幕外。

相關問題