Value returns by unwind is "null" - ios

i try to use an unwind method to return the value of a row (from a table ) in another view
but the value catch in the first viewController is null.
This is the code in FirstViewController
- (IBAction)retrievePremadeMessage:(UIStoryboardSegue *)segue;(h)
- (IBAction)retrievePremadeMessage:(UIStoryboardSegue *)segue
{
PreStoredMessagesTableViewController *cc = segue.sourceViewController;
self.textViewMsg = [[UITextView alloc] init];
self.textViewMsg.text = cc.valeurCell;
NSLog(#"cell selected: %#", cc.valeurCell);
}
and this is the secondViewController
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Retrieve the value of cell selected
self.valeurCell = [NSString stringWithFormat:#"%#", [tableView cellForRowAtIndexPath:indexPath].textLabel.text];
}
Thank you

In this method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Retrieve the value of cell selected
self.valeurCell = [NSString stringWithFormat:#"%#", [tableView cellForRowAtIndexPath:indexPath].textLabel.text];
}
Add:
[self performSegueWithIdentifier:#"identifier" sender:[NSString stringWithFormat:#"%#", [tableView cellForRowAtIndexPath:indexPath].textLabel.text]];
and then you can do something like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *stringPassedIn = id;
}

Related

IOS/Objective-C: Passing data (array) and receiving detailed strings

I'm trying to pass data (array) with PrepareForSegue into a detailed VC. The destination VC has a label that receives a string. I thought I could transform the array into a string, and the rest was straightforward. This was achieved, though, but kind of wrongly, because, obviously, the text I get, regardless the row I click, returns me the whole object...And my goal is to get details of a specific row...Can someone help me? Guess this is basic stuff, but this is my first app...The method PrepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detail"]) {
NSString *places = [_spots componentsJoinedByString:#" "];
DetailViewController * destination = segue.destinationViewController;
destination.detailedText = places;
}
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSManagedObject *ls = [self.spots objectAtIndex:indexPath.row];
NSLog(#"Table Spots are: %#", ls);
[cell.textLabel setText:[NSString stringWithFormat:#"%#", [ls valueForKey:#"name"]]];
cell.imageView.image = [UIImage imageNamed:#"city.jpg"];
return cell;
}
As far as I understood, you want to performSegue() on a row selection and you want to pass a data specific to the selected row. In order to do this you can use the tableView.indexPathForSelectedRows() method, which is going to return an array with the indexPaths of all selected rows. Then you can get the UITableViewCells for this indexPaths and get their data.
You can pass the string value to the perforSegue method as the sender:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"detail" sender:[_spots objectAtIndex:indexPath.row]];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"detail"]) {
DetailViewController * destination = segue.destinationViewController;
destination.detailedText = sender;
}
}

didSelectRowAtIndexPath not called the first time

