iOS - Why is there an error? - ios

Here is my Code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"startGame"]) {
UIViewController *controller = (UIViewController *)segue.destinationViewController;
controller.grade = self.currentGrade;
}
}
Error:
(!)Semantic Issue:
Property "grade" not found on object of type 'UIViewController *'
Why do I get this error? I defined it as a property of the view controller and hooked it up in the storyboard.

Type cast to your viewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"startGame"]) {
YourViewController *controller = (YourViewController *)segue.destinationViewController;
controller.grade = self.currentGrade;
}
}

Possbile Case : Check Your UIViewController Custom Class. As the segue.destinationViewController; will return you the object of ViewController what you bind with your root view.
As In Your Case :
UIViewController *controller = (UIViewController *)segue.destinationViewController;
controller.grade = self.currentGrade;
Your Access the grade property of your UIViewController and its not finding so it may be one of the reason of this crash.

The grade is your custom property.
You can customize a viewcontroller, and it define a property: grade.

Related

iOS: passing object to viewController error: unrecognized selector sent to instance

I have project where I'm using UINavegationController between to views:
I'm using the navigation controller because I'm using modal presentation (Flip)
But my problem is I'm trying to pass NSString Object to ViewControllerB:
Code:
- (IBAction)goToB:(id)sender {
[self performSegueWithIdentifier:#"goToB" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"goToB"]) {
BViewController *vc = (BViewController*)[segue destinationViewController];
vc.commigFromA = #"I'm going to B!";
}
}
But I'm getting this error:
-[UINavigationController setCommigFromA:]: unrecognized selector sent to instance 0x7ff1c4830c00
Any of you knows why or a way around this error?
I'll really appreciate your help
It's crashing because the destinationViewController on the segue is actually a UINavigationController, not a BViewController, and UINavigationControllers don't know about a property comingFromA. You can test this theory by replacing prepareWithSegue like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"goToB"]) {
UINavigationController *nvc = (UINavigationController*)[segue destinationViewController];
BViewController *vc = (BViewController*)nvc.viewControllers[0];
vc.commigFromA = #"I'm going to B!";
}
}

iOS Segue PopOver How To Back Pass Data

i want send data view controller (left) from PopOverController (Right), how can I do it?
You can pass data in prepareForSegue method, but first add identifier for segue.
And use this code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showPopover"]) {
NSLog(#"FirstViewController: prepareForSegue");
PopOverController * popoverVC = segue.destinationViewController;
popoverVC.myProperty = #"Data to be passed";
}
}
First make one property which you get data of another viewcontroller .
In you case assume we want String data to first viewController so we make one property in second means PopOverController
#property (nonatomic, strong) NSString *recipeName;
After call prepare for segue method in first viewController in your case ViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"IdentifierOfPushViewController"]) {
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = #"Hello this passing data"
}
}

id to NSString return null when passing to another class

I have this code below that send the row to my another class:
[self performSegueWithIdentifier:#"MeInfos" sender:[NSString stringWithFormat:#"%d",(int)indexPath.row]];
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
MeInfosViewController *viewController = [[MeInfosViewController alloc] init];
viewController.paramethersToLoad = (NSString*)sender;
NSLog(#"Will receive -> %#",sender);
}
In my another class (MeInfoViewController) I have:
.h
#property (nonatomic, retain) NSString *paramethersToLoad;
.m
#synthesize paramethersToLoad;
-(void)viewDidAppear:(BOOL)animated{
NSLog(#"What comes? -> %#",paramethersToLoad);
}
This code has a little problem, in console I receive the following messages:
Will receive -> 4
What comes? -> (null)
Why this is happening and what the best way to solve it?
The view controller you're creating in prepareForSegue: isn't the instance that the segue will actually load. The view controller that actually gets displayed is created by the iOS runtime, and is passed to you as a property in the segue.
What you want is:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
MeInfosViewController *viewController = (MeInfosViewController *)segue.destinationViewController;
viewController.paramethersToLoad = (NSString *)sender;
}
Your prepareForSegue should look like this :
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"MeInfos"]) {
// Get reference to the destination view controller
MeInfosViewController *viewController = [segue destinationViewController];
viewController.paramethersToLoad = (NSString*)sender;
}
}

