Reputation: 848
I know there are already a couple issues on this, but I can't figure it out. The previous solved issues would suggest that 'profileViewController' is nil, but I don't know why that would be the case. The UI is completely programmatic, no IB. Getting:
"fatal error: Can't unwrap Optional.None"
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's the relevant portion 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
}
Upvotes: 4
Views: 788
Reputation: 751
Is the Navigation Controller object self.navigationController
nil
?
The variable type on the navigationController
property is an unwrapped optional. If your View Controller is not inside a UINavigationController
that would be the problem.
To prevent the crash code along the following lines should be written:
if (self.navigationController)
{
self.navigationController.pushViewController(profileViewController, animated: true)
}
Alternatively you could write the code as:
self.navigationController?.pushViewController(profileViewController, animated: true)
The ?
operator will prevent any further code from being executed, if self.navigationController
evaluates to nil
.
Upvotes: 2