M
M
MagoVinch2014-01-10 17:38:46
Objective-C
MagoVinch, 2014-01-10 17:38:46

How to pass a parameter through segue?

You need to pass the cell index to the tableview via segue.
Or how to save it in a singleton.
How?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Zamula, 2014-01-10
@MagoVinch

To begin with, we redefine the following function for tableView and initialize the needValue variable (which we will pass), after which the performSegueWithIdentifier method: programmatically make the transition to the next viewController

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   needValue = idexPath.row; //Инициализируем номер ячейки
   [tableView deselectRowAtIndexPath:indexPath animated:true]; //Убираем select с ячейки, что бы при возвращении она не была выбрана
   [self performSegueWithIdentifier:@"schoolsToLogin" sender:nil]; //Инициализируем переход
}

Next, we override the prepareForSegue: method as follows:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([segue.identifier isEqualToString:@"yourSegue"]) //Проверяем тот ли это segue, который нам нужен
    {
        nextViewController *nextController = (nextViewController *)segue.destinationViewController; //Создаем ссылку на viewController который будет вызван в результате segue
        
        [nextController setNeedValue: _needValue]; //инициализируем значение нужного viewController
    }
}

In this code, we create a link to the next controller, where we have already prepared a variable in advance to store the cell number (needValue) and by calling the setNeedValue method we set a value for it, after which we can use it.

A
Alexey Storozhev, 2014-01-11
@storoj

if you use UITableViewController, then it has a property clearsSelectionOnViewWillAppearand you won't have to manually deselect it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question