Question

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

More Info

我在一个示例项目中try 了以下步骤:

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

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

  3. 在ViewController中添加一个按钮.xib和control单击按钮查看控制器.h创建内部事件的润色.

  4. 转到ViewController中新制作的iAction.我把它改成这个...

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

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

推荐答案

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)
        }
    }

present

   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];
}

swift

//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)
}

Swift相关问答推荐

在同步参与者隔离上下文中调用参与者隔离实例方法

String.Index encodedOffset已弃用,但建议的替代方案不起作用

Result类型的初始值设定项失败?

关闭 SwiftUI TabView 中的子视图

更改 SwiftUI 中按钮矩阵的填充 colored颜色

如何使用 Swift 在非本地函数中切换()一个 Bool?

Swift:结果的失败类型不能是协议 - Type 'any ShadowError' cannot conform to Error

memcpy 复制带偏移量的数组

ScreenCaptureKit/CVPixelBuffer 格式产生意外结果

在 SwiftUI 中将属性显式设置为其默认值

Swift 并发:@MainActor 对象上的通知回调

使用泛型的 Swift 枚举值

SwiftUI:决定键盘重叠的内容

如何在 RxSwift 中获取 controlEvent 类型?

swift中的SequenceType和CollectionType有什么区别?

如何删除桥头而不出错?

在 Swift 中获取双精度的小数部分

Swift - 导入我的 swift 类

将强制向下转换视为可选将永远不会产生零

将 UIImage 剪成圆形