导航控制器推视图控制器

 长不大的二楞子 发布于 2023-02-08 14:54

如何使用按钮内部事件触摸从一个视图控制器导航到另一个视图控制器?

更多信息

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

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

    添加一个新文件 - > Objective-C Class with XIB for user interface(ViewController2).

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

    转到ViewController.m中新创建的IBAction并将其更改为此...

    - (IBAction)GoToNext:(id)sender 
    {
        ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
    
        [[self navigationController] pushViewController:vc2 animated:YES];
    }
    

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

2 个回答
  • 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)
    }
    

    2023-02-08 14:56 回答
  •     UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil];
        MemberDetailsViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentiferInStoryBoard"];
    [self.navigationController pushViewController:viewControllerName animated:YES];
    

    斯威夫特4:

    let storyBoard = UIStoryboard(name: "storyBoardName", bundle:nil)
    let memberDetailsViewController = storyBoard.instantiateViewController(withIdentifier: "viewControllerIdentiferInStoryBoard") as! MemberDetailsViewController
    self.navigationController?.pushViewController(memberDetailsViewController, animated:true)
    

    2023-02-08 14:56 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有