从第一个视图控制器导航到第二个视图控制器时出现错误消息。我的编码就像这个
let vc = LoginViewController(nibName: "LoginViewController", bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
问题是我总是收到这种错误信息
2014-12-09 16:51:08.219 XXXXX[1351:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/FDC7AA0A-4F61-47E7-955B-EE559ECC06A2/XXXXX.app> (loaded)' with name 'LoginViewController''
*** First throw call stack:
(0x2efcaf0b 0x39761ce7 0x2efcae4d 0x31b693f9 0x31ac1eaf 0x3191e365 0x317fe895 0x318a930d 0x318a9223 0x318a8801 0x318a8529 0x318a8299 0x318a8231 0x317fa305 0x3147631b 0x31471b3f 0x314719d1 0x314713e5 0x314711f7 0x3146af1d 0x2ef96039 0x2ef939c7 0x2ef93d13 0x2eefe769 0x2eefe54b 0x33e6b6d3 0x3185d891 0x4ccc8 0x4cd04 0x39c5fab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
答案 0 :(得分:116)
我已经找到了答案
Swift 4
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
self.present(nextViewController, animated:true, completion:nil)
Swift 3
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
答案 1 :(得分:43)
试试这个。这里“LoginViewController”是storyboard中指定的storyboardID。
见下文
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as LoginViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
答案 2 :(得分:37)
XCODE 8.2 AND SWIFT 3.0
呈现存在的UIViewController
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.present(loginVC, animated: true, completion: nil)
推送存在的UIViewController
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(loginVC, animated: true)
请记住,您可以按照以下步骤放置UIViewController
标识符:
Main.storyboard
UIViewController
UIViewController
答案 3 :(得分:10)
见我的。
input_data = LOAD '$INPUT' USING PigStorage('|') AS (user_id:chararray ...)
static_data = LOAD ... AS (req_user_id:chararray
required_data = JOIN input_data BY useriD, static_data BY req_user_id USING 'replicated';
required_data_fmt = -- project required fields.
如果您使用呈现样式,则可能会丢失具有预设pushnavigation的页面导航栏。
答案 4 :(得分:10)
如果要导航到以编程方式创建的Controller,请执行以下操作:
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
如果您想使用标识符“newViewController”导航到StoryBoard上的Controller,请执行以下操作:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
答案 5 :(得分:9)
您可以使用以下代码以编程方式从一个场景移动到另一个场景:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let objSomeViewController = storyBoard.instantiateViewControllerWithIdentifier(“storyboardID”) as! SomeViewController
// If you want to push to new ViewController then use this
self.navigationController?.pushViewController(objSomeViewController, animated: true)
// ---- OR ----
// If you want to present the new ViewController then use this
self.presentViewController(objSomeViewController, animated:true, completion:nil)
这里storyBoardID是您使用Interface Builder设置为场景的值。 如下所示:
答案 6 :(得分:8)
<强> Swift3:强>
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController("LoginViewController) as UIViewController
self.navigationController?.pushViewController(vc, animated: true)
试一试。你只是把笔尖与故事板表示混淆了。
答案 7 :(得分:2)
let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "MyCustomViewController") as! ViewController
let navController = UINavigationController(rootViewController: VC1)
self.present(navController, animated:true, completion: nil)
答案 8 :(得分:2)
根据情况,程序上有不同的方式
在Storyboard笔尖文件中加载Storyenter代码
let yourVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
self.present(yourVc, animated: true, completion: nil)
从xib加载
let yourVc = YourViewController.init(nibName: "YourViewController", bundle: nil)
self.present(yourVc, animated: true, completion: nil)
通过Segue导航
self.performSegue(withIdentifier: "your UIView", sender: self)
答案 9 :(得分:1)
我给你我的代码进行转换。
在此示例中,操作连接到UIButton。所以不要忘记设置它。
不要忘记在transition
方法中设置ViewController的名称。
不要忘记设置故事板。您需要为每个viewController提供一个视图。将每个ViewController连接到storyBoard中的每个视图。您可以在下面的屏幕截图中看到
class PresentationViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var playButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
let image = UIImage(named: "YourPlayButton") as UIImage?
playButton.frame = CGRectMake(0, 0, 100, 100)
playButton.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/2)
playButton.addTarget(self, action: "transition:", forControlEvents: UIControlEvents.TouchUpInside)
playButton.setBackgroundImage(image, forState: UIControlState.Normal)
self.view.addSubview(playButton)
}
func transition(sender:UIButton!)
{
println("transition")
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("YourSecondViewController") as UIViewController
let window = UIApplication.sharedApplication().windows[0] as UIWindow
UIView.transitionFromView(
window.rootViewController!.view,
toView: secondViewController.view,
duration: 0.65,
options: .TransitionCrossDissolve,
completion: {
finished in window.rootViewController = secondViewController
})
}
}
答案 10 :(得分:1)
您可以在视图控制器的storyboard和storyboardId的帮助下使用代码在视图控制器之间进行导航。 此代码适用于Swift 3:
let signUpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
self.navigationController?.pushViewController(signUpVC, animated: true)
答案 11 :(得分:0)
如果您在没有拖放的情况下构建UI(不使用故事板) 并希望将默认页面或ViewController.swift导航到另一个页面? 跟着这些步骤 1)添加一个类(.swift) 2)导入UIKit 3)声明类名称,如
class DemoNavigationClass :UIViewController{
override func viewDidLoad(){
let lbl_Hello = UILabel(frame: CGRect(x:self.view.frame.width/3, y:self.view.frame.height/2, 200, 30));
lbl_Hello.text = "You are on Next Page"
lbl_Hello.textColor = UIColor.white
self.view.addSubview(lbl_Hello)
}
}
创建第二页后返回第一页(ViewController.swift) 这里在viewDidLoad方法中创建一个按钮
let button = UIButton()
button.frame = (frame: CGRect(x: self.view.frame.width/3, y: self.view.frame.height/1.5, width: 200, height: 50))
button.backgroundColor = UIColor.red
button.setTitle("Go to Next ", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
现在在同一个类
中的viewDidLoad()之外定义buttonAction方法func buttonAction(sender: UIButton!)
{
let obj : DemoNavigationClass = DemoNavigationClass();
self.navigationController?.pushViewController(obj, animated: true)
}
保留一件我忘记的事情 在main.storyboard中有一个带箭头的场景, 选择该箭头并按删除按钮
现在拖放一个navigationcontroller并删除navaigation控制器附带的tableview。 选择键盘上的navigationcontroller按下控件并将其拖到故事板上的另一个场景,即ViewController。 这意味着您的viewcontroller成为root viewcontroller 希望这能帮助你 谢谢 在main.storyboard中,拖放navigationcontroller,
答案 12 :(得分:0)
let vc = DetailUserViewController()
vc.userdetails = userViewModels[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
答案 13 :(得分:0)
我认为您正在寻找这样的东西:
let signUpViewController = SignUpViewController()
present(signUpViewController, animated: true, completion: nil)
如果要全屏显示新页面:
present(signUpViewController, animated: true, completion: nil)
希望这会有所帮助:)
答案 14 :(得分:0)
Swift 4.0.3
@IBAction func registerNewUserButtonTapped(_ sender: Any) {
print("---------------------------registerNewUserButtonTapped --------------------------- ");
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "RegisterNewUserViewController") as! RegisterNewUserViewController
self.present(nextViewController, animated:true, completion:nil)
}
更改我们的控制器名称RegisterNewUserViewController
答案 15 :(得分:0)
XCODE 9.2和SWIFT 3.0
没有Segue Connection ViewController
到NextViewcontroller
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
self.navigationController?.pushViewController(nextViewController, animated:true)
或
let VC:NextViewController = storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
self.navigationController?.pushViewController(VC, animated: true)
答案 16 :(得分:0)
除了上面的好答案,在您的应用程序的屏幕顶部设置导航视图控制器,您可以将其添加到块中的 AppDelegate.swift 文件,如下所示
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: LoginViewController())
return true
}
答案 17 :(得分:0)
let signUpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
// self.present(signUpVC, animated: false, completion: nil)
self.navigationController?.pushViewController(signUpVC, animated: true)
答案 18 :(得分:0)
我不确定尝试以下步骤,我认为可能会出现错误,原因如下。