I have a modal view controller that attempts to set an flag (an NSNumber property) of the source view controller that called it in its prepareForSegue method. It fails to build with the error "No known instance method for selector 'setGoToEditNewNote:'". Here is the code:
Source View Controller .h:
#property (strong, nonatomic) NSNumber *goToEditNewNote;
Source View Controller .m:
#synthesize goToEditNewNote;
...
- (void)viewDidLoad
{
[super viewDidLoad];
// clear the flag
goToEditNewNote = [[NSNumber alloc] initWithBool:FALSE];
...
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([goToEditNewNote boolValue] == TRUE) {
goToEditNewNote = FALSE;
[self performSegueWithIdentifier: #"editNote" sender: self];
...
Modal View Controller .h:
Modal View Controller .m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"done"])
{
[self done];
[[segue destinationViewController] setGoToEditNewNote:TRUE]; <<< get error here
}
}
I suspect the problem may have to do with goToEditNewNote not being retained when the modal view is loaded, but i don't understand why not. I have set other properties such as the managedObjectContext in a similar way with success. Please be as specific as possible in your answer as I am a novice with ARC. Thanks - Tom
destinationViewController is of the id type which doesn't contain a goToEditNewNote property. You probably want to cast destinationViewController to the SourceViewController type. This is usually a warning but it sounds like you're treating all warnings as errors (I do this too).
Your -prepareForSegue:sender: should look something like this.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"done"])
{
[self done];
SourceViewController *sourceViewController = (SourceViewController *)[segue destinationViewController];
[sourceViewController setGoToEditNewNote:[NSNumber numberWithBool:YES]];
}
}
I had the same problem. It took me 2 days and could't find any right answer on the web. Strangely, the same code in Stanford CS193p is working. Luckily I solved it now! The "set property method can not be found" error's cause is forgetting to #import "xxxx.h", where xxxx is the name of your segue's destination view controller. If you dodn't import, it never knows what xxxx's properties are!
Easy, now the segue works.
At first if you are using ARC you NEVER have to use retain (or release, too). You only have to instantiate an Object, but you dont have to care about to delete it. But take care if Objects references in a circle at each other. If A references B, B references C, C references A and you access these Variables via a Variable D (wich points at A) and you no more use D the circle of A-B-C stays in Memory. Then you have to define one of the A-B-C References as 'weak'.
But to your real question: Is it possible that you missed the #synthesize Statement in the Implementation file?
The error is telling you that you don't have any instance method that corresponds to the "setGoToEditNewNote:" selector; did you #synthesize your property (or write the accessors yourself)?
Related
I'm completely new to objective c and ios development, I'm simply trying to navigate to a new scene on my storyboard, and upon navigating change the text of a UIlabel to something.
The way I have this set up is I have a View controller and a tableview controller, each with their own header and implementation files. I'm using a segue from a button on the view controller to navigate to the tableview.
I did read this stack overflow post (How do I pass information between storyboard segues?) which explains how to pass values on a segue and have implemented it as such:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"prepareForSegue: %#", segue.identifier);
if([segue.identifier isEqualToString:#"navtotablebut"])
{
TableViewController *testcontroller = segue.destinationViewController;
testcontroller.testpass.text = #"Testing123";
NSLog(#"%#",[NSString stringWithFormat:#"testpass has been changed to %#", testcontroller.testpass.text]);
}
}
The logs verify that this segue method does get called, and it is able to correctly identify the id I gave it (navtotablebut). I then attempt to set the value of my UIlabel's text property on my tableview controller to a simple string "Testing123". However not only does this change not reflect on the new screen, but my print statement afterwards seems to think testcontroller.testpass.text is equal to (null).
Am I doing something horribly wrong here?
You are setting the value of label testcontroller in TableViewController before the view of `TableViewController ' is drawn.
Please note: update the view in viewdidLoad or viewDidLayoutSubviews if using Autolayout.
Best way is pass your value as a string to other view controller and then populate.
Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Here's an example...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
also with performSegueWithIdentifier:sender: method to activate the transition to a new view based on a selection or button press.
// When any of my buttons are pressed, push the next view
-(IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MySegue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}
In TableViewController, You need to create property,
TableViewController.h
#property (nonatomic, strong) NSString *subject;
you pass value like that,
In ViewController,
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier]isEqualToString:#"Detail"])
{
TableViewController *testcontroller = segue.destinationViewController;
testcontroller.testpass.text = #"Testing123";
detailView.subject = testcontroller.testpass.text;
}
}
I started using storyboards but am noticing one very significant difference: The storyboard appears to be instantiating a new ViewController each time I navigate back and forth.
Example: I create two new Xcode projects based on the Master-Detail template. In Case 1, I use the Storyboard and in Case 2 I use the .xib.
Normally I would expect these to behave identically, but they don't!
In both of the DetailViewController.m I add the following method:
-(void)viewDidAppear:(BOOL)animated{
if (xposition ==0) {
xposition=50;
}else{
xposition = xposition+50;
}
NSLog(#"xposition update %d", xposition);
}
(I have also declared the xposition as an "int" instance variable in the header):
When I run the Storyboard version and tap the "+" and navigate in and out of the DetailViewController then my NSLog statement keeps giving me "xposition update 50".
By contrast, for the .xib version I get my expected behavior where each time I navigate in and out of the DetailViewController that the "position" increments by 50: so 50, 100, 150 etc.
How do I fix Storyboard to make it behave in the same way as the .xib based version. Specifically, I want to only instantiate the DetailViewController once.
EDIT: Answering my own question. I got some help on this and wanted to post the answer that worked for me.
When you first perform the segue store the destination viewcontroller in a property (see method "PrepareForSegue". My VC is called MyViewController)
Then create the delegate method called "shouldPerformSegueWithIdentifier" and use this to intercept the segue and manually present the stored ViewController for all subsequent segues.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UIViewController *destination = segue.destinationViewController;
NSLog(#"identifier = %#", [segue identifier]);
if([[segue identifier] isEqualToString:#"mySegue"]) {
self.myViewController = (MyViewController*)destination;
NSLog(#"Saving myViewController for later use.");
}}
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if([identifier isEqualToString:#"mySegue"]) {
if(self.myViewController != nil) {
NSLog(#"Using the saved myViewController.");
[self.navigationController pushViewController:self.myViewController animated:YES];
return NO;
}else {
return YES;
}
}
return YES;}
When you navigate back and forth your storyboard pops off your DetailViewController. Because it is not retained by anything else it will be released, this is normal behavior.
If you want to keep the instance you'll have to retain it in the ViewController you are calling it from and use it later on again. Check this question for an example
Edit:
I think you solved the problem but here is an example:
Create a property for you viewcontroller in your interface, say myViewController
Retain the viewcontroller in the prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
[self setMyViewController:[segue destinationViewController]];
}
This it not leaking memory, your example could leak in some cases. Check out this guide here.
The next time the seque will be performed check if the property is already set and if so use it:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if([self myViewController] != nil){
[[self navigationController] pushViewController:[self myViewController] animated:YES];
return NO;
}else{
return YES;
}
}
I am able to pass data from one view to other view without any problem. But here is my situation i am facing problem.
Let's say
-> Passing data to other view
ViewA->ViewB This working perfectly fine
ViewB->ViewC This working perfectly fine
ViewC->ViewB Here is the problem.
I tried using push segue ,it goes to ViewB but when i press back button it goes to ViewC->back button->ViewB->back button->View A. From ViewB when i press back button it must goes to ViewA.
Tried using modal segue, it goes to ViewB but then i can't go any where.
As i am new to iOs I am really haven't got any idea about how to achieve this?
How to pass data back to ViewB from ViewC?
I think you guys can help me.
Edit
In View A i am calling ViewB like this
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"RemixScreen"])
{
if([self.recipeFrom isEqualToString:#"ViewB"])
{
ViewB *reciepe = [segue destinationViewController];
//somedata i will pass here
}
}
}
In View B i am calling like this
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"ViewA"])
{
ViewA *data = [segue destinationViewController];
//some data
}
}
Thanks for your help guys.
The correct way to do this is to use delegates. You use properties in prepareForSegue to pass data forward and delegates to pass data back.
The way it works is to have a delegate property in ViewC that is set by ViewB in prepareForSegue. That way ViewC can communicate with ViewB via a protocol you set up.
EDIT: Adding code to demonstrate:
ViewControllerBInterface:
#protocol ViewBProtocol
- (void)setData:(NSData *)data;
#end
#interface ViewBController: UIViewController <ViewBProtocol>
...
#end
Here we make ViewBController follow our protocol that ViewCController will communicate to.
Next up is ViewCController interface:
#interface ViewCController: UIViewController
#property (nonatomic, weak) id<ViewBProtocol> delegate;
...
#end
Now we look at ViewBController's prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController* controller = [segue destinationViewController];
if ([controller isKindOfClass:[ViewCController class]])
{
ViewCController* viewCController = (ViewCController *)controller;
viewCController.delegate = self;
}
}
As you can see we link the ViewC controller to the ViewB one via the delegate property. Now we do something in ViewC:
- (void)sendData:(NSData *)data
{
[self.delegate setData:data];
}
And you can use that in your viewWillDisappear method of ViewCController if you wanted to.
The way I have gotten around this problem in a recent project is the following;
View A is the parentViewController, so can be accessed from View B and View C at any time like this;
ViewAClass *parentView = (ViewAClass *)self.parentViewController;
You can then read and write to a property of the View A, like;
NSString *string = parentView.someStringProperty;
or
parentView.someStringProperty = #"Hello World";
EDIT - "To go back from view B to ViewA with out back button"
[parentView popViewControllerAnimated:YES];
Are you using navigation controller? If yes, you can easily pass data with Singleton, which is my favorite way to do that and also its easy to use. Otherwise, if you're trying to navigate through views with buttons or something, try to set a identifier to your segue, then call the method "performSegueWithIdentifier"
I think I'm following how delegation works, here's the tutorial I followed, but I'm messing up somewhere. I'm expecting my delegate to NSLog but it's not. Can anyone find out what am I missing or doing wrong?
My MainViewController.h:
#interface MainViewController : UITableViewController < AddClassDelegate >
MainViewController.m:
- (void)cancelAddingClass {
NSLog(#"Canceled Yo");
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
/*
When a row is selected, the segue creates the detail view controller as the destination.
Set the detail view controller's detail item to the item associated with the selected row.
*/
if ([[segue identifier] isEqualToString:#"addClassSegue"]) {
UINavigationController *nc = (UINavigationController *)segue.destinationViewController;
AddClassViewController *addClassVC = (AddClassViewController *)[nc.viewControllers objectAtIndex:0];
addClassVC.delegate = self;
}
My modal view controller AddClassViewController.h:
#protocol AddClassDelegate <NSObject>
- (void)cancelAddingClass;
#end
#interface AddClassViewController : UITableViewController
#property (weak, nonatomic) id< AddClassDelegate > delegate;
- (IBAction)cancelButtonPressed:(id)sender;
AddClassViewController.m:
#synthesize delegate;
- (IBAction)cancelButtonPressed:(id)sender {
[self.delegate cancelAddingClass];
}
cancelButtonPressed:
is hooked up to the modal view's Cancel button in Storyboard.
Your code looks fine, which suggests the problem is somewhere we can't see. My guess is here:
AddClassViewController *addClassVC = [segue destinationViewController];
addClassVC.delegate = self;
NSLog(#"segued");
Have you embedded your modal view controller in a navigation controller? If so, destinationViewController gives you the navigation controller, not the AddClassViewController. Check what class addClassVC actually is in the debugger.
If it is a navigation controller, no problem, you just need to get to your actual VC using the .viewControllers property. On several lines to make it simpler to understand:
UINavigationController *nc = (UINavigationController *)segue.destinationViewController;
AddClassViewController *addClassVC = (AddClassViewController *)[nc.viewControllers objectAtIndex:0];
addClassVC.delegate = self;
You can do it in fewer lines but it's a mess of casting and nested brackets, which is harder to debug.
Is it possible your main view controller is being released? That would set the weak reference to nil and the message you send to your delegate would simply be ignored because you'd be messaging nil.
Everything is perfect. I haven't seen any issue. Keep break points everywhere and debug it.
I hope I'm not asking something that's been already answered (but I found no answer to this, so hopefully I'm not).
I have an app in the current xcode version, using segues and navigationController. I need to pass data from one view to the other - what's the easiest way to do this? I ran onto some sharedData thing that could be possibly hooked onto the performSegueWithIdentifier method but don't know how to use it (or whether it is the right choice to do it like this).
Thanks
A segue has two view controllers: sourceViewController and destinationViewController. When UIKit executes a segue, it sends a prepareForSegue:sender: message to the source VC. You can override that method in your view controller subclass to pass data to the destination VC.
For example, suppose you have a master view controller with a table view of movies, and when the user clicks a row in the table view, you want to segue to a detail view controller for the movie.
#implementation MasterViewController
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *detailVC = segue.destinationViewController;
NSIndexPath *selectedPath = [self.tableView indexPathForSelectedRow];
detailVC.movie = [self movieForIndexPath:selectedPath];
}
This is explained in the Introducing Interface Builder Storyboarding video from WWDC 2011.
It's also worth noting that when the segue's origin is a table view cell, or the accessory button of a table view cell, the sender argument of prepareForSegue:sender: is the table view cell.
I think the best way is to import header for the view controller that will be shown in controller that is performing segue. And then use it's accessors or other methods to pass needed data inside prepareForSegue:
// In FirstViewController.h
#import "SecondViewController.h"
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"SegueToSecondViewController"]) {
// Get destination view controller and don't forget
// to cast it to the right class
SecondViewController *secondController = [segue destinationViewController];
// Pass data
secondController.dataArray = self.someDataArray;
secondController.name = #"Fancy name";
}
}
When you want data back from second to first, I suggest to use delegate:
// In FirstViewController.h
#import "SecondViewController.h"
#import "SecondViewControllerDelegate.h"
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"SegueToSecondViewController"]) {
SecondViewController *secondController = [segue destinationViewController];
// Declare first view controller as a delegate
secondController.delegate = self;
// Pass data
secondController.dataArray = self.someDataArray;
secondController.name = #"Fancy name";
}
}
// Second controller's delegate method,controller
// ie. used to return data after second view is dismissed
- (void)secondControllerFinishedSomeTask:(NSArray *)someReturnedData {
// Do something with returned data
}
When you want data back from second to first, better way is to use Unwind Segues.