I have a UITableView which when you select a row, it will lead you into the "details" of the selected row. However, the first time I click the row, it goes into the prepareForSegue method, without calling didSelectRowAtIndexPath. When I push the back button on the detail view, the app goes back, and if I select the row again, the method will be called.
I'm NOT using the didDeselectRowAtIndexPath.
This patter will continue and I can't figure out why. When I load my data, I do call [tableview reloadData].
I have also tried to add self.TagsTableView.allowsMultipleSelection = YES; to my viewDidLoad method.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
self.bluetoothManager = [[BlueToothLEManager alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadTableView:) name:#"ScanComplete" object:nil];
self.Tags = self.bluetoothManager.Tags;
NSLog(#"Fetch Tags: %#", self.Tags);
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [fetchTagsTableView dequeueReusableCellWithIdentifier:#"tagCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"tagCell"];
}
FetchTag* newTag = [fetchTags objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# %d", newTag.tagName, indexPath.row];
cell.detailTextLabel.text = [newTag.tagUUID UUIDString];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedTagFromTable = [fetchTags objectAtIndex:indexPath.row];
[TagsTableView deselectRowAtIndexPath:indexPath animated:YES];
}
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
if([[segue identifier] isEqualToString:#"DetailView"]){
TagDetailViewController* ftdvc = [segue destinationViewController];
[ftdvc setSelectedTag:selectedTagFromTable];
}
// Pass the selected object to the new view controller.
}
-(void)reloadTableView:(NSNotification*)notif{
NSLog(#"Reloading Table View");
NSLog(#"Fetch Tags: %#", self.Tags);
[self.TagsTableView reloadData];
}
I would have put the code from the didSelectRowAtIndexPath: to the prepareForSegue: instead Try using the sender parameter to identify a selected tag

Using an “IF” statement for popping to two possible View Controllers

Currently I just have a segue hooked up to a UITableView that pops to the next UITableView in the navigation stack.
What I need to do though now instead, is set up an if statement based on the text in the UITableView cell, to decide which of two possible UIViewControllers to pop to.
So ViewController2 needs to be able to pop either":
- back to ViewController1
OR
- forward to ViewController3
based on the text in the UITableView cell that is populated by JSON.
Can you help me out with that? Will post any code needed. Thanks!
Current ViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Spring *spring = [springs objectAtIndex:indexPath.section];
Leaf *leaf = [spring.leafs objectAtIndex:indexPath.row];
cell.textLabel.text = leaf.shortName;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:#"themes"]) {
UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Get reference to destination view controller
MRTViewController *tvc = (MRTViewController *)[segue destinationViewController];
}
}
UPDATE
Added code per #troops but still not sure how to pop to VC3
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *stringToCheck = #"AAA";
if([cell.textLabel.text isEqualToString:stringToCheck])
{
//Pop to VC1
[self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:0] animated:YES];
} else {
//Pop to VC3, not sure what to put here
// Pass the info
tvc.spring = [springs objectAtIndex:indexPath.section];
tvc.leaf = [tvc.spring.leafs objectAtIndex:indexPath.row];
tvc.buttonNumber = _buttonNumber;
}
}
You could simply check the cell's textLabel's string: If you really want to use segues and not the tableView's delegate method of: didSelectRowAtIndexPath: That's why I based my answer off what your initial question/code looks like.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *stringToCheck = #"AAA";
if ([cell.textLabel.text isEqualToString:stringToCheck])
{
// Pop to VC1
[self.navigationController popToRootViewControllerAnimated:YES];
}
else
{
// Push to VC3
MRTViewController3 *tvc = [self.storyboard instantiateViewControllerWithIdentifier:#"YourID"];
tvc.spring = [springs objectAtIndex:indexPath.section];
tvc.leaf = [tvc.spring.leafs objectAtIndex:indexPath.row];
tvc.buttonNumber = _buttonNumber;
[self.navigationController pushViewController:tvc animated:YES];
}
}
Use delegate method, get cell, compare text.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([cell.textLabel.text isEqualToString:#"Your String"])
{
//Pop or present view controller
}
}

-[UIViewController setSelectedRowTitle:]: unrecognized selector sent to instance 0x13fab360

I am trying to pass data from one table view Cell to next UIViewController . I am using Navigation Controller also , But when i am clicking Cell then it giving error :-
-[UIViewController setSelectedRowTitle:]: unrecognized selector sent to instance 0x13fab360
my prepareForSegue code which i used go next with data is:-
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [NearBy_PlacesArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TableIdentifire = #"NearMe";
NearByTableViewCell *cell = (NearByTableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:TableIdentifire];
if (cell == nil)
{
cell = [[NearByTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifire];
}
NearByPlace *nearBy = [NearBy_PlacesArray objectAtIndex:indexPath.row];
cell.NearBy_Thumbnail_Image.image = [UIImage imageNamed:nearBy.NearByImageName];
cell.NearBy_TitleLabel.text = nearBy.NearByPlaceName;
cell.NearBy_NoLabel.text = nearBy.NearByPlace_no;
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ShowNearByPlaces"])
{
NSIndexPath *indexPath = nil;
NearByPlace *nearPlace = nil;
indexPath = [self.tableView indexPathForSelectedRow];
nearPlace = [NearBy_PlacesArray objectAtIndex:indexPath.row];
NSLog(#"indexPath-- %# \n" , indexPath);
NSLog(#"recipe-- %# \n " , nearPlace);
nearByPlaceDetail_ViewController *destViewController = segue.destinationViewController;
destViewController.SelectedRowTitle = nearPlace;
}
}
Mantosh, you can simply do this :
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *aStrIdentifier = #"cell";
UITableViewCell *aCell=[tableView dequeueReusableCellWithIdentifier:aStrIdentifier];
UILabel *aLblPlaceName=(UILabel*)[aCell viewWithTag:1];
aLblPlaceName.text=[NSString stringWithFormat:#"%#", [arrPlaces objectAtIndex:indexPath.row]];
return aCell;
}
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
strStoreValue = [NSString stringWithFormat:#"%#", [arrPlaces objectAtIndex:indexPath.row]];
[self performSegueWithIdentifier:#"detailViewSegue" sender:self];
}
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *aObjDetailVC = segue.destinationViewController;
aObjDetailVC.strPlace = strStoreValue;
}

Different Arrays in tableview

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Abelia"]) {
if ([[segue destinationViewController] isKindOfClass:[SpeciesViewController class]]) {
SpeciesViewController *Species = (SpeciesViewController *)[segue destinationViewController];
Species.SpeciesInt = 0;
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
if (SpeciesInt == 0)
return [abeliaArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SpeciesNameCell";
SpeciesNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[SpeciesNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"SpeciesNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
if (SpeciesInt == 0)
cell.SpeciesNameLabel.text = [abeliaArray objectAtIndex:indexPath.row];
return cell;
}
Im not sure where the problem in this 3 methods, it runs but the abeliaArray will run in place of the other arrays when I try to push. I have other arrays but when I push those the objects in the abeliaArray will show instead of the objects for that array.

Resources