Swift / IOS8 error: "fatal error: Unable to deploy Optional.

I know that there are already a couple of questions on this subject, but I cannot understand this. Previous resolved issues suggest that the "profileViewController" is zero, but I don't know why this is. The user interface is fully software, without IB. Receiving:

"fatal error: Unable to deploy Optional. Some"

on pushViewController () in the following code:

class FavoritesViewController: UIViewController {

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Custom initialization
        self.title = "Favorites"
        self.tabBarItem.image = UIImage(named: "MikeIcon")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.redColor()

        let profileButton = UIButton.buttonWithType(.System) as UIButton
        profileButton.frame = CGRectMake(60, 300, 200, 44)
        profileButton.setTitle("View Profile", forState: UIControlState.Normal)
        profileButton.addTarget(self, action: "showProfile:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(profileButton)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func showProfile(sender: UIButton) {
        let profileViewController = ProfileViewController(nibName: nil, bundle: nil)
        self.navigationController.pushViewController(profileViewController, animated: true)

    }

Here is the relevant part of AppDelegate.swift :

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // Override point for customization after application launch.

        let feedViewController = FeedViewController(nibName: nil, bundle: nil)
        let favoritesViewController = FavoritesViewController(nibName: nil, bundle: nil)
        let profileViewController = ProfileViewController(nibName: nil, bundle: nil)

        let tabBarController = UITabBarController()
        self.window!.rootViewController = tabBarController

        tabBarController.viewControllers = [feedViewController, favoritesViewController, profileViewController]        


        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()
        return true
    }

screenshot

+4
source share
1 answer

Is a navigation manager object self.navigationController nil?

navigationController . UINavigationController, .

:

if (self.navigationController)
{
    self.navigationController.pushViewController(profileViewController, animated: true) 
}

:

self.navigationController?.pushViewController(profileViewController, animated: true)

? , self.navigationController nil.

+2

Source: https://habr.com/ru/post/1547509/


All Articles