我是Swift的新手。我正在将数据传递到一个视图到另一个视图。 我正在使用下面的代码
@IBAction func loginAction(sender: AnyObject) {
let nextViewController = NextViewController(nibName: "NextViewController", bundle: nil)
self.navigationController?.pushViewController(nextViewController, animated: true)
}
但它不会导航第二页。
请帮帮我。
先谢谢。
答案 0 :(得分:0)
如果您没有使用StoryBoard,可以使用以下代码: 的 AppDelegate.swift 强>
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var userInputViewContrller: UserInputViewController = UserInputViewController()
var navigationViewController: UINavigationController = UINavigationController()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
self.navigationViewController = UINavigationController.init(rootViewController: userInputViewContrller)
self.window?.rootViewController = self.navigationViewController
self.window?.makeKeyAndVisible()
return true
}
现在你已经为你的naviagationViewController设置了rootViewController作为你的naviagationViewController,它是用页面变量初始化的" userInputViewContrller"
import UIKit
class UserInputViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
let viewFrame = CGRect(x: 1, y: 30, width: 373, height: 635)
let view: UIView = UIView(frame: viewFrame)
self.view.addSubview(view)
let clickMeButton:UIButton = UIButton()
let nextButtonFrame = CGRect(x: 200, y: 275, width: 150, height: 30)
nextButton = UIButton(frame: nextButtonFrame)
clickMeButton.setTitle("Click here", forState: UIControlState.Normal)
clickMeButton.addTarget(self, action: "clickMeTapped:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(clickMeButton)
}
func clickMeTapped(sender: UIButton){
let itemViewController = ItemViewController()
self.navigationController?.pushViewController(itemViewController, animated: true)
}
}
点击"点击此处"在UserInputViewController中,您将被导航到下一个屏幕itemViewController。
import UIKit
class ItemViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
//View
let viewFrame = CGRect(x: 1, y: 30, width: 373, height: 635)
let view: UIView = UIView(frame: viewFrame)
self.view.addSubview(view)
//Header Label
let headerLabelFrame = CGRect(x: 25, y: 50, width: 325, height: 30)
var headerLabel:UILabel = UILabel()
headerLabel = UILabel(frame: headerLabelFrame)
headerLabel.text = "Hello I'm in itemViewVireControllr"
view.addSubview(headerLabel)
}
}