为了账号安全,请及时绑定邮箱和手机立即绑定

导航控制器推式视图控制器

导航控制器推式视图控制器

Go
UYOU 2019-12-15 16:12:42
题如何仅使用按钮的touch up内部事件从一个视图控制器导航到另一个视图控制器?更多信息我在一个示例项目中尝试执行的步骤是:创建示例单视图应用程序。为用户界面(ViewController2)添加一个新文件->具有XIB的Objective-C类。在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测试了按钮的功能。但是,它仍然不能将我导航到第二个视图控制器。任何帮助,将不胜感激。
查看完整描述

3 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

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)

}



查看完整回答
反对 回复 2019-12-16
?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

  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)



查看完整回答
反对 回复 2019-12-16
?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

对于Swift,请使用以下代码:


 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    self.window!.backgroundColor = UIColor.whiteColor()


    // Create a nav/vc pair using the custom ViewController class


    let nav = UINavigationController()

    let vc = NextViewController(nibName: "NextViewController", bundle: nil)


    // Push the vc onto the nav

    nav.pushViewController(vc, animated: false)


    // Set the window’s root view controller

    self.window!.rootViewController = nav


    // Present the window

    self.window!.makeKeyAndVisible()

    return true


}

ViewController:


 @IBAction func Next(sender : AnyObject)

{

    let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil)


    self.navigationController.pushViewController(nextViewController, animated: true)

}



查看完整回答
反对 回复 2019-12-16
  • 3 回答
  • 0 关注
  • 591 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信