I've been trying to pass data from one tableViewController to another, but as soon as I assign a value to a property in my second tableViewController the app crashes, giving me the message "...unrecognized selector sent to instance 0x715c340...".
Here is the code I have used:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"displayWordlist"]){
DisplayWordlist *dwl = (DisplayWordlist *)[segue destinationViewController];
dwl.apa = #"hej";
}
}
As soon as the program reaches 'dwl.apa = #"hej";' it crashes. I have both made a property of apa and synthesized it.
Make sure you have set the class of this view controller to DisplayWordlist in the Storyboard. If you don't, dwl will be assumed to be an instance of UIViewController despite the cast.
Related
I have a problem with presenting a ViewController. The Presented ViewController contains:
UIImageView
UIScrollView which contains a UICollectionView.
In the
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
I pass a value from the first to the second ViewController like this:
SecondViewController *secondVC = segue.destinationViewController;
secondVC.value = passValue;
When I launch the App and present the SecondViewController, everything works fine. If I go back to the first ViewController and then present the second ViewController again I get the following error message:
*** -[SecondViewController isKindOfClass:]: message sent to deallocated instance 0x170224e0
Of course the App breaks.
What could that mean and how could I solve this.
I already got much trouble with this problem and thank you for help.
Jannes
I'm having some trouble passing data from my UITableView to a UIViewController. I'm using PUSH in storyboard to go from my Cell to the UIViewController, and everything is working well. When I select a cell in my UITableView it pushes to the UIViewController and I get a nice back button to return to the UITableView. My problem is passing data to the UIViewController using the PrepareForSegue method ... this is what I'm doing:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowUserDetailSegue"])
{
UserDetailViewController *userInfo = segue.destinationViewController;
userInfo.myLabel.text = #"Hello World";
NSLog(#"I'm here");
}
}
The label "myLabel" does not change when the UIViewController is displayed. Any idea why?
I've used prepareForSegue before and have been able to pass data, not sure what I'm missing.
Also, xcode is showing all the proper code completions and there are no errors.
Thanks!
At the prepareForSegue stage the destination viewController hasn't yet loaded it's view so userInfo.myLabel is nil.
Add a new property to UserDetailViewController, set that to your string instead and in viewDidLoad set it up in your label.
Tell me, please, how should i transfer several variables from one ViewController to another?
For example, i have
ViewControlle.m
- (void) transferVar{
int a = 10;
int b = 11;
}
and i want to transfer them to another ViewController (SecondViewController.m). What should i do?
you can add a property on the second view controller for every variable you want to pass (or an array to contain them all), then if you are making the segue on the storyboard use the method
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
and inside it make this
SecondViewController *svc = (SecondViewController *) segue.destinationViewController;
There you have the view controller you want to pass values so only save it
svc.yourArray = #[[NSNumber numberWithInt:a],[NSNumber numberWithInt:b]];
Also it is highly recommendable to add a segue identifier to the storyboard segue and check
if([segue.identifier isEqualToString: #"theIdentifier"])
to be sure it is the segue that you wanted
Now on the viewDidLoadMethod you can use them.
If you are not using the storyboard segue just assign the value after initiating the view.
Here's a quick and dirty question:
I have a "main" view controller (VC) which is opened up from a parent VC, that uses a navigation controller. Then I have a "sub" VC that is opened (modal segue) from the "main" VC.
I have set a property in the main VC's interface:
#property (nonatomic) int myVar;
Then set it from the button's action that is touched to display the "sub"VC from the "main"VC's interface:
self.myVar=1;
I imported the mainVC.h in the subVC.h
Then at the viewDidLoad method of the subVC, I'm trying to access myVar's value, but can't do that with:
NSLog(#"Myvar is %i", ((mainVC*)self.parentViewController).myVar);
Which returns the value as 0.
And when I try presentingViewController method instead, I get an error (which did not cause the error when I pushed the segue instead of making it a modal:
[MainVC myVar]: unrecognized selector sent to instance
I'm trying to code for iOS 5, and needless to say that I'm still a noob.
What am I missing?
Thanks!
The parentViewController of the "sub" view controller is not the mainVC, it's the navigation controller. The mainVC is not accessible - for all you know, it may be deallocated to save memory.
If you need to pass data from the main controller to the sub controller on the segue, add an instance variable to the "sub" view controller, and set it in the prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"open_sub"]) {
subVC *sub = segue.destinationViewController;
sub.myVar = 1;
}
}
I am using storyboard in one of my application and while passing a property from SourceViewController to DestinationViewController in prepareForSegue method the property being passed is passed by reference instead of passed by value. Which is causing some trouble for me as in Destination View Controller if user fiddles with the values and taps on back then I get an updated value in my SourceViewController which I do not want.
Please see the following to get more information on the issue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{if([[segue identifier] isEqualToString:#"DestinationView"])
{
DestinationViewController *destinationViewController = [segue destinationViewController];
[destinationViewController setActivity:activity];
//Here the value of activity is passed by reference. I have verified the same by checking the address of activity variable in Variable pane of Xcode in Source and destination view controller
}
Now I am not sure how to only pass the value and not the reference in the above case.
try to use a copy of it
[destinationViewController setActivity:[activity copy]];
Implement its delegate copywithZone if needed
- (id)copyWithZone:(NSZone *)zone