How to pass data to ViewController, using UIViewController - ios

How to pass data to ViewController using this code?
UIStoryboard *main = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [main instantiateViewControllerWithIdentifier:#"Catalog"];
[self presentViewController:vc animated:YES completion:nil];
I want to pass data in example to itemId, like here:
ViewController *vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
vc.itemId = 0;
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:vc animated:YES completion:nil];

Type cast your UIViewController if it's ViewController
ViewController *vc = (ViewController*)[main instantiateViewControllerWithIdentifier:#"Catalog"];
vc.itemId = 0;
[self presentViewController:vc animated:YES completion:nil];

This should also work for you.
ViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:#"Catalog"];
vc.itemId =0;
[self presentViewController:vc animated:YES completion:Nil];

Related

Dismiss to other storyboard

In my project i have 2 storyboards. One for login and one for the main.
After login is complete i present the main storyboard using this method:
-(void)successLogin {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateInitialViewController];
[self presentViewController:vc animated:YES completion:nil];
}
My question is how do I get back to the login storyboard if i want to put logout button in the main storyboard. I tried doing the following:
[self dismissViewControllerAnimated:YES completion:nil];
And it still doesn't work.
try this on LOGOUT click event:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LoginviewController *Login = [storyboard instantiateViewControllerWithIdentifier:#"LoginviewController"];
[self.navigationController pushViewController:Login animated:YES];
try to set one view controller as root view controller then present another view controller.
[[[UIApplication sharedApplication] keyWindow] setRootViewController:viewController];
[viewController presentViewController:VC animated:YES completion:nil];
you will come to the rootViewController when you dismissViewControllerAnimated:
logout success
Try this code it works for you
-(void)successlogout
{
//[self dismissViewControllerAnimated:NO completion:nil];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
login *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"login"];
[self presentViewController:vc animated:NO completion:nil];
}

Black screen after button click

I'm quite new to Objective-C. I want to go to another view controller but it shows me only the black screen after the button click. Is there something that I'm missing here?
Here is my code:
- (IBAction)measuring1stscan:(id)sender {
measuringscan *second = [[measuringscan alloc]initWithNibName:nil bundle:nil];
[self presentViewController:second animated:NO completion:NULL];
}
*measuringscan is the name for another view controller
viewConreoller default is no color, it is black. just add second.view.backgroundColor = [UIColor redColor];
- (IBAction)measuring1stscan:(id)sender {
measuringscan *second = [[measuringscan alloc] init];
second.view.backgroundColor = [UIColor redColor];
[self presentViewController:second animated:NO completion:NULL];
}
#rose is right try to give different color and then check.
Still you face same issue then try to run below code.
Embadded viewcontroller into navigation controller and give unique identifier to second viewcontroller.
- (IBAction)measuring1stscan:(id)sender
{
UIStoryboard *st = [UIStoryboard storyboardWithName:[NSString stringWithFormat:#"StoryboardWithName"] bundle:NULL];
measuringscan *second = [st instantiateViewControllerWithIdentifier:#"viewcontrollerIdentifier"];
[self.navigationController pushViewController:second animated:YES];
}
If you are using storyboard then use following
measuringscan *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"StoryboardIdentifier"];
[self presentViewController:controller animated:YES completion:nil];
or
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[NSString stringWithFormat:#"StoryboardName"] bundle:nil];
measuringscan *controller = [storyboard instantiateViewControllerWithIdentifier:#"viewcontroller'sIdentifier"];
[self presentViewController:controller animated:YES completion:nil];
And if you aren't using storyboard instead you are using xib's then use following
measuringscan *controller = [[measuringscan alloc] initWithNibName:nil bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
Points to remember (as said by #rmaddy)
Class name should be start with capital letter
Variable name should start with small letter
It is the naming convention that should be followed by a developer.
For more details click here

Game Center: Matchmaking

In my card playing game I am trying to create a match, which, for a part, works;
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match {
self.match = match;
match.delegate = self;
if (!_matchStarted && match.expectedPlayerCount == 0) {
NSLog(#"Ready to start match!");
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"GameVC"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[viewController presentViewController:vc animated:YES completion:nil];
//[viewController dismissViewControllerAnimated:YES completion:nil];
}
}
After a match is created successfully I want the GKMatchmakerViewController to be dismissed and I want the "vc" UIViewController to be shown. In the above example this is accomplished, but the GKMatchmakerViewController does not get dismissed.
If I remove the comment quotes it will be loaded after it got dismissed somehow and if I place the line above the presentViewController line I get an error stating that I am trying to present a view controller on a view controller that is not in the view hierarchy.
How do I dismiss the GKMVC and show the "vc" at the "same" time?
Thanks!
You need to dismiss the GKMatchMakerViewController, then use your current view controller to present the new modal:
if (!_matchStarted && match.expectedPlayerCount == 0) {
NSLog(#"Ready to start match!");
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"GameVC"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[viewController dismissViewControllerAnimated:YES completion:nil];
[self presentViewController:vc animated:YES completion:nil];
}
Edit: A trick to get the current root view controller is [[UIApplication sharedApplication] delegate].window.rootViewController. So you should be able to present your modal with this:
[[[UIApplication sharedApplication] delegate].window.rootViewController presentViewController:vc animated:YES completion:nil];

Objective-C: whose view is not in the window hierarchy

I have this code that works so that once i press a button the view changes in my storyboard:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:#"view1"];
[self presentViewController:viewController animated:YES completion:nil];
This code work but when i use it again to change back to my original view using:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:#"view2"];
[self presentViewController:viewController animated:YES completion:nil];
It does not work and i receive this error:
Warning: Attempt to present <UIViewController: 0x863ad30> on <UITabBarController: 0x86309b0> whose view is not in the window hierarchy!
The identifies are set and i am not the functions in my viewdidload so i do not know how to fix this.
Can anybody help?
Try this,
First Method
-(IBAction)signin:(id)sender
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"viewController"];
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];
}
Second method (to return back to the previous view)
- (IBAction)signout:(id)sender
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"parentViewController"];
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];
}
This might happen if you call the method before calling makeKeyAndVisible, so move the calling after that.
If not try the hack below:
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:viewController animated:YES completion:nil];
The error message says all: you have to add the view controller's view to the window before presenting the view controller:
UIWindow* keyWindow= [[UIApplication sharedApplication] keyWindow];
[keyWindow addSubview: viewController.view];
[self presentViewController:viewController animated:YES completion:nil];

Presenting ViewController over another modalView

I have presented a modalView(B) on another modalView(A) which is presented on a navigation controller.
The problem is that the dealloc method of modalView(A) is never called, and somewhere the app crashes while fetching from NSUserDefaults.
I could not figure out what exactly is the problem?
EDIT :-
Code
ViewControllerA * aViewController = [[ViewControllerA alloc] initWithNibName:#"ViewControllerA" bundle:[NSBundle mainBundle]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: aViewController];
navController.navigationBar.barStyle = UIBarStyleBlack;
[self presentViewController:navController animated:YES completion:^{}];
[aViewController release];
[navController release];
modalView(B)
ViewControllerB *bViewController = [[ViewControllerA alloc] initWithNibName:#"ViewControllerA" bundle:[NSBundle mainBundle]];
[self presentViewController: bViewController animated:YES completion:^{}];
[bViewController release];

Resources