UITableViewController UINavigationController

I declared a GamePicerViewController (subclass of UITableViewController) .But I got a bug in the prepareForSegue:sender: method . The console says that gamePickerViewController is a UINavigationController. Have any ideas?
I post the GamePickerViewController.h PlayerDetailsViewController.m and the console in the pic abve.
problem solved.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"PickGame"]) {
UINavigationController *controller = segue.destinationViewController;
GamePickerViewController *gamePickerViewController = [controller viewControllers][0];
gamePickerViewController.delegate = self;
gamePickerViewController.game = _game;
}
}
I forgot the line:
UINavigationController *controller = segue.destinationViewController;

iOS - Error : unrecognized selector sent to instance in PrepareForSegue:

I wanna to pass data between controller, but I am getting this error, and I am blocked here :s
Error :
[UITabBarController setUID:]: unrecognized selector sent to instance
Code :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *test = (NSString *)sender;
if ([segue.identifier isEqualToString:#"segueno"]) {
FirstViewController *VC = (FirstViewController *)[segue destinationViewController];
VC.uID = test;
NSLog(#"%#",VC.uID);
}}
Here is storyboaard
Check what is actual type of VC during runtime. Looks like you cast to wrong type.
To check real type during runtime set breakpoint on line VC.uID = test;. In debug window you should have something like:
VC = (RealClass *) 0x312321312
Real class is not what you expected.
The most possible reason, that VC is UITabBarController. So you have to replace
FirstViewController *VC = (FirstViewController *)[segue destinationViewController];
with
UITabBarController *tabBarController = (UITabBarController *)[segue destinationViewController];
// replace 1 with real index of your FirstVC
FirstViewController *VC = [[tabBarController viewControllers] objectAtIndex:1];
segue destination is UITabBarController. so you need to check viewcontrollers list. then based on that u can look for uID.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *test = (NSString *)sender;
if ([segue.identifier isEqualToString:#"segueno"]) {
FirstViewController *firstVC ;
UITabBarController *destinat = [segue destinationViewController];
if ([destinat isKindOfClass:[UITabBarController class]]) {
for (id listOfViewControllers in [destinat viewControllers]) {
if ([listOfViewControllers isKindOfClass:[FirstViewController class]]) {
firstVC = listOfViewControllers;
break;
}
}
}
firstVC.uID = test;
NSLog(#"%#",firstVC.uID);
}}
Here is how I pass managedObjectContext between segues
In your class where you will pass the data use prepareForSegue call. (The assumption is this class has a variable called _managedObjectContext which can be passed along to the segue class)
Class to Segue From:
.h file:
#property (weak, nonatomic) NSManagedObjectContext *managedObjectContext;
.m file:
#synthesize managedObjectContext
The call to #synthesize will make the following:
a local variable called _managedObjectContext
a method to getManagedObjectContext
a method to setManagedObjectContext
Additionally add the following method to your class
// Pass on managedObjectContext
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// If the destination VC is able to take the setManagedObjectContext method the current objectContext will be passed along.
if ([segue.destinationViewController respondsToSelector:#selector(setManagedObjectContext:)]) {
[segue.destinationViewController performSelector:#selector(setManagedObjectContext:)
withObject:_managedObjectContext];
} else {
NSLog(#"Segue to controller [%#] that does not support passing managedObjectContext", [segue destinationViewController]);
}
}
Then in my "class" to receive the data I do:
in the .h file i have
#property (weak, nonatomic) NSManagedObjectContext *managedObjectContext;
and in the .m file i have:
#synthesize managedObjectContext;
What this does (with syntehsiation) is make a setManagedObjectContext and getManagedObjectContext call. Upon being about to segue I check to make sure the destinationController will "respond to" this method, if so the data gets set.
clear?

Resources