How do I call the following method from this IBAction? I want to call the prepareForSegue: method instead of the NSLog(#"clicked");
Here's the IBAction:
- (IBAction) annotationViewClick:(id) sender {
NSLog(#"clicked");
}
Here's the method:
// Do some customisation of our new view when a table item has been selected
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure we're referring to the correct segue
if ([[segue identifier] isEqualToString:#"ShowMoreInfo"]) {
// Get reference to the destination view controller
MoreDetailViewController *mdvc = [segue destinationViewController];
[mdvc setSelectedItemName:[NSString stringWithFormat:#"%#", placeName.text]];
[mdvc setSelectedItemAddress:[NSString stringWithFormat:#"%#", placeFormattedAddress.text]];
[mdvc setSelectedItemWeb:[NSString stringWithFormat:#"%#", placeWebsite.text]];
[mdvc setSelectedItemRating:[NSString stringWithFormat:#"%#", placeRating.text]];
// [mdvc setSelectedItemDistance:[NSString stringWithFormat:#"%#", placeDistance.text]];
}
}
thanks for the help!
prepareForSegue: method calls automatically when you performing a segue.
To perform a segue you should use performSegueWithIdentifier:,
for ex. [self performSegueWithIdentifier:#"showGuide" sender:self];,
in this case you need assign identifier to a segue in Xcode Interface Builder.
id try something like
UIStoryBoardSegue *segue = ... setup the one you want
[self performSelector:#selector(prepareForSegue:) withObject:seque afterDelay:0];
ive never worked with storyboards - but thats how you call a selector :)
Related
I am having a problem, I want to change to another viewcontroller when a timer expires and this works with this code:
- (IBAction)Akkoord:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"Innameformulier"];
[self presentViewController:vc animated:YES completion:nil];
[self performseguewithidentifier:#"nextcontroller"]
}
But when I use this, my variables in my prepareforsegue method are not passing. How can I do this? I already tried [self performseguewithidentifier] but this is not working.
my preparforsegue code is:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
Innameformulier *IForm = [segue destinationViewController];
IForm.SignatureTransport = _drawImage.image;
IForm.KlantnaamInname = _Klantnaamtransport;
}
How can I call this function to happen on the timer?
In Interface Builder, select the View Controller from which you want to fire a segue, CTRL-drag from it to the destination View Controller and create a segue.
Select the segue, open the Attributes Inspector and create an Identifier for it.
Go back to your View Controller Class that fires the segue and if you don't already have it commented, add the method prepareForSegue:
In your prepareForSegue: method you should have something like the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue {
if ([segue.identifier isEqualToString:#"XXXX"]) {
DestinationVC *dVC = segue.destinationViewController;
dVC.attribute1 = self.anAttribute;
dVC.attribute2 = self.anotherAttribute;
}
}
And now you can fire the segue by calling [self performSegueWithIdentifier:#"XXXX"].
Note: you can also fire an action segue from an UIControl subclass like UIButton
OK, what you are doing here is bypassing the segue completely. You need to do something like this...
- (IBAction)Akkoord:(id)sender {
[self performseguewithidentifier:#"nextcontroller"]
}
This is all you need to do. The segue will then create the nextController for you and then the code inside prepareForSegue will pass the variables across.
If you want it on a timer then you just need to set up a timer and when it expires you can run...
[self performseguewithidentifier:#"nextcontroller"]
As long as you have a segue with this identifier then it will work anywhere. Ever after a timer.
I am new to iOS developing. I am building a project where I want to show the text of a textfield of a view controller to the label of a table view cell of another viewcontroller. Can anyone tell me just the logic how to do that?
Since you haven't posted any code, I will try to explain it to you.
You will have to create event for button, and there you can do something like [self performSegueWithIdentifier:#"yourSegue" sender:self];
Next thing is to create property in your new ViewController that will accept your value.
After that, in your prepareForSegue you can do NSString *text = [self.textField text];, get your new VC from segue.destinationViewController; and assign your value there.
So, in the end you will have :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if(segue.identifier isEqualToString:#"yourSegue"])
{
YourViewController *vc = segue.destinationViewController;
vc.accpetValue = [self.yourTextField text];
}
}
and in your action for button:
- (IBAction)yourButton_pressed:(id)sender
{
[self performSegueWithIdentifier:#"yourSegue" sender:self];
}
If you have tableViewController there, and you want to assign this value to the cell on the same screen, on button you can obtain the value with same way as above, and in you cellAtIndexPath you can do the following:
if(self.yourTextBox && [self.yourTextBox text] && ![[self.yourTextBox text] isEqualToString:#""])
{
cell.textLabel.text = [self.yourTextBox text];
}
else
{
//something else
}
And on your button event you can do
[self.tableView reloadData];
Hope it helps.
If the view controller with the cell is open from the view controller with the textfield, is quite easy.
You can create a property NSString in the second view controller .h and then you can call it when you open this from the first.
SecondViewController * cotroller = [[SecondViewController alloc]init];
controller.string = [textField text];
[self presentViewController:controller animated:YES completion:^{
}]
I recently started using storyboard.
I have the main viewcontroller, which by code must call another viewcontroller.
in storyboard, I created a "segue" that goes from the first to the second controller, with the identifier "segueToSignSelection."
in the main viewcontroller, when I need to call up the second viewcontroller, insert this code:
[self performSegueWithIdentifier:#"segueToSignSelection" sender:self];
that calls this method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"segueToSignSelection"])
{
signSelectionViewController = (SignSelectionViewController*)[segue destinationViewController];
[self presentModalViewController:segue.destinationViewController animated:YES];
[segue.destinationViewController test];
}
}
in the second controller, the test method is invoked. But the view does not appear. What could be the problem?
Thanks
This is a noob question.what i want is to pass data from my QuizViewcontroller to QuizModalViewController.i was successful in creating a normal modal view controller but having problem when passing data between the two..below is my code.
QuizViewController
-(IBAction)button3Clicked:(id)sender
{
QuizModalViewController *mvid=[[QuizModalViewController alloc]initWithNibName:#"QuizModalViewController" bundle:nil];
mvid.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:mvid animated:YES];
}
QuizModalViewController
- (IBAction)goBackToView
{
[self dismissModalViewControllerAnimated:YES];
//[controller loadQuestion:currentQuestionIndex+1];
}
-(IBAction)button3Clicked:(id)sender
{
QuizModalViewController *mvid=[[QuizModalViewController alloc]initWithNibName:#"QuizModalViewController" bundle:nil];
mvid.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mvid.theDataYouWantToPass = theData; // this is how you do it
[self presentModalViewController:mvid animated:YES];
}
Note that theDataYouWantToPass must be a property declared in the QuizModalViewController.h file.
You can also use the -(void)prepareForSegue:... - Method to share data between controllers.
For Example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"Your_Identifier"]) {
TestViewController *controller = [segue destinationViewController];
NSArray *array = [[NSArray alloc] initWithObjects:#"",nil];
controller.objectInTestViewController = array;
}
}
Hope this helps someone....
Make your QuizModalViewController available at class scope instead of using a local variable.
Then in the goBackToView: you can access the controller's content prior to dismissing it.
By the way: in your code you're leaking mvid. Release it after presenting it modally.
Here is my code, posted in view1:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([segue.identifier isEqualToString:#"toLevels"])
{
levelViewController *vc = [segue destinationViewController];
vc.currentEnchantmentSelected = self.title;
}
}
This doesn't send the value of "self.title" to the variable in "levelViewController". Why is this? It sends it if I use a string.
Edit (levelViewController):
#synthesize enchantment = _enchantment;
- (enchantmentViewController *)enchantment
{
if (!_enchantment){
_enchantment = [[enchantmentViewController alloc] init];
}
return _enchantment;
}
the #property enchantment is declared in the header.
So from the chat, the issue was that you were setting this property in didSelectRowAtIndexPath:, which is actually called after prepareForSegue:sender: with storyboards. (when the segue is attached to a cell in a table)
So the solution is just to do that work directly in prepareForSegue:sender: instead. The sender will be the cell that was tapped. And if you need the indexPath for that cell you can use:
[self.tableView indexPathForSelectedRow]
Good luck with the app.