导航控制器推送视图控制器 [英] Navigation Controller Push View Controller

查看:42
本文介绍了导航控制器推送视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

如何简单地使用按钮的触摸事件从一个视图控制器导航到另一个视图控制器?

How to navigate from one view controller to another simply using a button's touch up inside event?

更多信息

我在示例项目中逐步尝试的是:

What I tried in a sample project, in steps, was:

  1. 创建示例单视图应用程序.

  1. Create the sample single view application.

添加一个新文件 -> 带有 XIB 的 Objective-C 类用于用户界面 (ViewController2).

Add a new file -> Objective-C Class with XIB for user interface (ViewController2).

在 ViewController.xib 中添加一个按钮并控制单击该按钮到 ViewController.h 以创建内部事件.

Add a button into ViewController.xib and control click the button to ViewController.h to create the touch up inside event.

到ViewController.m中新建的IBAction,改成这个...

Go to the newly made IBAction in ViewController.m and change it to this...

- (IBAction)GoToNext:(id)sender 
{
    ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];

    [[self navigationController] pushViewController:vc2 animated:YES];
}

代码运行没有错误,我用 NSLog 测试了按钮的功能.但是它仍然没有将我导航到第二个视图控制器.任何帮助将不胜感激.

The code runs without errors and I tested the button's functionality with NSLog. However it still doesn't navigate me to the second view controller. Any help would be appreciated.

推荐答案

Swift3

 **Push**

喜欢

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController 
 vc.newsObj = newsObj
 navigationController?.pushViewController(vc,
 animated: true)

或更安全

  if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController {
        viewController.newsObj = newsObj
        if let navigator = navigationController {
            navigator.pushViewController(viewController, animated: true)
        }
    }

现在

   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController
      vc.newsObj = newsObj
           present(vc!, animated: true, completion: nil)  

或更安全

   if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController
     {

     vc.newsObj = newsObj
    present(vc, animated: true, completion: nil)
    }





//Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"
                                                       bundle:nil];
    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
    self.window.rootViewController = navigation;
    [self.window makeKeyAndVisible];
    return YES;
}


//ViewController.m

- (IBAction)GoToNext:(id)sender 
{
    ViewController2 *vc2 = [[ViewController2 alloc] init];     
    [self.navigationController pushViewController:vc2 animated:YES];
}

迅速

//Appdelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let navigat = UINavigationController()
    let vcw = ViewController(nibName: "ViewController", bundle: nil)

    // Push the vcw  to the navigat
    navigat.pushViewController(vcw, animated: false)

    // Set the window’s root view controller
    self.window!.rootViewController = navigat

    // Present the window
    self.window!.makeKeyAndVisible()
    return true
}

//ViewController.swift

@IBAction func GoToNext(sender : AnyObject)
{
    let ViewController2 = ViewController2(nibName: "ViewController2", bundle: nil)
    self.navigationController.pushViewController(ViewController2, animated: true)
}

这篇关于导航控制器推送视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