get method parameters on prepareForSegue - objective-c

I have a method that gets the data(specifiers and title) from that row and i after segue.
- (void)yd_deleteViewControllerWithTargetTypeSpecifiable:(id<YDTargetTypeSpecifiable>)specifier
title:(NSString *)title
onClose:(void(^)(void))onCloseBlock
{
NSLog(#"%#/%#", title, specifier.target_id);
[self performSegueWithIdentifier:SEUGE_GO_DELETE sender:self];
}
How can i pass that parameter from prepareForSegue?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:SEUGE_GO_DELETE]) {
// Get destination view
YDDeleteViewController *makeDC = (YDDeleteViewController*)segue.destinationViewController;
// Pass the information to your destination view
makeDC.labelname = #"title";
}
}

Store the info you need in ivars (properties):
NSLog(#"%#/%#", title, specifier.target_id);
[self setTitleInfo:title];
[self setSpecifierInfo:specifier];
[self performSegueWithIdentifier:SEUGE_GO_DELETE sender:self];
Then in prepareForSegue you pick them up:
YDDeleteViewController *makeDC = (YDDeleteViewController*)segue.destinationViewController;
// pick up [self titleInfo] and pass it along to makeDC
// pick up [self specifierInfo] and pass it along to makeDC

Related

How to set destination view controller using indexpath value?

I have a collection view controller if the user select any cell means its need to go destination view controller.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
cell=[collectionview cellForItemAtIndexPath:indexNumberStorager];
NSInteger indexnum;
indexnum=indexNumberStorager.row;
switch (indexnum) {
case 1:
if ([segue.identifier isEqualToString:#"Car"])
{
CarCleaningVC *dest = [segue destinationViewController];
}
break;
default:
break;
}
i tried using this code but not working properly please help me to find out the problem.
sender here is the cell that has been selected by the user (if the segue is on cell selection). You don't need to store indexPath anywhere.
Simply typecast sender into UICollectionViewCell(or your subclass if you're using any), and call indexPathForCell: on your collection view to get the indexPath.
Sample code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSUInteger indexNum = [self.collectionView indexPathForCell:(UICollectionViewCell*)sender].row;
switch (indexNum)
// Relevant code
}
Also, instead of depending on the segue identifier, try to use the destinationViewController's class:
if ([segue.destinationViewController isKindOfClass:[CarCleaningVC class]]) {
//relevant code
}
You can do something like this-
in your cellForItemAtIndexPath method, set the cell tag. Something like-
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.tag=indexPath.row;
return cell;
}
And then when you cast your sender to a cell, you can just access its tag which is your Collection View's Indexpath.row.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UICollectionViewCell *cell=(UICollectionViewCell*)sender;
NSInteger indexnum;
indexnum=cell.tag;
switch (indexnum) {
case 1:
if ([segue.identifier isEqualToString:#"Car"])
{
CarCleaningVC *dest = [segue destinationViewController];
}
break;
default:
break;
}

How to show the data of a textfield to the label of a tableview cell?

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:^{
}]

Retrieving UITableViewCell Label Values For Passing To Modal Edit View

I am trying to pass existing attribute values for a managed object, to a modal segue view for editing. From "editing mode," I'm going from a UITableViewController subclass (BreadRackTableViewController), using a modal segue to a UIViewController subclass (EditBreadRackTableViewController) by means of an embedded UINavigationController. Normal TableViewCell selection pushes to another UITableViewController subclass, but that's not important for the time being.
I've added properties to my editing view controller header (and #synthesized), to function as getters:
#property (nonatomic, strong) id existingBreadRackDataName;
#property (nonatomic, strong) id existingBreadRackDataDetails;
What I'm trying to do is populate the edit view's fields with the existing Core Data Entity Attributes, which would have been previously set by the user (in theory). I can get the data to show up if I pass along a literal string value, but I need to retrieve and pass the existing data.
Here's what I have currently:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"editBreadRackSegue"])
{
EditBreadRackTableViewController *ebrtvc = (EditBreadRackTableViewController *)[segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:indexPath];
// [ebrtvc setBreadRackClass:breadRackClass];
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:#"breadRackDataDetails"];
}
}
These last two lines of code...
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:#"breadRackDataDetails"];
...they both return nil. If I specify a literal string though, that literal string shows up where it needs to.
All of that said, my question is:
How do I retrieve the attribute's key value for that selected row (or just the textLabel and detailTextLabels), and assign it to my setter?
Solution:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"editBreadRackSegue"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:indexPath];
[segue.destinationViewController setExistingBreadRackDataName:[breadRackClass valueForKey:#"breadRackDataName"]];
[segue.destinationViewController setExistingBreadRackDataDetails:[breadRackClass valueForKey:#"breadRackDataDetails"]];
}
// code for other segues here...
}
I ended up removing the ebrtv variable, and cleaned up the code a little bit. What worked though, was using the indexPathForCell:sender instead of indexPathForSelectedRow when declaring my indexPath for breadRackClass.
I think the indexPathForSelectedRow could be a problem.
You can just pass the indexPath from the didSelectRowAtIndexPath to the prepareForSegue , like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"editBreadRackSegue" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"editBreadRackSegue"])
{
NSIndexPath *clickedIndexPath = (NSIndexPath*)sender;
if( [clickedIndexPath isKindOfClass:[NSIndexPath class]] ){
BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:clickedIndexPath];
EditBreadRackTableViewController *ebrtvc = (EditBreadRackTableViewController *)[segue destinationViewController];
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:#"breadRackDataDetails"];
}
}
}
Let me know if it works for you.

What CallOut have I pressed?

If I have many Annotation in my map view… how to understand what CallOut I've pressed? In tableview for example we have "didSelectRowAtIndexPath"... but here?
-(void)button:(id)sender {
[self performSegueWithIdentifier: #"dettaglio"sender: self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if (![[segue identifier] isEqualToString:#"dettaglio"]&&![[segue identifier]
isEqualToString:#"cerca"])
{
[(paginaOpzioni*)segue.destinationViewController setDelegate:self];
}
if ([[segue identifier] isEqualToString:#"dettaglio"])
{
paginaDettaglio *ibcVC = [segue destinationViewController];
Annotazione *annotazioneDaPassare=[mapAnnotations objectAtIndex:2];
ibcVC.immagine = annotazioneDaPassare.immagine;
ibcVC.navigationItem.title = annotazioneDaPassare.title;
}
}
I need to change the value (2) of [mapAnnotations objectAtIndex:2]; because I want to send to the DestinationViewController the right image and text! Thanks
Your MKMapViewDelegate will tell you what was selected when it calls -mapView:didSelectAnnotationView:
http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/occ/intfm/MKMapViewDelegate/mapView:didSelectAnnotationView:
Unless of course you were referring to the accessory being tapped. That would be -mapView:annotationView:calloutAccessoryTapped:
http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/occ/intfm/MKMapViewDelegate/mapView:annotationView:calloutAccessoryControlTapped:

How do I call the following method from this IBAction?

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 :)