ObjC - presentViewController breaks - objective-c

I am trying to programmatically show another View Controller. I wrote some code that I learned from examples, it gives no errors. But it Breaks and shows this in green:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); thread 1 : signal SIGABRT
I used this code:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"secondViewController"];
[self presentViewController:vc animated:NO completion:nil];
Is there something wrong with my code, or is it something else?
Thanks in advance.

This happens probably because you don't have a UIViewController with Storyboard ID secondViewController in your storyboard.
You can drag a UIViewcontroller to your Storyboard and in Identity inspector set Storyboard ID to secondViewController.
See the image below
I hope this helps :)

Related

Navigate to storyboard from a viewController programmatically

in my iOS application, I would like to navigate programmatically to a whole storyboard (tabBarController) from a viewcontroller.
I found this solution:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"SMShopViewController"];
[self.navigationController pushViewController:controller animated:YES];
but it is not what I need, I want to show directly my tabBarController
thank you for your reply
As i understand, You have UITabBarController on storyboard like this and you want show it from code?
So, Firstly set storyboard identifier for UITabBarController
Then in code
UITabBarController *tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"KITTabBarController"];
[self.navigationController pushViewController:tabBar animated:NO];
If you have parent navigation controller better way to do it is:
[self.navigationController setViewControllers:#[tabBar] animated:YES];

Switch Active View Controller Programmatically

I have 2 views as part of my Navigation Controller.
I created a class for my Navigation Controller with a public method that certain actions on the first view will trigger to load the Detail View Controller. I am importing the Detail View Controller, but nothing in my code is working. I know the method is being called correctly because I'm logging it.
What am I doing wrong here?
#import <UIKit/UIKit.h>
#interface NearbyController : UINavigationController
- (void) nextpage;
#end
#import "NearbyController.h"
#import "DetailView.h"
#implementation NearbyController
-(void) nextpage {
NSLog(#"working");
DetailView *nextView = [[DetailView alloc] init];
[self pushViewController:nextView animated:YES];
}
#end
If you initialize a view controller programmatically you should use this init.
If you have set a storyboard ID you can do this
DetailView *nextView = (DetailView *)[self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewControllerID"];
[self pushViewController:nextView animated:YES];
If you want to load from a XIB you can call
DetailView *nextView = [[DetailView alloc] initWithNibName:#"MyDetailViewControllerID" bundle:nil];
[self pushViewController:nextView animated:YES];
Hope it helps!
If you're using storyboards what you wanna do is setup the storyboard id in the identity inspector on your DetailViewViewController
Then you would wanna instantiate your view controller like so:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
DetailView *nextView =[sb instantiateViewControllerWithIdentifier:#"DetailViewViewController"];
/* your identifier is your storyboard id*/
sb = nextView.storyboard;
Then you should be able to push your view controller.
Im still new to this but i hope this helps!

Syntax error trying to set a property value

I want to pass a id from table view (TableViewController) to another view (ViewController) after tap a table cell. I have declared a storyboard ID "ViewController" for ViewController
Here is TableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
viewController.id=#"test";
[self.navigationController pushViewController:viewController animated:YES];
}
Here is ViewController.h
#property (nonatomic) NSString *id;
ViewController.m
- (void)viewDidLoad{
id= [[NSString alloc] init];
}
But the statement viewController.id=#"test"; occurred the syntax error.
id is a reserved word in Objective-C. Do not name any variable or property id. Rename it to something else.
Also, your viewController variable is declared as UIViewController. There is no id property in UIViewController. Change the type of viewController to the actual type it is (ViewController?).
ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
Check the link below, covers these basics for beginners on how to pass data between UITableViewController to another view.
Storyboards Segue Tutorial: Pass Data Between View Controllers
By the way, i don't think you can use "id" as your property name.

Passing property value using instantiateViewControllerWithIdentifier with storyboards

Apologies if my terminology is a bit off, new to objective C!
I am trying to pass a value from one UIViewController class to another. I am using storyboards. I am able to display the second ViewController using the following code:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"FormView"];
[self presentModalViewController:vc animated:YES];
That works fine. In my second ViewController's (FormView) header file I set a property like so:
#interface FormController : UIViewController {
NSString *selectedBooking;
}
#property (nonatomic, retain) NSString *selectedBooking;
#end
How do I "pass the value" from my first Controller to the second Controller? As far as I understand I have to set the property like so:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"FormView"];
vc.selectedBooking = #"test";
[self presentModalViewController:vc animated:YES];
Which gives me the error: Property 'selectedBooking' not found on object of type 'UIViewController'.
What is the correct way to set the property? Is there something else I should be using rather that instantiateViewControllerWithIdentifier?
Thanks
You'll need to cast it.
i.e.
FormController *fc = (FormController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"FormView"];
fc.selectedBooking = #"test";
[self presentModalViewController:fc animated:YES];
then the rest of your code will work :D
Just in case if you do not wish to import the class FormController and go with it you can do as follow. really very handy:
UIStoryboard *mainStoryboard = ..
UIViewController *vc = ...
[vc setvalue:#"test" ForKey:#"selectedBooking"]; //using KVC
[self presentModalViewController:vc animated:YES];
NOTE: This will only work with properties.

How to use the animation which is used to reveal the lower view?

Does somebody know how to use the animation which is used to reveal the lower view(Map,Satellite,Hybrid, List) in the maps application?
You have to create a new ViewController
Eg:
MyViewController *myvc = [[MyViewController alloc] initWithNibName: #"blablabla"];
[self presentModalViewCOntroller: myvc animated: YES];
Giving that MyViewController is a subclass of UIViewController for example and this i run from another viewcontroller.