ios - 导航 Controller 推送 View Controller

标签 ios objective-c iphone swift

问题

如何简单地使用按钮的触摸内部事件从一个 View Controller 导航到另一个?

更多信息

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

  1. 创建示例单 View 应用程序。

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

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

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

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

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

最佳答案

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

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

关于ios - 导航 Controller 推送 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20742745/

相关文章:

ios - 使用属性在代码中创建 UI

ios - 具有泛型类型和 UITableview 的 Swift 3 ViewController

iphone - 如何移除 KVO 观察者?

iphone - 如何检测 AVCaptureDevice 的闪光灯何时完成以及静止图像捕获何时开始?

ios - UICollectionViewCell 图像中的 UIImageView 不适合

objective-c - 如何在文件名中使用十六进制计数进行编程加载?

iphone - 我想要带重复计数的时间表计时器

iphone - 从 objective-c 中的方法返回一个整数

iphone - 将枚举值保存到字典中

iphone - 从线程调用委托(delegate)方法