iOS - Push Viewcontroller from Code and Storyboard

Swift PushViewController From A Different Storyboard

This means that the following line is either nil or not of type SecondVC.

secondStoryBoard.instantiateViewController(withIdentifier: "secondVC")

I would verify in your storyboard that you have an identifier secondVC and that view controller is of type SecondVC.


Edit:

Based on the new code the OP posted in the question, changing the SecondVC to the following should work.

class SecondVC: ViewController
{
@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
mapView.isHidden = true
}
}

Basically this will wait until the view has loaded before accessing the mapView. For the original code, you were trying to access mapView before it has actually been loaded.


Edit 2:

It looks like mapView outlet is not properly linked to the storyboard.

Push new view controller when button is pressed

Got an answer!

In my SceneDelegate.swift I set:

let menuViewController = UINavigationController(rootViewController: MenuViewController())
window?.rootViewController = menuViewController

Now it works! Thanks for the comments everyone :)

Programmatically navigate to another view controller/scene

I already found the answer

Swift 4

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
self.present(nextViewController, animated:true, completion:nil)

Swift 3

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)

How to push and present to UIViewController programmatically without segue in iOS Swift 3

Push

do like

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController
vc.newsObj = newsObj
navigationController?.pushViewController(vc,
animated: true)

or safer

  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)

or safer

   if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController
{

vc.newsObj = newsObj
present(vc, animated: true, completion: nil)
}

How to push viewcontroller from appdelegate in storyboard inside navigation controller

actually SwLRevalViewController Taken the ownership of Root, so do like this

in Appdelegate.m

//initially navigate to your Main controller on SWL

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{

NSString *alert = userInfo[@"aps"][@"redirect_url"];
[[NSUserDefaults standardUserDefaults]setObject:alert forKey:@"itemType"];
[[NSUserDefaults standardUserDefaults]synchronize];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SWRevealViewController *main = (SWRevealViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"SWRevealViewController"];

self.window.rootViewController = main;
}

//it automatically redirect to your root controller

on that main view controller ViewDidLoad check / Navigate to your photo view controller

        NSString *getType=[[NSUserDefaults standardUserDefaults]objectForKey:@"itemType"];
if ([gettypeofItem isEqualToString:@"something"])
{
yourPhotoViewController *ivc=[self.storyboard instantiateViewControllerWithIdentifier:@"yourPhotoViewController"];

[self.navigationController pushViewController:ivc animated:YES];
}
else

{

// do nothing

}


Related Topics



Leave a reply



Submit