2016-07-23 111 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值颠倒过来,以便在完成后将其返回屏幕外。

相关问题